Skip to content

Commit c9d06fd

Browse files
FabienMotteHaroenv
authored andcommitted
feat(autocomplete-core): add insights option to enable the Insights plugin (#1121)
1 parent efb27ce commit c9d06fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+444
-196
lines changed

bundlesize.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"files": [
33
{
44
"path": "packages/autocomplete-core/dist/umd/index.production.js",
5-
"maxSize": "6.5 kB"
5+
"maxSize": "8.5 kB"
66
},
77
{
88
"path": "packages/autocomplete-js/dist/umd/index.production.js",
9-
"maxSize": "19 kB"
9+
"maxSize": "20.75 kB"
1010
},
1111
{
1212
"path": "packages/autocomplete-preset-algolia/dist/umd/index.production.js",

jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module.exports = {
1515
__TEST__: true,
1616
},
1717
moduleNameMapper: {
18+
'^@algolia/autocomplete-shared/dist/esm/(.*)$':
19+
'<rootDir>/packages/autocomplete-shared/src/$1',
1820
'^@algolia/autocomplete-(.*)$': '<rootDir>/packages/autocomplete-$1/src/',
1921
},
2022
};

packages/autocomplete-core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"watch": "watch \"yarn on:change\" --ignoreDirectoryPattern \"/dist/\""
3232
},
3333
"dependencies": {
34-
"@algolia/autocomplete-shared": "1.8.3"
34+
"@algolia/autocomplete-shared": "1.8.3",
35+
"@algolia/autocomplete-plugin-algolia-insights": "1.8.3"
3536
},
3637
"devDependencies": {
3738
"@algolia/autocomplete-preset-algolia": "1.8.3",

packages/autocomplete-core/src/__tests__/createAutocomplete.test.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights';
2+
13
import { createAutocomplete } from '../createAutocomplete';
24

35
describe('createAutocomplete', () => {
@@ -43,4 +45,126 @@ describe('createAutocomplete', () => {
4345
setStatus: autocomplete.setStatus,
4446
});
4547
});
48+
49+
describe('Insights plugin', () => {
50+
test('does not add Insights plugin by default', () => {
51+
const onStateChange = jest.fn();
52+
53+
createAutocomplete({ onStateChange });
54+
55+
expect(onStateChange).toHaveBeenCalledTimes(0);
56+
});
57+
58+
test('`insights: true` adds only one Insights plugin', () => {
59+
const onStateChange = jest.fn();
60+
61+
createAutocomplete({
62+
onStateChange,
63+
insights: true,
64+
});
65+
66+
expect(onStateChange).toHaveBeenCalledTimes(1);
67+
expect(onStateChange).toHaveBeenCalledWith(
68+
expect.objectContaining({
69+
state: expect.objectContaining({
70+
context: expect.objectContaining({
71+
algoliaInsightsPlugin: expect.objectContaining({
72+
insights: expect.objectContaining({
73+
init: expect.any(Function),
74+
setUserToken: expect.any(Function),
75+
clickedObjectIDsAfterSearch: expect.any(Function),
76+
clickedObjectIDs: expect.any(Function),
77+
clickedFilters: expect.any(Function),
78+
convertedObjectIDsAfterSearch: expect.any(Function),
79+
convertedObjectIDs: expect.any(Function),
80+
convertedFilters: expect.any(Function),
81+
viewedObjectIDs: expect.any(Function),
82+
viewedFilters: expect.any(Function),
83+
}),
84+
}),
85+
}),
86+
}),
87+
})
88+
);
89+
});
90+
91+
test('`insights` with options still creates one plugin only', () => {
92+
const onStateChange = jest.fn();
93+
const insightsClient = jest.fn();
94+
95+
createAutocomplete({
96+
onStateChange,
97+
insights: { insightsClient },
98+
});
99+
100+
expect(onStateChange).toHaveBeenCalledTimes(1);
101+
expect(onStateChange).toHaveBeenCalledWith(
102+
expect.objectContaining({
103+
state: expect.objectContaining({
104+
context: expect.objectContaining({
105+
algoliaInsightsPlugin: expect.objectContaining({
106+
insights: expect.objectContaining({
107+
init: expect.any(Function),
108+
setUserToken: expect.any(Function),
109+
clickedObjectIDsAfterSearch: expect.any(Function),
110+
clickedObjectIDs: expect.any(Function),
111+
clickedFilters: expect.any(Function),
112+
convertedObjectIDsAfterSearch: expect.any(Function),
113+
convertedObjectIDs: expect.any(Function),
114+
convertedFilters: expect.any(Function),
115+
viewedObjectIDs: expect.any(Function),
116+
viewedFilters: expect.any(Function),
117+
}),
118+
}),
119+
}),
120+
}),
121+
})
122+
);
123+
});
124+
125+
test('`insights` with options passes options to plugin', () => {
126+
const insightsClient = jest.fn();
127+
128+
createAutocomplete({
129+
insights: { insightsClient },
130+
});
131+
132+
expect(insightsClient).toHaveBeenCalledTimes(1);
133+
expect(insightsClient).toHaveBeenCalledWith(
134+
'addAlgoliaAgent',
135+
'insights-plugin'
136+
);
137+
});
138+
139+
test('`insights: false` disables default Insights plugin', () => {
140+
const onStateChange = jest.fn();
141+
142+
createAutocomplete({
143+
insights: false,
144+
onStateChange,
145+
});
146+
147+
expect(onStateChange).toHaveBeenCalledTimes(0);
148+
});
149+
150+
test("users' Insights plugin overrides the default one when `insights: true`", () => {
151+
const defaultInsightsClient = jest.fn();
152+
const userInsightsClient = jest.fn();
153+
const insightsPlugin = createAlgoliaInsightsPlugin({
154+
insightsClient: userInsightsClient,
155+
});
156+
157+
createAutocomplete({
158+
insights: { insightsClient: defaultInsightsClient },
159+
plugins: [insightsPlugin],
160+
});
161+
162+
expect(defaultInsightsClient).toHaveBeenCalledTimes(0);
163+
expect(userInsightsClient).toHaveBeenCalledTimes(1);
164+
expect(userInsightsClient).toHaveBeenCalledWith(
165+
'addAlgoliaAgent',
166+
'insights-plugin'
167+
);
168+
});
169+
});
46170
});

