Skip to content

Commit

Permalink
feat: Send account and trigger to the launcher
Browse files Browse the repository at this point in the history
When launching an existing trigger

Or else the launcher will reacreate a new account and trigger each time
  • Loading branch information
doubleface authored and doubleface committed Feb 27, 2023
1 parent a1a2d97 commit 5d54d14
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 9 deletions.
16 changes: 9 additions & 7 deletions packages/cozy-harvest-lib/src/models/ConnectionFlow.js
Expand Up @@ -617,6 +617,15 @@ export class ConnectionFlow {
*/
async launch({ autoSuccessTimer = true } = {}) {
const konnectorPolicy = findKonnectorPolicy(this.konnector)

const computedAutoSuccessTimer = autoSuccessTimer

logger.info('ConnectionFlow: Launching job...')
this.setState({ status: PENDING })

if (this.trigger) {
this.account = await prepareTriggerAccount(this.client, this.trigger)
}
if (konnectorPolicy.onLaunch) {
konnectorPolicy.onLaunch({
konnector: this.konnector,
Expand All @@ -627,13 +636,6 @@ export class ConnectionFlow {
if (!konnectorPolicy.needsTriggerLaunch) {
return
}

const computedAutoSuccessTimer = autoSuccessTimer

logger.info('ConnectionFlow: Launching job...')
this.setState({ status: PENDING })

this.account = await prepareTriggerAccount(this.client, this.trigger)
this.realtime.subscribe(
'updated',
ACCOUNTS_DOCTYPE,
Expand Down
40 changes: 40 additions & 0 deletions packages/cozy-harvest-lib/src/models/ConnectionFlow.spec.js
Expand Up @@ -340,6 +340,46 @@ describe('ConnectionFlow', () => {
})
})

describe('launch', () => {
afterEach(() => {
jest.clearAllMocks()
})

it('should call the launcher when needed with existing account and trigger', async () => {
prepareTriggerAccount.mockImplementation(
async () => fixtures.existingAccount
)
const { client } = setup()
const flow = new ConnectionFlow(
client,
fixtures.existingTrigger,
fixtures.clientKonnector
)
window.cozy = {
ClientConnectorLauncher: 'react-native'
}
window.ReactNativeWebView = {
postMessage: jest.fn()
}

await flow.launch()
expect(window.ReactNativeWebView.postMessage).toHaveBeenCalledWith(
JSON.stringify({
message: 'startLauncher',
value: {
connector: fixtures.clientKonnector,
account: fixtures.existingAccount,
trigger: fixtures.existingTrigger
}
})
)
expect(launchTrigger).not.toHaveBeenCalled()

delete window.cozy
delete window.ReactNativeWebView
})
})

describe('handleFormSubmit', () => {
const isSubmitting = flow => {
return flow.getState().running === true
Expand Down
6 changes: 4 additions & 2 deletions packages/cozy-harvest-lib/src/policies/clisk.js
Expand Up @@ -31,7 +31,7 @@ function isRunnable() {
* @param {Object} options - options object
* @param {import('cozy-client/types/types').KonnectorsDoctype} options.konnector - konnector object
*/
function onLaunch({ konnector }) {
function onLaunch({ konnector, account, trigger }) {
const launcher = getLauncher()
if (launcher) {
logger.debug('Found a launcher', launcher)
Expand All @@ -44,7 +44,9 @@ function onLaunch({ konnector }) {
JSON.stringify({
message: 'startLauncher',
value: {
connector: konnector
connector: konnector,
account,
trigger
}
})
)
Expand Down
51 changes: 51 additions & 0 deletions packages/cozy-harvest-lib/src/policies/clisk.spec.js
Expand Up @@ -27,3 +27,54 @@ describe('isRunnable', () => {
expect(konnectorPolicy.isRunnable()).toBe(false)
})
})

describe('onLaunch', () => {
const cozy = {
ClientConnectorLauncher: 'react-native'
}
beforeEach(() => {
window.cozy = cozy
window.ReactNativeWebView = {
postMessage: jest.fn()
}
})
afterEach(() => {
delete window.cozy
jest.clearAllMocks()
delete window.ReactNativeWebView
})

it('should send konnector when launcher is react-native', () => {
konnectorPolicy.onLaunch({
konnector: { slug: 'testkonnectorslug' }
})
expect(window.ReactNativeWebView.postMessage).toHaveBeenCalledWith(
JSON.stringify({
message: 'startLauncher',
value: {
connector: { slug: 'testkonnectorslug' }
}
})
)
})

it('should also send account and trigger when available when launcher is react-native', () => {
konnectorPolicy.onLaunch({
konnector: {
slug: 'testkonnectorslug'
},
account: { _id: 'testaccountid' },
trigger: { _id: 'testtriggerid' }
})
expect(window.ReactNativeWebView.postMessage).toHaveBeenCalledWith(
JSON.stringify({
message: 'startLauncher',
value: {
connector: { slug: 'testkonnectorslug' },
account: { _id: 'testaccountid' },
trigger: { _id: 'testtriggerid' }
}
})
)
})
})

0 comments on commit 5d54d14

Please sign in to comment.