Skip to content

Commit

Permalink
fix: Make sure the initializing promise is reset on logout
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Jul 8, 2020
1 parent dd3f57f commit 673029b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/cozy-flags/src/flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,19 @@ class FlagClientPlugin {
this.client.on('login', this.handleLogin)
this.client.on('logout', this.handleLogout)

this.setupInitializing()

if (client.isLogged) this.handleLogin()
}

/**
* Sets up a promise that can be awaited to wait for flag complete
* initialization
*/
setupInitializing() {
this.initializing = new Promise(resolve => {
this.resolveInitializing = resolve
})

if (client.isLogged) this.handleLogin()
}

async handleLogin() {
Expand All @@ -154,6 +162,7 @@ class FlagClientPlugin {

async handleLogout() {
flag.reset()
this.setupInitializing()
}
}

Expand Down
18 changes: 18 additions & 0 deletions packages/cozy-flags/src/tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CozyClient from 'cozy-client'
import util from 'util'

export default function testFlagAPI(flag) {
afterEach(() => {
Expand Down Expand Up @@ -112,6 +113,23 @@ export default function testFlagAPI(flag) {
expect(flag('number_of_foos')).toBe(10)
expect(flag('bar_config')).toEqual({ qux: 'quux' })
})

it('should reset the initializing promise on logout', async () => {
const { client } = setup()
client.isLogged = true
client.registerPlugin(flag.plugin)

const initializing1 = client.plugins.flags.initializing
const state = util.inspect(initializing1)
expect(state).toContain('pending')
await client.plugins.flags.initializing
const state2 = util.inspect(initializing1)
expect(state2).not.toContain('pending')
await client.logout()
const initializing2 = client.plugins.flags.initializing
expect(initializing2).not.toBe(initializing1)
expect(util.inspect(initializing2)).toContain('pending')
})
})

if (typeof document !== 'undefined') {
Expand Down

0 comments on commit 673029b

Please sign in to comment.