Skip to content

Commit

Permalink
feat: Make konnector prop mandatory to FlowProvider
Browse files Browse the repository at this point in the history
Now it won't be possible to have a ConnectionFlow created in
FlowProvider without a defined konnector, because it has no meaning at
all.
  • Loading branch information
doubleface authored and doubleface committed Apr 5, 2023
1 parent e04a9ee commit f3e3d48
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 14 deletions.
Expand Up @@ -18,7 +18,7 @@ import FlowProvider from '../FlowProvider'
*/
const Status = ({ t, trigger, konnector }) => {
return (
<FlowProvider initialTrigger={trigger}>
<FlowProvider initialTrigger={trigger} konnector={konnector}>
{({ flow }) => {
const { error, running } = flow.getState()
const errorTitle = getErrorLocale(error, konnector, t, 'title')
Expand Down
2 changes: 2 additions & 0 deletions packages/cozy-harvest-lib/src/components/FlowProvider.jsx
Expand Up @@ -5,6 +5,7 @@ import { createPortal } from 'react-dom'
import { withClient } from 'cozy-client'
import { translate } from 'cozy-ui/transpiled/react/I18n'

import assert from '../assert'
import ConnectionBackdrop from './AccountForm/ConnectionBackdrop'
import TwoFAModal from './TwoFAModal'
import logger from '../logger'
Expand Down Expand Up @@ -38,6 +39,7 @@ export class FlowProvider extends Component {
constructor(props, context) {
super(props, context)
const { initialTrigger, konnector } = this.props
assert(konnector, 'no konnector given to FlowProvider')
this.state = {
showTwoFAModal: false
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-harvest-lib/src/components/OAuthForm.jsx
Expand Up @@ -73,7 +73,7 @@ export const OAuthForm = props => {
if (response.result === OAUTH_SERVICE_OK) {
const konnectorPolicy = findKonnectorPolicy(konnector)
if (konnectorPolicy.isBIWebView && flag('harvest.bi.fullwebhooks')) {
flow.expectTriggerLaunch({ konnector })
flow.expectTriggerLaunch()
}
const accountId = response.key
if (typeof onSuccess === 'function') onSuccess(accountId)
Expand Down
3 changes: 3 additions & 0 deletions packages/cozy-harvest-lib/src/components/TriggerManager.jsx
Expand Up @@ -475,6 +475,7 @@ const LegacyTriggerManager = props => {
onLoginSuccess,
onError,
initialTrigger,
konnector,
...otherProps
} = props

Expand All @@ -494,13 +495,15 @@ const LegacyTriggerManager = props => {
onSuccess={onSuccess}
onLoginSuccess={onLoginSuccess}
onError={onError}
konnector={konnector}
initialTrigger={initialTrigger}
>
{({ flow }) => (
<TriggerManager
{...otherProps}
error={flow.getState().error}
flow={flow}
konnector={konnector}
/>
)}
</FlowProvider>
Expand Down
Expand Up @@ -132,7 +132,7 @@ describe('LaunchTriggerCard', () => {

it('should display a syncing message when a trigger launch is expected', async () => {
const flow = new ConnectionFlow(client, triggerFixture, konnectorFixture)
flow.expectTriggerLaunch({ konnector: konnectorFixture })
flow.expectTriggerLaunch()

const { root } = setup({
props: {
Expand Down
8 changes: 3 additions & 5 deletions packages/cozy-harvest-lib/src/models/ConnectionFlow.js
Expand Up @@ -351,8 +351,7 @@ export class ConnectionFlow {
* @param {Object} options
* @param {import('cozy-client/types/types').KonnectorsDoctype} options.konnector
*/
expectTriggerLaunch({ konnector }) {
this.konnector = konnector
expectTriggerLaunch() {
// @ts-ignore
logger.info(
`ConnectionFlow: Expecting trigger launch for konnector ${this.konnector.slug}`
Expand Down Expand Up @@ -477,12 +476,11 @@ export class ConnectionFlow {
})
this.trigger = trigger
this.account = account
this.konnector = konnector

this.t = t

assert(client, 'No client')
const konnectorPolicy = findKonnectorPolicy(konnector)
const konnectorPolicy = this.getKonnectorPolicy()
// @ts-ignore
logger.log(
`ConnectionFlow: Handling submit, with konnector policy ${konnectorPolicy.name}`
Expand Down Expand Up @@ -723,7 +721,7 @@ export class ConnectionFlow {
// this will especially allow to show the backdrop effect to tell the user to stay until the login is successful
this.setState({ firstRun: true })
}
const konnectorPolicy = findKonnectorPolicy(this.konnector)
const konnectorPolicy = this.getKonnectorPolicy()

const computedAutoSuccessTimer = autoSuccessTimer

Expand Down
12 changes: 6 additions & 6 deletions packages/cozy-harvest-lib/src/models/ConnectionFlow.spec.js
Expand Up @@ -809,7 +809,7 @@ describe('ConnectionFlow', () => {
.mockReset()
.mockReturnValue(onAccountCreationResult)

const { flow, client } = setup()
const { flow, client } = setup({ konnector: bankingKonnector })

jest.spyOn(flow, 'launch')

Expand All @@ -836,7 +836,7 @@ describe('expectTriggerLaunch', () => {
const { flow } = setup({ trigger: fixtures.erroredTrigger })
flow.setState({ accountError: 'error to hide' })

flow.expectTriggerLaunch({ konnector: fixtures.konnector })
flow.expectTriggerLaunch()

const { accountError } = flow.getState()
expect(accountError).toBe(null)
Expand All @@ -845,7 +845,7 @@ describe('expectTriggerLaunch', () => {
it('sets the flow status to EXPECTING_TRIGGER_LAUNCH', () => {
const { flow } = setup({ trigger: fixtures.runningTrigger })

flow.expectTriggerLaunch({ konnector: fixtures.konnector })
flow.expectTriggerLaunch()

const { status } = flow.getState()
expect(status).toBe(EXPECTING_TRIGGER_LAUNCH)
Expand All @@ -854,7 +854,7 @@ describe('expectTriggerLaunch', () => {
it('starts watching for konnector jobs creation', () => {
const { flow } = setup({ trigger: fixtures.erroredTrigger })

flow.expectTriggerLaunch({ konnector: fixtures.konnector })
flow.expectTriggerLaunch()

expect(realtimeMock.subscribe).toHaveBeenCalledWith(
'created',
Expand All @@ -872,7 +872,7 @@ describe('expectTriggerLaunch', () => {

it('stops watching for konnector jobs creation', async () => {
const { flow } = setup({ trigger: fixtures.erroredTrigger })
flow.expectTriggerLaunch({ konnector: fixtures.konnector })
flow.expectTriggerLaunch()

const job = konnectorJob(flow)
realtimeMock.events.emit(realtimeMock.key('created', 'io.cozy.jobs'), job)
Expand All @@ -886,7 +886,7 @@ describe('expectTriggerLaunch', () => {

it('starts watching for updates on the job itself', () => {
const { flow } = setup({ trigger: fixtures.erroredTrigger })
flow.expectTriggerLaunch({ konnector: fixtures.konnector })
flow.expectTriggerLaunch()

const watchJobSpy = jest.spyOn(flow, 'watchJob')
try {
Expand Down

0 comments on commit f3e3d48

Please sign in to comment.