-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix issue where flush callback could be called twice. (#779)
Relates to #777
- Loading branch information
1 parent
1926b49
commit c377e89
Showing
2 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
packages/shared/sdk-server/__tests__/LDClientImpl.events.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { LDClientImpl } from '../src'; | ||
import { createBasicPlatform } from './createBasicPlatform'; | ||
import TestLogger from './Logger'; | ||
import makeCallbacks from './makeCallbacks'; | ||
|
||
it('flushes events successfully and executes the callback', async () => { | ||
const platform = createBasicPlatform(); | ||
platform.requests.fetch.mockImplementation(() => | ||
Promise.resolve({ status: 200, headers: new Headers() }), | ||
); | ||
|
||
const client = new LDClientImpl( | ||
'sdk-key-events', | ||
platform, | ||
{ | ||
logger: new TestLogger(), | ||
stream: false, | ||
}, | ||
makeCallbacks(false), | ||
); | ||
|
||
client.identify({ key: 'user' }); | ||
client.variation('dev-test-flag', { key: 'user' }, false); | ||
|
||
const flushCallback = jest.fn(); | ||
|
||
await client.flush(flushCallback); | ||
|
||
expect(platform.requests.fetch).toHaveBeenCalledWith( | ||
'https://events.launchdarkly.com/bulk', | ||
expect.objectContaining({ | ||
method: 'POST', | ||
body: expect.any(String), | ||
}), | ||
); | ||
expect(flushCallback).toHaveBeenCalledWith(null, true); | ||
expect(flushCallback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('flushes events successfully', async () => { | ||
const platform = createBasicPlatform(); | ||
platform.requests.fetch.mockImplementation(() => | ||
Promise.resolve({ status: 200, headers: new Headers() }), | ||
); | ||
|
||
const client = new LDClientImpl( | ||
'sdk-key-events', | ||
platform, | ||
{ | ||
logger: new TestLogger(), | ||
stream: false, | ||
}, | ||
makeCallbacks(false), | ||
); | ||
|
||
client.identify({ key: 'user' }); | ||
client.variation('dev-test-flag', { key: 'user' }, false); | ||
|
||
await client.flush(); | ||
|
||
expect(platform.requests.fetch).toHaveBeenCalledWith( | ||
'https://events.launchdarkly.com/bulk', | ||
expect.objectContaining({ | ||
method: 'POST', | ||
body: expect.any(String), | ||
}), | ||
); | ||
}); | ||
|
||
it('calls error callback once when flush fails with http status code', async () => { | ||
const platform = createBasicPlatform(); | ||
platform.requests.fetch.mockImplementation(() => | ||
Promise.resolve({ status: 401, headers: new Headers() }), | ||
); | ||
|
||
const flushCallback = jest.fn(); | ||
const client = new LDClientImpl( | ||
'sdk-key-events', | ||
platform, | ||
{ | ||
logger: new TestLogger(), | ||
stream: false, | ||
}, | ||
makeCallbacks(false), | ||
); | ||
|
||
client.identify({ key: 'user' }); | ||
client.variation('dev-test-flag', { key: 'user' }, false); | ||
|
||
await client.flush(flushCallback); | ||
|
||
expect(flushCallback).toHaveBeenCalledWith(expect.any(Error), false); | ||
expect(flushCallback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('calls error callback once when flush fails with exception', async () => { | ||
const platform = createBasicPlatform(); | ||
platform.requests.fetch.mockImplementation(() => Promise.reject(new Error('test error'))); | ||
|
||
const flushCallback = jest.fn(); | ||
const client = new LDClientImpl( | ||
'sdk-key-events', | ||
platform, | ||
{ | ||
logger: new TestLogger(), | ||
stream: false, | ||
}, | ||
makeCallbacks(false), | ||
); | ||
|
||
client.identify({ key: 'user' }); | ||
client.variation('dev-test-flag', { key: 'user' }, false); | ||
|
||
await client.flush(flushCallback); | ||
|
||
expect(flushCallback).toHaveBeenCalledWith(expect.any(Error), false); | ||
expect(flushCallback).toHaveBeenCalledTimes(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters