Skip to content

Commit 3f47d9e

Browse files
authored
[CI-4365] Add Option to Pass Logged in User Id and other parameters to the JS-Client (#147)
* add option to pass ui to js-client * lint
1 parent dc1bfb7 commit 3f47d9e

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint-disable react/jsx-filename-extension */
2+
import useCioPlpProvider from '../../../src/hooks/useCioPlpProvider';
3+
import { renderHookServerSide } from '../../test-utils.server';
4+
5+
describe('Testing Hook on the server: useCioPlpProvider', () => {
6+
beforeEach(() => {
7+
const spy = jest.spyOn(console, 'error');
8+
spy.mockImplementation(() => {});
9+
});
10+
11+
afterAll(() => {
12+
jest.resetAllMocks();
13+
});
14+
15+
it('Should return client when custom client is provided', () => {
16+
const mockClient = { tracker: () => {} };
17+
const { result } = renderHookServerSide(({ cioClient }) => useCioPlpProvider({ cioClient }), {
18+
initialProps: { cioClient: mockClient },
19+
});
20+
21+
expect(result.cioClient).toBe(mockClient);
22+
});
23+
24+
it('Should return when clientOptions are provided', () => {
25+
const key = 'test-key';
26+
const clientOptions = {
27+
serviceUrl: 'https://special.cnstrc.com',
28+
quizzesServiceUrl: 'https://quizzes.cnstrc.com',
29+
sessionId: 1,
30+
clientId: 'id-1',
31+
userId: 'ui-1',
32+
segments: ['segment-1', 'segment-2'],
33+
testCells: { test: 'cell' },
34+
fetch: 'mock-fetch-fn',
35+
trackingSendDelay: 400,
36+
sendReferrerWithTrackingEvents: true,
37+
beaconMode: false,
38+
eventDispatcher: undefined,
39+
networkParameters: { timeout: 1000 },
40+
};
41+
42+
const { result } = renderHookServerSide(
43+
({ apiKey, options }) => useCioPlpProvider({ apiKey, options }),
44+
{
45+
initialProps: { apiKey: key, options: clientOptions },
46+
},
47+
);
48+
49+
expect(result.cioClient).toBe(null);
50+
});
51+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { renderHook, waitFor } from '@testing-library/react';
2+
import useCioPlpProvider from '../../../src/hooks/useCioPlpProvider';
3+
4+
describe('Testing Hook: useCioPlpProvider', () => {
5+
it('Should return client when custom client is provided', () => {
6+
const mockClient = { tracker: () => {} };
7+
const { result } = renderHook(({ cioClient }) => useCioPlpProvider({ cioClient }), {
8+
initialProps: { cioClient: mockClient },
9+
});
10+
11+
expect(result.current.cioClient).toBe(mockClient);
12+
});
13+
14+
test('Should pass custom client options if set', () => {
15+
const key = 'test-key';
16+
const clientOptions = {
17+
serviceUrl: 'https://special.cnstrc.com',
18+
quizzesServiceUrl: 'https://quizzes.cnstrc.com',
19+
sessionId: 1,
20+
clientId: 'id-1',
21+
userId: 'ui-1',
22+
segments: ['segment-1', 'segment-2'],
23+
testCells: { test: 'cell' },
24+
fetch: 'mock-fetch-fn',
25+
trackingSendDelay: 400,
26+
sendReferrerWithTrackingEvents: true,
27+
beaconMode: false,
28+
eventDispatcher: undefined,
29+
networkParameters: { timeout: 1000 },
30+
};
31+
32+
const { result } = renderHook(
33+
({ apiKey, options }) => useCioPlpProvider({ apiKey, cioClientOptions: options }),
34+
{
35+
initialProps: { apiKey: key, options: clientOptions },
36+
},
37+
);
38+
39+
const { cioClientOptions } = result.current;
40+
expect(cioClientOptions).toEqual({
41+
...clientOptions,
42+
});
43+
});
44+
45+
test('Should update client if setCioClientOptions is called', async () => {
46+
const key = 'test-key';
47+
48+
const { result } = renderHook(({ apiKey }) => useCioPlpProvider({ apiKey }), {
49+
initialProps: { apiKey: key },
50+
});
51+
52+
let firstRun = true;
53+
await waitFor(() => {
54+
const { cioClientOptions, setCioClientOptions, cioClient } = result.current;
55+
56+
if (firstRun) {
57+
setCioClientOptions({ userId: 'test' });
58+
firstRun = false;
59+
}
60+
61+
expect(cioClientOptions.userId).toEqual('test');
62+
expect(cioClient.options.userId).toEqual('test');
63+
});
64+
});
65+
});

src/hooks/useCioPlpProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ export default function useCioPlpProvider(
1616
urlHelpers,
1717
staticRequestConfigs = {},
1818
cioClient: customCioClient,
19+
cioClientOptions: customCioClientOptions = {},
1920
} = props;
2021

21-
const [cioClientOptions, setCioClientOptions] = useState({});
22+
const [cioClientOptions, setCioClientOptions] = useState(customCioClientOptions);
2223
const cioClient = useCioClient({ apiKey, cioClient: customCioClient, options: cioClientOptions });
2324

2425
const contextValue = useMemo(

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ export interface PlpBrowseResponse extends PlpSearchResponse {}
211211
export interface CioPlpProviderProps {
212212
apiKey: string;
213213
cioClient?: Nullable<ConstructorIOClient>;
214+
cioClientOptions?: Omit<ConstructorClientOptions, 'apiKey' | 'version'>;
214215
formatters?: Partial<Formatters>;
215216
callbacks?: Partial<Callbacks>;
216217
itemFieldGetters?: Partial<ItemFieldGetters>;

0 commit comments

Comments
 (0)