-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BugFix: isCacheLoaded was always false on remounts (#127)
- Loading branch information
1 parent
f41b2f6
commit 071c041
Showing
2 changed files
with
115 additions
and
15 deletions.
There are no files selected for viewing
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
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,93 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import '@testing-library/jest-dom'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import React from 'react'; | ||
import StatsigJS from 'statsig-js'; | ||
import { Statsig, StatsigProvider, useConfig } from '..'; | ||
import * as TestInitializeData from './single_gate_init_response.json'; | ||
|
||
StatsigJS.encodeIntializeCall = false; | ||
|
||
let renderedUserIDs: string[] = []; | ||
|
||
function TestComponent() { | ||
const { config } = useConfig('a_config'); | ||
|
||
const userID = config.get('result', 'not-found'); | ||
renderedUserIDs.push(userID); | ||
|
||
return <div>{userID}</div>; | ||
} | ||
|
||
function createResponseFromExtractedUserID(userID: unknown) { | ||
return { | ||
...TestInitializeData, | ||
...{ | ||
dynamic_configs: { | ||
a_config: { | ||
name: 'a_config', | ||
value: { | ||
result: userID, | ||
}, | ||
rule_id: 'a_rule_id', | ||
group: 'a_group', | ||
is_device_based: false, | ||
secondary_exposures: [], | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
describe('Caching and Updating Users', () => { | ||
(global as any).fetch = jest.fn((_url, request) => { | ||
const response = createResponseFromExtractedUserID( | ||
JSON.parse(request.body).user.userID, | ||
); | ||
|
||
return Promise.resolve({ | ||
ok: true, | ||
status: 200, | ||
text: () => JSON.stringify(response), | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(Statsig as any).instance = null; | ||
renderedUserIDs = []; | ||
}); | ||
|
||
it('waits for cache and updates users', async () => { | ||
render( | ||
<StatsigProvider | ||
sdkKey="client-dummy-key" | ||
user={{ userID: 'a-user' }} | ||
waitForCache={true} | ||
> | ||
<TestComponent /> | ||
</StatsigProvider>, | ||
); | ||
|
||
render( | ||
<StatsigProvider | ||
sdkKey="client-dummy-key" | ||
user={{ userID: 'b-user' }} | ||
waitForCache={true} | ||
> | ||
<TestComponent /> | ||
</StatsigProvider>, | ||
); | ||
|
||
await waitFor(() => screen.getByText('b-user')); | ||
|
||
expect(renderedUserIDs).toEqual([ | ||
'not-found', // cache loaded for a-user, no result | ||
'not-found', // cache loaded for b-user, no result | ||
'b-user', // network loaded for b-user | ||
]); | ||
}); | ||
}); |