diff --git a/bundlesize.config.json b/bundlesize.config.json index 40af35a20..154e4dce4 100644 --- a/bundlesize.config.json +++ b/bundlesize.config.json @@ -14,7 +14,7 @@ }, { "path": "packages/autocomplete-plugin-algolia-insights/dist/umd/index.production.js", - "maxSize": "2.1 kB" + "maxSize": "2.5 kB" }, { "path": "packages/autocomplete-plugin-redirect-url/dist/umd/index.production.js", diff --git a/package.json b/package.json index 1823c6a60..1c8521a14 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,7 @@ "rollup-plugin-filesize": "9.1.2", "rollup-plugin-license": "2.9.1", "rollup-plugin-terser": "7.0.2", + "search-insights": "2.3.0", "shipjs": "0.24.1", "start-server-and-test": "1.15.2", "stylelint": "13.13.1", diff --git a/packages/autocomplete-plugin-algolia-insights/src/__tests__/createAlgoliaInsightsPlugin.test.ts b/packages/autocomplete-plugin-algolia-insights/src/__tests__/createAlgoliaInsightsPlugin.test.ts index 00dcad110..9b31d7170 100644 --- a/packages/autocomplete-plugin-algolia-insights/src/__tests__/createAlgoliaInsightsPlugin.test.ts +++ b/packages/autocomplete-plugin-algolia-insights/src/__tests__/createAlgoliaInsightsPlugin.test.ts @@ -4,6 +4,7 @@ import { getAlgoliaResults, } from '@algolia/autocomplete-preset-algolia'; import { noop } from '@algolia/autocomplete-shared'; +import { fireEvent } from '@testing-library/dom'; import userEvent from '@testing-library/user-event'; import insightsClient from 'search-insights'; @@ -12,11 +13,17 @@ import { createPlayground, createSearchClient, createSource, + defer, runAllMicroTasks, } from '../../../../test/utils'; import { createAlgoliaInsightsPlugin } from '../createAlgoliaInsightsPlugin'; -jest.useFakeTimers(); +beforeEach(() => { + (window as any).AlgoliaAnalyticsObject = undefined; + (window as any).aa = undefined; + + document.body.innerHTML = ''; +}); describe('createAlgoliaInsightsPlugin', () => { test('has a name', () => { @@ -70,7 +77,7 @@ describe('createAlgoliaInsightsPlugin', () => { ); }); - test('sets a user agent on the Insights client on subscribe', () => { + test('sets a user agent on on subscribe', () => { const insightsClient = jest.fn(); const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient }); @@ -167,7 +174,129 @@ describe('createAlgoliaInsightsPlugin', () => { ]); }); + describe('automatic pulling', () => { + const consoleError = jest + .spyOn(console, 'error') + .mockImplementation(() => {}); + + afterAll(() => { + consoleError.mockReset(); + }); + + it('does not load the script when the Insights client is passed', async () => { + createPlayground(createAutocomplete, { + plugins: [createAlgoliaInsightsPlugin({ insightsClient: noop })], + }); + + await defer(noop, 0); + + expect(document.body).toMatchInlineSnapshot(` + +
+ +
+ + `); + expect((window as any).AlgoliaAnalyticsObject).toBeUndefined(); + expect((window as any).aa).toBeUndefined(); + }); + + it('does not load the script when the Insights client is present in the page', async () => { + (window as any).AlgoliaAnalyticsObject = 'aa'; + const aa = noop; + (window as any).aa = aa; + + createPlayground(createAutocomplete, { + plugins: [createAlgoliaInsightsPlugin({})], + }); + + await defer(noop, 0); + + expect(document.body).toMatchInlineSnapshot(` + +
+ +
+ + `); + expect((window as any).AlgoliaAnalyticsObject).toBe('aa'); + expect((window as any).aa).toBe(aa); + expect((window as any).aa.version).toBeUndefined(); + }); + + it('loads the script when the Insights client is not passed and not present in the page', async () => { + createPlayground(createAutocomplete, { + plugins: [createAlgoliaInsightsPlugin({})], + }); + + await defer(noop, 0); + + expect(document.body).toMatchInlineSnapshot(` + +