-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure environment is set on prefetch (#263)
- Loading branch information
1 parent
5901129
commit 35cc6fd
Showing
8 changed files
with
178 additions
and
45 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
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
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
92 changes: 92 additions & 0 deletions
92
packages/js-client/src/__tests__/ClientAndEvironments.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,92 @@ | ||
import fetchMock from 'jest-fetch-mock'; | ||
import { InitResponseString, MockLocalStorage } from 'statsig-test-helpers'; | ||
|
||
import StatsigClient from '../StatsigClient'; | ||
|
||
describe('StatsigClient and Environments', () => { | ||
const user = { userID: 'a-user' }; | ||
const env = { statsigEnvironment: { tier: 'dev' } }; | ||
const expectedCacheKey = 'statsig.cached.evaluations.1769418430'; // DJB2(JSON({userID: 'a-user', statsigEnvironment: {tier: 'dev'}})) | ||
|
||
let storageMock: MockLocalStorage; | ||
let client: StatsigClient; | ||
|
||
beforeAll(() => { | ||
storageMock = MockLocalStorage.enabledMockStorage(); | ||
fetchMock.enableMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
MockLocalStorage.disableMockStorage(); | ||
}); | ||
|
||
beforeEach(() => { | ||
storageMock.clear(); | ||
|
||
fetchMock.mock.calls = []; | ||
fetchMock.mockResponse(InitResponseString); | ||
|
||
client = new StatsigClient('client-key', user, { | ||
environment: { tier: 'dev' }, | ||
disableStatsigEncoding: true, | ||
}); | ||
}); | ||
|
||
describe('When triggered by StatsigClient', () => { | ||
it('sets the environment on post sync init requests', async () => { | ||
client.initializeSync(); | ||
await new Promise((r) => setTimeout(r, 1)); | ||
|
||
const [, req] = fetchMock.mock.calls[0]; | ||
const body = JSON.parse(String(req?.body)); | ||
|
||
expect(body.user).toMatchObject(env); | ||
expect(storageMock.data[expectedCacheKey]).toBeDefined(); | ||
}); | ||
|
||
it('sets the environment on async init requests', async () => { | ||
await client.initializeAsync(); | ||
|
||
const [, req] = fetchMock.mock.calls[0]; | ||
const body = JSON.parse(String(req?.body)); | ||
|
||
expect(body.user).toMatchObject(env); | ||
expect(storageMock.data[expectedCacheKey]).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('When triggered by DataAdapter', () => { | ||
it('sets the environment on prefetch requests', async () => { | ||
await client.dataAdapter.prefetchData(user); | ||
|
||
const [, req] = fetchMock.mock.calls[0]; | ||
const body = JSON.parse(String(req?.body)); | ||
|
||
expect(body.user).toMatchObject(env); | ||
expect(storageMock.data[expectedCacheKey]).toBeDefined(); | ||
}); | ||
|
||
it('sets the environment on getDataAsync requests', async () => { | ||
await client.dataAdapter.getDataAsync(null, user, {}); | ||
|
||
const [, req] = fetchMock.mock.calls[0]; | ||
const body = JSON.parse(String(req?.body)); | ||
|
||
expect(body.user).toMatchObject(env); | ||
expect(storageMock.data[expectedCacheKey]).toBeDefined(); | ||
}); | ||
}); | ||
|
||
it('includes env on DataAdapter reads', () => { | ||
storageMock.data[expectedCacheKey] = JSON.stringify({ | ||
source: 'Network', | ||
receivedAt: Date.now(), | ||
data: InitResponseString, | ||
stableID: null, | ||
fullUserHash: null, | ||
}); | ||
|
||
expect(client.dataAdapter.getDataSync(user)).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.