From 1598795658f211d15e407fbbb5556677f93f967d Mon Sep 17 00:00:00 2001
From: tore-statsig <74584483+tore-statsig@users.noreply.github.com>
Date: Thu, 2 Mar 2023 09:54:54 -0800
Subject: [PATCH] Patch: issue single initCallback
---
package-lock.json | 18 ++++++-------
package.json | 4 +--
src/__tests__/StatsigProvider.test.tsx | 14 ++++++++++-
.../StatsigSynchronousProvider.test.tsx | 25 ++++++++++++-------
4 files changed, 40 insertions(+), 21 deletions(-)
diff --git a/package-lock.json b/package-lock.json
index 3459011..bf87666 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,15 +1,15 @@
{
"name": "statsig-react",
- "version": "1.23.0",
+ "version": "1.23.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "statsig-react",
- "version": "1.23.0",
+ "version": "1.23.1",
"license": "ISC",
"dependencies": {
- "statsig-js": "^4.30.0"
+ "statsig-js": "^4.30.1"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.14.1",
@@ -4242,9 +4242,9 @@
}
},
"node_modules/statsig-js": {
- "version": "4.30.0",
- "resolved": "https://registry.npmjs.org/statsig-js/-/statsig-js-4.30.0.tgz",
- "integrity": "sha512-UcoqJZGUMQDvpAAOXW/ZXyKgXvV8M5xistHmfX7dHsS5rvJSUz9foRq4lQ8EsZUDT6yV9cbn9zY+werAW8obcw==",
+ "version": "4.30.1",
+ "resolved": "https://registry.npmjs.org/statsig-js/-/statsig-js-4.30.1.tgz",
+ "integrity": "sha512-nm5i9t6qYg4GZdzYjOftoOs2RTAWdHjEyqP5+SQsQGNBl4SpVpnS5qfbbN1U4v0PmoRGzXMxIOTZ1RSrCtJOxw==",
"dependencies": {
"js-sha256": "^0.9.0",
"uuid": "^8.3.2"
@@ -8053,9 +8053,9 @@
}
},
"statsig-js": {
- "version": "4.30.0",
- "resolved": "https://registry.npmjs.org/statsig-js/-/statsig-js-4.30.0.tgz",
- "integrity": "sha512-UcoqJZGUMQDvpAAOXW/ZXyKgXvV8M5xistHmfX7dHsS5rvJSUz9foRq4lQ8EsZUDT6yV9cbn9zY+werAW8obcw==",
+ "version": "4.30.1",
+ "resolved": "https://registry.npmjs.org/statsig-js/-/statsig-js-4.30.1.tgz",
+ "integrity": "sha512-nm5i9t6qYg4GZdzYjOftoOs2RTAWdHjEyqP5+SQsQGNBl4SpVpnS5qfbbN1U4v0PmoRGzXMxIOTZ1RSrCtJOxw==",
"requires": {
"js-sha256": "^0.9.0",
"uuid": "^8.3.2"
diff --git a/package.json b/package.json
index c3e280b..2de9c36 100644
--- a/package.json
+++ b/package.json
@@ -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",
@@ -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"
diff --git a/src/__tests__/StatsigProvider.test.tsx b/src/__tests__/StatsigProvider.test.tsx
index f223af6..876aa9b 100644
--- a/src/__tests__/StatsigProvider.test.tsx
+++ b/src/__tests__/StatsigProvider.test.tsx
@@ -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';
@@ -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();
@@ -59,6 +62,9 @@ function UserTestComponent(props: {
waitForInitialization={true}
options={{
disableDiagnosticsLogging: true,
+ initCompletionCallback: () => {
+ initCallbacks++;
+ }
}}
>
{user.userID}
@@ -100,6 +106,10 @@ describe('StatsigProvider', () => {
beforeEach(() => {
requestsMade = [];
+ initCallbacks = 0;
+
+ // @ts-ignore
+ Statsig.instance = null;
});
it('renders children', async () => {
@@ -107,6 +117,7 @@ describe('StatsigProvider', () => {
const child = await waitFor(() => screen.getByTestId(TID_USER_VALUE));
expect(child).toHaveTextContent('a-user');
+ expect(initCallbacks).toEqual(1);
await VerifyInitializeForUserWithRender('a-user');
});
@@ -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 () => {
diff --git a/src/__tests__/StatsigSynchronousProvider.test.tsx b/src/__tests__/StatsigSynchronousProvider.test.tsx
index 372950b..c6e0ca4 100644
--- a/src/__tests__/StatsigSynchronousProvider.test.tsx
+++ b/src/__tests__/StatsigSynchronousProvider.test.tsx
@@ -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';
@@ -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();
@@ -61,7 +63,8 @@ function UserTestComponent(props: {
options={{
disableDiagnosticsLogging: true,
initCompletionCallback: (duration, success, message) => {
- initTime = duration
+ initTime = duration;
+ initCallbacks++;
}
}}
initializeValues={TestData}
@@ -80,7 +83,7 @@ function UserTestComponent(props: {
);
}
-describe('StatsigProvider', () => {
+describe('StatsigSynchronousProvider', () => {
let requestsMade: {
url: RequestInfo | URL;
body: Record;
@@ -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();
+ });
+ 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();
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();
await waitFor(
() => screen.getByTestId(TID_USER_VALUE) && requestsMade.length === 1,
);
@@ -150,7 +158,6 @@ describe('StatsigProvider', () => {
});
it('partially updates the user via the useUpdateUser hook', async () => {
- render();
await waitFor(
() => screen.getByTestId(TID_USER_VALUE) && requestsMade.length === 1,
);