Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(insights): add Algolia agent on subscribe #1058

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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