Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab committed Apr 13, 2023
1 parent c8fdd1f commit e446d48
Show file tree
Hide file tree
Showing 2 changed files with 362 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,55 @@ describe('createAlgoliaInsightsPlugin', () => {
});
});

test('sends a `viewedObjectIDs` event with additional parameters if client supports it', async () => {
const insightsClient = jest.fn();
// @ts-ignore
insightsClient.version = '2.4.0';
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });

const { inputElement } = createPlayground(createAutocomplete, {
plugins: [insightsPlugin],
defaultActiveItemId: 0,
openOnFocus: true,
getSources() {
return [
createSource({
getItems: () => [
{
label: '1',
objectID: '1',
__autocomplete_algoliaCredentials: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
},
__autocomplete_indexName: 'index1',
__autocomplete_queryID: 'queryID1',
},
],
}),
];
},
});

inputElement.focus();

await runAllMicroTasks();
jest.runAllTimers();

expect(insightsClient).toHaveBeenCalledWith(
'viewedObjectIDs',
expect.objectContaining({
objectIDs: ['1'],
}),
{
headers: {
'X-Algolia-Application-Id': 'algoliaAppId',
'X-Algolia-API-Key': 'algoliaApiKey',
},
}
);
});

test('sends as many `viewedObjectIDs` events as there are compatible sources', async () => {
const insightsClient = jest.fn();
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });
Expand Down Expand Up @@ -649,6 +698,58 @@ describe('createAlgoliaInsightsPlugin', () => {
);
});

test('sends a `clickedObjectIDsAfterSearch` event with additional parameters if client supports it', async () => {
const insightsClient = jest.fn();
// @ts-ignore
insightsClient.version = '2.4.0';
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });

const { inputElement } = createPlayground(createAutocomplete, {
plugins: [insightsPlugin],
defaultActiveItemId: 0,
openOnFocus: true,
getSources() {
return [
createSource({
getItems: () => [
{
label: '1',
objectID: '1',
__autocomplete_algoliaCredentials: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
},
__autocomplete_indexName: 'index1',
__autocomplete_queryID: 'queryID1',
},
],
}),
];
},
});

inputElement.focus();

await runAllMicroTasks();

userEvent.type(inputElement, '{enter}');

await runAllMicroTasks();

expect(insightsClient).toHaveBeenCalledWith(
'clickedObjectIDsAfterSearch',
expect.objectContaining({
objectIDs: ['1'],
}),
{
headers: {
'X-Algolia-Application-Id': 'algoliaAppId',
'X-Algolia-API-Key': 'algoliaApiKey',
},
}
);
});