packages/autocomplete-core/src/__tests__/openOnFocus.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import {
66
runAllMicroTasks,
77
} from '../../../../test/utils';
88
import { createAutocomplete } from '../createAutocomplete';
9-
import { BaseItem } from '../types/AutocompleteApi';
10-
import { AutocompleteOptions } from '../types/AutocompleteOptions';
9+
import { BaseItem, AutocompleteOptions } from '../types';
1110

1211
describe('openOnFocus', () => {
1312
function setupTest<TItem extends BaseItem>(

packages/autocomplete-core/src/createAutocomplete.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights';
2+
13
import { checkOptions } from './checkOptions';
24
import { createStore } from './createStore';
35
import { getAutocompleteSetters } from './getAutocompleteSetters';
@@ -66,6 +68,15 @@ export function createAutocomplete<
6668
});
6769
}
6870

71+
if (
72+
options.insights &&
73+
!props.plugins.some((plugin) => plugin.name === 'aa.algoliaInsightsPlugin')
74+
) {
75+
const insightsParams =
76+
typeof options.insights === 'boolean' ? {} : options.insights;
77+
props.plugins.push(createAlgoliaInsightsPlugin(insightsParams));
78+
}
79+
6980
props.plugins.forEach((plugin) =>
7081
plugin.subscribe?.({
7182
...setters,

packages/autocomplete-core/src/getDefaultProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function getDefaultProps<TItem extends BaseItem>(
3131
autoFocus: false,
3232
defaultActiveItemId: null,
3333
stallThreshold: 300,
34+
insights: false,
3435
environment,
3536
shouldPanelOpen: ({ state }) => getItemsCount(state) > 0,
3637
reshape: ({ sources }) => sources,

packages/autocomplete-core/src/types/AutocompleteStore.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { CancelablePromiseList } from '../utils';
22

3-
import { BaseItem } from './AutocompleteApi';
4-
import { InternalAutocompleteOptions } from './AutocompleteOptions';
5-
import { AutocompleteState } from './AutocompleteState';
3+
import { BaseItem, InternalAutocompleteOptions, AutocompleteState } from './';
64

75
export interface AutocompleteStore<TItem extends BaseItem> {
86
getState(): AutocompleteState<TItem>;

packages/autocomplete-core/src/types/AutocompleteSubscribers.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import { BaseItem } from './AutocompleteApi';
2-
import {
3-
OnActiveParams,
4-
OnResolveParams,
5-
OnSelectParams,
6-
} from './AutocompleteSource';
1+
import { BaseItem, OnActiveParams, OnResolveParams, OnSelectParams } from './';
72

83
export type AutocompleteSubscriber<TItem extends BaseItem> = {
94
onSelect(params: OnSelectParams<TItem>): void;
Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
export * from './AutocompleteApi';
2-
export * from './AutocompleteCollection';
3-
export * from './AutocompleteContext';
4-
export * from './AutocompleteEnvironment';
5-
export * from './AutocompleteOptions';
6-
export * from './AutocompleteSource';
7-
export * from './AutocompletePropGetters';
8-
export * from './AutocompletePlugin';
9-
export * from './AutocompleteReshape';
10-
export * from './AutocompleteSetters';
11-
export * from './AutocompleteState';
1+
export * from '@algolia/autocomplete-shared/dist/esm/core';
122
export * from './AutocompleteStore';
133
export * from './AutocompleteSubscribers';
4+
5+
import { CreateAlgoliaInsightsPluginParams } from '@algolia/autocomplete-plugin-algolia-insights';
6+
import {
7+
AutocompleteOptions as _AutocompleteOptions,
8+
BaseItem,
9+
} from '@algolia/autocomplete-shared/dist/esm/core';
10+
11+
export interface AutocompleteOptions<TItem extends BaseItem>
12+
extends _AutocompleteOptions<TItem> {
13+
/**
14+
* Whether to enable the Insights plugin and load the Insights library if it has not been loaded yet.
15+
*
16+
* See [**autocomplete-plugin-algolia-insights**](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-algolia-insights/) for more information.
17+
*
18+
* @default false
19+
* @link https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete/#param-insights
20+
*/
21+
insights?: CreateAlgoliaInsightsPluginParams | boolean;
22+
}

0 commit comments

Comments
 (0)