Skip to content

Commit

Permalink
fix(insights): add Algolia agent on subscribe (#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan committed Dec 20, 2022
1 parent d7d2842 commit 60f8ae4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Expand Up @@ -64,6 +64,21 @@ describe('createAlgoliaInsightsPlugin', () => {
);
});

test('sets a user agent on the Insights client on subscribe', () => {
const insightsClient = jest.fn();
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });

expect(insightsClient).not.toHaveBeenCalled();

createPlayground(createAutocomplete, { plugins: [insightsPlugin] });

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'addAlgoliaAgent',
'insights-plugin'
);
});

describe('onItemsChange', () => {
test('sends a `viewedObjectIDs` event by default', async () => {
const insightsClient = jest.fn();
Expand Down Expand Up @@ -180,7 +195,10 @@ describe('createAlgoliaInsightsPlugin', () => {
await runAllMicroTasks();
jest.runAllTimers();

expect(insightsClient).not.toHaveBeenCalled();
expect(insightsClient).not.toHaveBeenCalledWith(
'viewedObjectIDs',
expect.any(Object)
);
});

test('debounces calls', async () => {
Expand Down Expand Up @@ -271,7 +289,10 @@ describe('createAlgoliaInsightsPlugin', () => {
await runAllMicroTasks();
jest.runAllTimers();

expect(insightsClient).not.toHaveBeenCalled();
expect(insightsClient).not.toHaveBeenCalledWith(
'viewedObjectIDs',
expect.any(Object)
);
});

test('does not send an event when there are no results', async () => {
Expand All @@ -296,7 +317,10 @@ describe('createAlgoliaInsightsPlugin', () => {
await runAllMicroTasks();
jest.runAllTimers();

expect(insightsClient).not.toHaveBeenCalled();
expect(insightsClient).not.toHaveBeenCalledWith(
'viewedObjectIDs',
expect.any(Object)
);
});
});

Expand Down Expand Up @@ -436,7 +460,10 @@ describe('createAlgoliaInsightsPlugin', () => {

await runAllMicroTasks();

expect(insightsClient).not.toHaveBeenCalled();
expect(insightsClient).not.toHaveBeenCalledWith(
'clickedObjectIDsAfterSearch',
expect.any(Object)
);
});

test('does not send an event with non-Algolia Insights hits', async () => {
Expand Down Expand Up @@ -464,7 +491,10 @@ describe('createAlgoliaInsightsPlugin', () => {

await runAllMicroTasks();

expect(insightsClient).not.toHaveBeenCalled();
expect(insightsClient).not.toHaveBeenCalledWith(
'viewedObjectIDs',
expect.any(Object)
);
});
});

Expand Down Expand Up @@ -493,6 +523,9 @@ describe('createAlgoliaInsightsPlugin', () => {
},
});

// The client is always called once with `addAlgoliaAgent` on `subscribe`
insightsClient.mockClear();

inputElement.focus();

await runAllMicroTasks();
Expand Down
Expand Up @@ -89,7 +89,6 @@ export function createAlgoliaInsightsPlugin(
onSelect: onSelectEvent,
onActive: onActiveEvent,
} = getOptions(options);

const insights = createSearchInsightsApi(insightsClient);
const previousItems = createRef<AlgoliaInsightsHit[]>([]);

Expand Down Expand Up @@ -123,6 +122,8 @@ export function createAlgoliaInsightsPlugin(
return {
name: 'aa.algoliaInsightsPlugin',
subscribe({ setContext, onSelect, onActive }) {
insightsClient('addAlgoliaAgent', 'insights-plugin');

setContext({ algoliaInsightsPlugin: { insights } });

onSelect(({ item, state, event }) => {
Expand Down

0 comments on commit 60f8ae4

Please sign in to comment.