Skip to content

Commit

Permalink
feat: Emit events when plugins are ready
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lohse committed Jul 8, 2020
1 parent dd3f57f commit 5fc182c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/cozy-flags/src/flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,12 @@ class FlagClientPlugin {
async handleLogin() {
await flag.initialize(this.client)
this.resolveInitializing()
this.client.emit('plugin:flag:login')
}

async handleLogout() {
flag.reset()
this.client.emit('plugin:flag:logout')
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/cozy-flags/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@ export default function testFlagAPI(flag) {
const { client } = setup()
client.isLogged = true
client.registerPlugin(flag.plugin)
const onLogin = jest.fn()
client.on('plugin:flag:login', onLogin)
await client.plugins.flags.initializing
expect(flag('has_feature1')).toBe(true)
expect(flag('has_feature2')).toBe(false)
expect(flag('from_remote')).toBe(true)
expect(flag('number_of_foos')).toBe(10)
expect(flag('bar_config')).toEqual({ qux: 'quux' })
expect(onLogin).toHaveBeenCalled()
})
})

Expand Down
2 changes: 2 additions & 0 deletions packages/cozy-realtime/src/RealtimePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ class RealtimePlugin {
this.realtime = new CozyRealtime({
client: this.client
})
this.client.emit('plugin:realtime:login')
}

handleLogout() {
this.unsubscribeAll()
this.realtime = null
this.client.emit('plugin:realtime:logout')
}

checkRealtimeInstance() {
Expand Down
9 changes: 9 additions & 0 deletions packages/cozy-realtime/src/RealtimePlugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@ it('should expose the same API as CozyRealtime', () => {
it('should login/logout correctly', async () => {
client = new CozyClient({})
client.registerPlugin(RealtimePlugin)

const onLogin = jest.fn()
const onLogout = jest.fn()
client.on('plugin:realtime:login', onLogin)
client.on('plugin:realtime:logout', onLogout)
expect(client.plugins.realtime.realtime).toBeNull()
await client.login({
uri: 'http://cozy.tools:8080',
token: 'fake-token'
})
expect(client.plugins.realtime.realtime).not.toBeNull()
expect(onLogin).toHaveBeenCalledTimes(1)
expect(onLogout).toHaveBeenCalledTimes(0)
await client.logout()
expect(client.plugins.realtime.realtime).toBeNull()
expect(onLogin).toHaveBeenCalledTimes(1)
expect(onLogout).toHaveBeenCalledTimes(1)
})

it('throws user friendly errors when trying to use the realtime while logged out', async () => {
Expand Down

0 comments on commit 5fc182c

Please sign in to comment.