Skip to content

Commit

Permalink
Test to verify provider does not access local storage when rendering …
Browse files Browse the repository at this point in the history
…(#106)
  • Loading branch information
tore-statsig authored Apr 3, 2023
1 parent cacbb4e commit cab4cc7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
roots: ['./'],
testMatch: ['**/__tests__/**/*.(ts|tsx)', '**/?(*.)+test.(ts|tsx)'],
testMatch: ['**/__tests__/**/*.tsx', '**/?(*.)+test.tsx'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/dist/'],
preset: 'ts-jest',
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/__tests__/LocalStorageMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default class LocalStorageMock {
constructor() {}

clear(): void {
for (var key in this) {
if (key in ['getItem', 'setItem', 'removeItem']) {
continue;
}
this.removeItem(key);
}
}

getItem(key: string): string | null {
// @ts-ignore
return this[key] ? String(this[key]) : null;
}

setItem(key: string, value: string): void {
// @ts-ignore
this[key] = String(value);
}

removeItem(key: string): void {
// @ts-ignore
delete this[key];
}
}
31 changes: 26 additions & 5 deletions src/__tests__/StatsigSynchronousProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import useUpdateUser from '../useUpdateUser';
import * as TestBootstrapData from './initialize_response.json';
import * as TestInitializeData from './other_initialize_response.json';
import * as UpdatedInitializeData from './updated_initialize_values.json';
import LocalStorageMock from './LocalStorageMock';

const TID_USER_VALUE = 'statsig-user-object';
const TID_SET_USER_STATE = 'update-via-set-state';
Expand Down Expand Up @@ -84,7 +85,8 @@ function UserTestComponent(props: {
initCompletionCallback: (duration, success, message) => {
initTime = duration;
initCallbacks++;
}
},
overrideStableID: "override-stable-id",
}}
initializeValues={initializeValues}
>
Expand Down Expand Up @@ -113,6 +115,9 @@ describe('StatsigSynchronousProvider', () => {
body: Record<string, unknown>;
}[] = [];

let localStorage = new LocalStorageMock();


async function VerifyInitializeForUserWithRender(userID: string) {
await waitFor(() => screen.getByText(userID));

Expand Down Expand Up @@ -150,10 +155,19 @@ describe('StatsigSynchronousProvider', () => {
}
});

// @ts-ignore
var setTimeout = jest.spyOn(global, 'setTimeout');

beforeEach(() => {
requestsMade = [];
initTime = 0;
initCallbacks = 0;
localStorage = new LocalStorageMock();
setTimeout = jest.spyOn(global, 'setTimeout');
// @ts-ignore
Object.defineProperty(window, 'localStorage', {
value: localStorage,
});

// @ts-ignore
Statsig.instance = null;
Expand All @@ -162,7 +176,10 @@ describe('StatsigSynchronousProvider', () => {
});

it('renders children', async () => {
expect.assertions(6);
expect.assertions(9);

const spyOnSet = jest.spyOn(localStorage, 'setItem');
const spyOnGet = jest.spyOn(localStorage, 'getItem');

const child = await waitFor(() => screen.getByTestId(TID_USER_VALUE));
expect(child).toHaveTextContent('a-user');
Expand All @@ -173,16 +190,21 @@ describe('StatsigSynchronousProvider', () => {
expect(initTime).toBeGreaterThan(0);
expect(initTime).toBeLessThan(100);
expect(initCallbacks).toEqual(1);

expect(requestsMade).toEqual([]);
expect(spyOnSet).toHaveBeenCalledTimes(0);
expect(spyOnGet).toHaveBeenCalledTimes(0);
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 20);
});

it('calls updateUser when user object changes', async () => {
expect.assertions(10);

await waitFor(
() => screen.getByTestId(TID_USER_VALUE),
);
expect(requestsMade.length).toEqual(0);
expect(initCallbacks).toEqual(1);

requestsMade = [];

const gate = screen.getByTestId(TID_GATE_VALUE);
Expand All @@ -191,12 +213,11 @@ describe('StatsigSynchronousProvider', () => {
expect(gate).toHaveTextContent("OFF");
expect(configVal).toHaveTextContent("17");
expect(configPos).toHaveTextContent("default");

await userEvent.click(screen.getByTestId(TID_SET_USER_STATE));

await VerifyInitializeForUserWithRender('a-user-update-via-useState');
expect(initCallbacks).toEqual(1);

expect(gate).toHaveTextContent("ON");
expect(configVal).toHaveTextContent("12");
expect(configPos).toHaveTextContent("jet");
Expand Down

0 comments on commit cab4cc7

Please sign in to comment.