Skip to content

Commit 833d6c9

Browse files
Added test to validate bugfix
1 parent 628b185 commit 833d6c9

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- Added support for targeting rules based on large segments for browsers.
33
- Updated @splitsoftware/splitio package to version 11.0.0 that includes major updates, and updated some transitive dependencies for vulnerability fixes.
44
- Renamed distribution folders from `/lib` to `/cjs` for CommonJS build, and `/es` to `/esm` for EcmaScript Modules build.
5+
- Bugfixing - When the `config` prop is provided, the `SplitFactoryProvider` now makes the SDK factory and client instances available in the context immediately during the initial render, instead of waiting for the first SDK event. This change fixes a bug in the `useTrack` hook, which was not retrieving the client's `track` method during the initial render but rather a no-op function.
56
- BREAKING CHANGES:
67
- Updated error handling: using the library modules without wrapping them in a `SplitFactoryProvider` component will now throw an error instead of logging it, as the modules requires the `SplitContext` to work properly.
78
- Removed the `core.trafficType` configuration option and the `trafficType` parameter from the SDK `client()` method, `useSplitClient`, `useTrack`, and `SplitClient` component. This is because traffic types can no longer be bound to SDK clients in JavaScript SDK v11.0.0, and so the traffic type must be provided as first argument in the `track` method calls.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"check": "npm run check:lint && npm run check:types",
2727
"check:lint": "eslint 'src/**/*.ts*'",
2828
"check:types": "tsc --noEmit",
29-
"test": "jest src",
29+
"test": "jest src --silent",
3030
"test:watch": "npm test -- --watch",
3131
"test:coverage": "jest src --coverage",
3232
"test:debug": "node --inspect node_modules/.bin/jest --runInBand",

src/__tests__/SplitClient.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ describe('SplitClient', () => {
214214
count++;
215215

216216
// side effect in the render phase
217-
if (!(client as IClientWithContext).__getStatus().isReady) {
218-
console.log('emit');
217+
if (!(client as any).__getStatus().isReady) {
219218
(client as any).__emitter__.emit(Event.SDK_READY);
220219
}
221220

src/__tests__/useTrack.test.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React from 'react';
2-
import { render } from '@testing-library/react';
1+
import React, { useEffect } from 'react';
2+
import { render, act } from '@testing-library/react';
33

44
/** Mocks */
5-
import { mockSdk } from './testUtils/mockSplitFactory';
5+
import { mockSdk, Event, getLastInstance } from './testUtils/mockSplitFactory';
66
jest.mock('@splitsoftware/splitio/client', () => {
77
return { SplitFactory: mockSdk() };
88
});
@@ -13,6 +13,7 @@ import { sdkBrowser } from './testUtils/sdkConfigs';
1313
import { SplitFactoryProvider } from '../SplitFactoryProvider';
1414
import { SplitClient } from '../SplitClient';
1515
import { useTrack } from '../useTrack';
16+
import { useSplitClient } from '../useSplitClient';
1617
import { EXCEPTION_NO_SFP } from '../constants';
1718

1819
describe('useTrack', () => {
@@ -22,7 +23,34 @@ describe('useTrack', () => {
2223
const value = 10;
2324
const properties = { prop1: 'prop1' };
2425

25-
test('returns the track method of the client at Split context updated by SplitFactoryProvider.', () => {
26+
test('returns the track method of the client at Split context updated by SplitFactoryProvider (config prop).', () => {
27+
render(
28+
<SplitFactoryProvider config={sdkBrowser} >
29+
{React.createElement(() => {
30+
const clientTrack = useTrack();
31+
32+
const { client } = useSplitClient();
33+
expect(clientTrack).toBe(client!.track);
34+
35+
clientTrack(tt, eventType, value, properties);
36+
37+
useEffect(() => {
38+
clientTrack(tt, eventType, value, properties);
39+
}, [clientTrack]);
40+
return null;
41+
})}
42+
</SplitFactoryProvider>,
43+
);
44+
45+
act(() => getLastInstance(SplitFactory).client().__emitter__.emit(Event.SDK_READY_FROM_CACHE));
46+
act(() => getLastInstance(SplitFactory).client().__emitter__.emit(Event.SDK_READY));
47+
48+
const track = getLastInstance(SplitFactory).client().track as jest.Mock;
49+
expect(track).toBeCalledWith(tt, eventType, value, properties);
50+
expect(track).toBeCalledTimes(4); // 3 from render + 1 from useEffect (`clientTrack` dependency doesn't change)
51+
});
52+
53+
test('returns the track method of the client at Split context updated by SplitFactoryProvider (factory prop).', () => {
2654
const outerFactory = SplitFactory(sdkBrowser);
2755
let clientTrack;
2856
let trackResult;

src/useTrack.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { useSplitClient } from './useSplitClient';
44
const noOpFalse = () => false;
55

66
/**
7-
* 'useTrack' is a hook that returns the track method from a Split client.
8-
* It uses the 'useContext' hook to access the client from the Split context.
7+
* 'useTrack' is a hook that retrieves the track method from a Split client.
8+
* It uses the 'useSplitClient' hook to access the client from the Split context.
9+
* Basically, it is a shortcut for `const track = useSplitClient().client?.track || (() => false);`.
910
*
10-
* @returns A track function bound to a Split client. If the client is not available, the result is a no-op function that returns false.
11+
* @returns A track function of the Split client. If the client is not available, the result is a no-op function that returns false.
1112
*
1213
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#track}
1314
*/

0 commit comments

Comments
 (0)