Skip to content

Commit 05d05a9

Browse files
author
Eunjae Lee
authoredApr 21, 2021
fix(insights): use getUserToken method instead of _get (#4744)
* fix(insights): use getUserToken method instead of _get * fix test cases
1 parent 179591c commit 05d05a9

File tree

4 files changed

+15
-30
lines changed

4 files changed

+15
-30
lines changed
 

‎src/middlewares/__tests__/createInsightsMiddleware.ts

+2-21
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,6 @@ describe('insights', () => {
8686
});
8787

8888
describe('initialize', () => {
89-
it('initialize insightsClient', () => {
90-
const { insightsClient, instantSearchInstance } = createTestEnvironment();
91-
expect.assertions(3);
92-
93-
insightsClient('setUserToken', 'abc');
94-
createInsightsMiddleware({
95-
insightsClient,
96-
})({ instantSearchInstance });
97-
insightsClient('_get', '_hasCredentials', hasCredentials => {
98-
expect(hasCredentials).toBe(true);
99-
});
100-
insightsClient('_get', '_appId', appId => {
101-
expect(appId).toBe('myAppId');
102-
});
103-
insightsClient('_get', '_apiKey', apiKey => {
104-
expect(apiKey).toBe('myApiKey');
105-
});
106-
});
107-
10889
it('passes initParams to insightsClient', () => {
10990
const { insightsClient, instantSearchInstance } = createTestEnvironment();
11091
createInsightsMiddleware({
@@ -299,7 +280,7 @@ describe('insights', () => {
299280
insightsClient('setUserToken', 'token-from-queue');
300281
libraryLoadedAndProcessQueue();
301282

302-
insightsClient('_get', '_userToken', userToken => {
283+
insightsClient('getUserToken', null, (_error, userToken) => {
303284
expect(userToken).toEqual('token-from-queue');
304285
});
305286

@@ -322,7 +303,7 @@ describe('insights', () => {
322303
insightsClient('init', { appId: 'myAppId', apiKey: 'myApiKey' });
323304
insightsClient('setUserToken', 'token-from-queue');
324305

325-
insightsClient('_get', '_userToken', userToken => {
306+
insightsClient('getUserToken', null, (_error, userToken) => {
326307
expect(userToken).toEqual('token-from-queue');
327308
});
328309

‎src/middlewares/createInsightsMiddleware.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const createInsightsMiddleware: CreateInsightsMiddleware = props => {
7575
([method]) => method === 'setUserToken'
7676
) || [];
7777
}
78-
insightsClient('_get', '_userToken', (userToken: string) => {
78+
insightsClient('getUserToken', null, (_error: any, userToken: string) => {
7979
// If user has called `aa('setUserToken', 'my-user-token')` before creating
8080
// the `insights` middleware, we store them temporarily and
8181
// set it later on.

‎src/types/insights.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ export type InsightsOnUserTokenChange = (
2828
options?: { immediate?: boolean }
2929
) => void;
3030

31-
export type InsightsGet = (
32-
method: '_get',
33-
key: string,
34-
callback: (value: any) => void
31+
export type InsightsGetUserToken = (
32+
method: 'getUserToken',
33+
options?: any,
34+
callback?: (error: any, userToken: string) => void
3535
) => void;
3636

3737
export type InsightsInit = (
@@ -44,7 +44,7 @@ export type InsightsInit = (
4444

4545
export type InsightsClient = InsightsSendEvent &
4646
InsightsOnUserTokenChange &
47-
InsightsGet &
47+
InsightsGetUserToken &
4848
InsightsInit &
4949
InsightsSetUserToken & {
5050
queue?: Array<[string, any]>;

‎test/mock/createInsightsClient.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ export const ANONYMOUS_TOKEN = 'anonymous-user-id-1';
33
export type AlgoliaAnalytics = {
44
setUserToken(userToken: string): void;
55
init({ appId, apiKey }): void;
6-
_get(key: string, callback: (value: any) => void): void;
6+
getUserToken(
7+
options: any,
8+
callback: (error: any, userToken: string) => void
9+
): void;
710
onUserTokenChange(
811
callback: (value: string) => void,
912
options?: { immediate?: boolean }
@@ -30,7 +33,8 @@ export function createAlgoliaAnalytics(): AlgoliaAnalytics {
3033
setValues({ _hasCredentials: true, _appId: appId, _apiKey: apiKey });
3134
setUserToken(ANONYMOUS_TOKEN);
3235
};
33-
const _get = (key, callback) => callback(values[key]);
36+
const getUserToken = (_options, callback) =>
37+
callback(null, values._userToken);
3438
const onUserTokenChange = (callback, { immediate = false } = {}) => {
3539
userTokenCallback = callback;
3640
if (immediate) {
@@ -42,7 +46,7 @@ export function createAlgoliaAnalytics(): AlgoliaAnalytics {
4246
return {
4347
setUserToken,
4448
init,
45-
_get,
49+
getUserToken,
4650
onUserTokenChange,
4751
viewedObjectIDs,
4852
};

0 commit comments

Comments
 (0)
Please sign in to comment.