test('sends a custom event', async () => {
const insightsClient = jest.fn();
const insightsPlugin = createAlgoliaInsightsPlugin({
Expand Down Expand Up @@ -854,7 +955,14 @@ describe('createAlgoliaInsightsPlugin', () => {
expect(track).toHaveBeenCalledWith('Product Browsed from Autocomplete', {
eventName: 'Item Active',
index: 'index1',
objectIDs: ['1'],
items: [
expect.objectContaining({
label: '1',
objectID: '1',
__autocomplete_indexName: 'index1',
__autocomplete_queryID: 'queryID1',
}),
],
positions: [0],
queryID: 'queryID1',
algoliaSource: ['autocomplete'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,265 @@
import { createSearchInsightsApi } from '../createSearchInsightsApi';
import { AlgoliaInsightsHit } from '../types';

function getAlgoliaItems(count: number): AlgoliaInsightsHit[] {
return Array.from({ length: count }).map((_, i) => ({
objectID: i.toString(),
__autocomplete_algoliaCredentials: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
},
__autocomplete_indexName: 'index1',
__autocomplete_queryID: 'queryID',
}));
}

describe('createSearchInsightsApi', () => {
test('viewedObjectIDs splits large payloads into multiple chunks', () => {
describe('with supported client versions', () => {
const insightsClient = jest.fn();
// @ts-ignore
insightsClient.version = '2.4.0';
const insightsApi = createSearchInsightsApi(insightsClient);

beforeEach(() => {
insightsClient.mockReset();
});

test('clickedObjectIDsAfterSearch() sends events with additional parameters', () => {
const items = getAlgoliaItems(1);

insightsApi.clickedObjectIDsAfterSearch({
eventName: 'Items Clicked',
index: 'index1',
items,
positions: [1],
queryID: 'queryID',
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'clickedObjectIDsAfterSearch',
expect.objectContaining({
objectIDs: items.map(({ objectID }) => objectID),
}),
{
headers: {
'X-Algolia-Application-Id': 'algoliaAppId',
'X-Algolia-API-Key': 'algoliaApiKey',
},
}
);
});

test('clickedObjectIDs() sends events with additional parameters', () => {
const items = getAlgoliaItems(1);

insightsApi.clickedObjectIDs({
eventName: 'Items Clicked',
index: 'index1',
items,
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'clickedObjectIDs',
expect.objectContaining({
objectIDs: items.map(({ objectID }) => objectID),
}),
{
headers: {
'X-Algolia-Application-Id': 'algoliaAppId',
'X-Algolia-API-Key': 'algoliaApiKey',
},
}
);
});

test('convertedObjectIDsAfterSearch() sends events with additional parameters', () => {
const items = getAlgoliaItems(1);

insightsApi.convertedObjectIDsAfterSearch({
eventName: 'Items Added to cart',
index: 'index1',
items,
queryID: 'queryID',
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'convertedObjectIDsAfterSearch',
expect.objectContaining({
objectIDs: items.map(({ objectID }) => objectID),
}),
{
headers: {
'X-Algolia-Application-Id': 'algoliaAppId',
'X-Algolia-API-Key': 'algoliaApiKey',
},
}
);
});

test('convertedObjectIDs() sends events with additional parameters', () => {
const items = getAlgoliaItems(1);

insightsApi.convertedObjectIDs({
eventName: 'Items Added to cart',
index: 'index1',
items,
userToken: 'userToken',
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'convertedObjectIDs',
expect.objectContaining({
objectIDs: items.map(({ objectID }) => objectID),
}),
{
headers: {
'X-Algolia-Application-Id': 'algoliaAppId',
'X-Algolia-API-Key': 'algoliaApiKey',
},
}
);
});

test('viewedObjectIDs() sends events with additional parameters', () => {
const items = getAlgoliaItems(10);

insightsApi.viewedObjectIDs({
eventName: 'Items Viewed',
index: 'index1',
items,
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'viewedObjectIDs',
expect.objectContaining({
objectIDs: items.map(({ objectID }) => objectID),
}),
{
headers: {
'X-Algolia-Application-Id': 'algoliaAppId',
'X-Algolia-API-Key': 'algoliaApiKey',
},
}
);
});
});

describe('with unsupported client versions', () => {
const insightsClient = jest.fn();
const insightsApi = createSearchInsightsApi(insightsClient);

beforeEach(() => {
insightsClient.mockReset();
});

test('clickedObjectIDsAfterSearch() sends events without additional parameters', () => {
const items = getAlgoliaItems(1);

insightsApi.clickedObjectIDsAfterSearch({
eventName: 'Items Clicked',
index: 'index1',
items,
positions: [1],
queryID: 'queryID',
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'clickedObjectIDsAfterSearch',
expect.objectContaining({
objectIDs: items.map(({ objectID }) => objectID),
})
);
});

test('clickedObjectIDs() sends events without additional parameters', () => {
const items = getAlgoliaItems(1);

insightsApi.clickedObjectIDs({
eventName: 'Items Clicked',
index: 'index1',
items,
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'clickedObjectIDs',
expect.objectContaining({
objectIDs: items.map(({ objectID }) => objectID),
})
);
});

test('convertedObjectIDsAfterSearch() sends events without additional parameters', () => {
const items = getAlgoliaItems(1);

insightsApi.convertedObjectIDsAfterSearch({
eventName: 'Items Added to cart',
index: 'index1',
items,
queryID: 'queryID',
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'convertedObjectIDsAfterSearch',
expect.objectContaining({
objectIDs: items.map(({ objectID }) => objectID),
})
);
});

test('convertedObjectIDs() sends events without additional parameters', () => {
const items = getAlgoliaItems(1);

insightsApi.convertedObjectIDs({
eventName: 'Items Added to cart',
index: 'index1',
items,
userToken: 'userToken',
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'convertedObjectIDs',
expect.objectContaining({
objectIDs: items.map(({ objectID }) => objectID),
})
);
});

test('viewedObjectIDs() sends events without additional parameters', () => {
const items = getAlgoliaItems(10);

insightsApi.viewedObjectIDs({
eventName: 'Items Viewed',
index: 'index1',
items,
});

expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'viewedObjectIDs',
expect.objectContaining({
objectIDs: items.map(({ objectID }) => objectID),
})
);
});
});

test('viewedObjectIDs() splits large payloads into multiple chunks', () => {
const insightsClient = jest.fn();
const insightsApi = createSearchInsightsApi(insightsClient);

insightsApi.viewedObjectIDs({
eventName: 'Items Viewed',
index: 'index1',
objectIDs: Array.from({ length: 50 }, (_, i) => `${i}`),
items: getAlgoliaItems(50),
});

expect(insightsClient).toHaveBeenCalledTimes(3);
Expand Down

0 comments on commit e446d48

Please sign in to comment.