Skip to content

Commit

Permalink
Merge pull request #10 from statsig-io/singleinitcallback
Browse files Browse the repository at this point in the history
Patch: issue single initCallback
  • Loading branch information
tore-statsig authored Mar 3, 2023
2 parents 28d93b8 + 1598795 commit c698309
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "statsig-react",
"version": "1.23.0",
"version": "1.23.1",
"description": "An SDK for using Statsig Feature Management and Experimentation platform in React clients",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -39,7 +39,7 @@
"typescript": "^4.3.2"
},
"dependencies": {
"statsig-js": "^4.30.0"
"statsig-js": "^4.30.1"
},
"peerDependencies": {
"react": "^16.6.3 || ^17.0.0 || ^18.0.0"
Expand Down
14 changes: 13 additions & 1 deletion src/__tests__/StatsigProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import userEvent from '@testing-library/user-event';
import React, { useState } from 'react';
import StatsigJS, { StatsigUser } from 'statsig-js';
import StatsigProvider from '../StatsigProvider';
import Statsig from '../Statsig';
import useUpdateUser from '../useUpdateUser';

const TID_USER_VALUE = 'statsig-user-object';
Expand All @@ -17,6 +18,8 @@ const TID_PARTIAL_UPDATE_USER_HOOK = 'partial-update-via-hook';

StatsigJS.encodeIntializeCall = false;

let initCallbacks = 0;

function UpdateUserHookTestComponent(props: { userID: string }) {
const updateUser = useUpdateUser();

Expand Down Expand Up @@ -59,6 +62,9 @@ function UserTestComponent(props: {
waitForInitialization={true}
options={{
disableDiagnosticsLogging: true,
initCompletionCallback: () => {
initCallbacks++;
}
}}
>
<div data-testid={TID_USER_VALUE}>{user.userID}</div>
Expand Down Expand Up @@ -100,13 +106,18 @@ describe('StatsigProvider', () => {

beforeEach(() => {
requestsMade = [];
initCallbacks = 0;

// @ts-ignore
Statsig.instance = null;
});

it('renders children', async () => {
render(<UserTestComponent userID="a-user" />);

const child = await waitFor(() => screen.getByTestId(TID_USER_VALUE));
expect(child).toHaveTextContent('a-user');
expect(initCallbacks).toEqual(1);

await VerifyInitializeForUserWithRender('a-user');
});
Expand All @@ -116,12 +127,13 @@ describe('StatsigProvider', () => {
await waitFor(
() => screen.getByTestId(TID_USER_VALUE) && requestsMade.length === 1,
);

expect(initCallbacks).toEqual(1);
requestsMade = [];

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

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

it('updates the user via the useUpdateUser hook', async () => {
Expand Down
25 changes: 16 additions & 9 deletions src/__tests__/StatsigSynchronousProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import userEvent from '@testing-library/user-event';
import React, { useState } from 'react';
import StatsigJS, { StatsigUser } from 'statsig-js';
import StatsigSynchronousProvider from '../StatsigSynchronousProvider';
import Statsig from '../Statsig';
import useUpdateUser from '../useUpdateUser';
import * as TestData from './initialize_response.json';

Expand All @@ -18,6 +19,7 @@ const TID_PARTIAL_UPDATE_USER_HOOK = 'partial-update-via-hook';

StatsigJS.encodeIntializeCall = false;
let initTime = 0;
let initCallbacks = 0;

function UpdateUserHookTestComponent(props: { userID: string }) {
const updateUser = useUpdateUser();
Expand Down Expand Up @@ -61,7 +63,8 @@ function UserTestComponent(props: {
options={{
disableDiagnosticsLogging: true,
initCompletionCallback: (duration, success, message) => {
initTime = duration
initTime = duration;
initCallbacks++;
}
}}
initializeValues={TestData}
Expand All @@ -80,7 +83,7 @@ function UserTestComponent(props: {
);
}

describe('StatsigProvider', () => {
describe('StatsigSynchronousProvider', () => {
let requestsMade: {
url: RequestInfo | URL;
body: Record<string, unknown>;
Expand All @@ -106,36 +109,41 @@ describe('StatsigProvider', () => {
beforeEach(() => {
requestsMade = [];
initTime = 0;
});
initCallbacks = 0;

// @ts-ignore
Statsig.instance = null;

it('renders children', async () => {
expect.assertions(4);
render(<UserTestComponent userID="a-user" />);
});

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

const child = await waitFor(() => screen.getByTestId(TID_USER_VALUE));
expect(child).toHaveTextContent('a-user');

expect(initTime).toBeGreaterThan(0);
expect(initTime).toBeLessThan(100);
expect(initCallbacks).toEqual(1);

expect(requestsMade).toEqual([]);
});

it('calls updateUser when user object changes', async () => {
render(<UserTestComponent userID="a-user" />);
await waitFor(
() => screen.getByTestId(TID_USER_VALUE) && requestsMade.length === 1,
);

expect(initCallbacks).toEqual(1);
requestsMade = [];

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

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

it('updates the user via the useUpdateUser hook', async () => {
render(<UserTestComponent userID="a-user" />);
await waitFor(
() => screen.getByTestId(TID_USER_VALUE) && requestsMade.length === 1,
);
Expand All @@ -150,7 +158,6 @@ describe('StatsigProvider', () => {
});

it('partially updates the user via the useUpdateUser hook', async () => {
render(<UserTestComponent userID="a-user" />);
await waitFor(
() => screen.getByTestId(TID_USER_VALUE) && requestsMade.length === 1,
);
Expand Down

0 comments on commit c698309

Please sign in to comment.