From 71973305d8a2c70f5a2a4c8cbc94b2cc78eed98d Mon Sep 17 00:00:00 2001 From: Sarah Dayan <5370675+sarahdayan@users.noreply.github.com> Date: Tue, 7 Mar 2023 17:39:08 +0100 Subject: [PATCH 1/6] feat(createAlgoliaInsightsPlugin): automatically load Insights when not passed --- package.json | 1 + .../createAlgoliaInsightsPlugin.test.ts | 131 +++++++++++++++++- .../src/createAlgoliaInsightsPlugin.ts | 58 +++++++- .../src/types/InsightsClient.ts | 31 ++++- .../src/__tests__/safelyRunOnBrowser.test.ts | 64 +++++++++ packages/autocomplete-shared/src/index.ts | 1 + .../src/safelyRunOnBrowser.ts | 24 ++++ yarn.lock | 5 + 8 files changed, 310 insertions(+), 5 deletions(-) create mode 100644 packages/autocomplete-shared/src/__tests__/safelyRunOnBrowser.test.ts create mode 100644 packages/autocomplete-shared/src/safelyRunOnBrowser.ts 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..f604aa89c 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,127 @@ 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).toBe(undefined); + expect((window as any).aa).toBe(undefined); + }); + + 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); + }); + + 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(` + +