diff --git a/packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts b/packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts index 8b2507d0da..208574daea 100644 --- a/packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts +++ b/packages/instantsearch.js/src/middlewares/__tests__/createInsightsMiddleware.ts @@ -15,6 +15,7 @@ import { wait } from '@instantsearch/testutils/wait'; import type { JSDOM } from 'jsdom'; import type { PlainSearchParameters } from 'algoliasearch-helper'; import { fireEvent } from '@testing-library/dom'; +import { createInstantSearch } from '../../../test/createInstantSearch'; declare const jsdom: JSDOM; @@ -227,15 +228,36 @@ describe('insights', () => { }); it('throws when search client does not have credentials', () => { - const { insightsClient, instantSearchInstance } = createTestEnvironment({ - searchClient: createSearchClient(), + const { insightsClient } = createInsights(); + const instantSearchInstance = createInstantSearch({ + // @ts-expect-error fake client + client: { search: () => {} }, }); expect(() => createInsightsMiddleware({ insightsClient, })({ instantSearchInstance }) ).toThrowErrorMatchingInlineSnapshot( - `"[insights middleware]: could not extract Algolia credentials from searchClient"` + `"apiKey is missing, please provide it so we can authenticate the application"` + ); + }); + + it('warns when search client does not have credentials', () => { + const { insightsClient } = createInsights(); + const instantSearchInstance = createInstantSearch({ + // @ts-expect-error fake client + client: { search: () => {} }, + }); + expect(() => { + try { + createInsightsMiddleware({ + insightsClient, + })({ instantSearchInstance }); + } catch (e) { + // insights error + } + }).toWarnDev( + '[InstantSearch.js]: could not extract Algolia credentials from searchClient in insights middleware.' ); }); diff --git a/packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts b/packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts index 74e8f26984..2071cb633f 100644 --- a/packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts +++ b/packages/instantsearch.js/src/middlewares/createInsightsMiddleware.ts @@ -88,11 +88,10 @@ export function createInsightsMiddleware< const [appId, apiKey] = getAppIdAndApiKey(instantSearchInstance.client); // search-insights.js also throws an error so dev-only clarification is sufficient - if (__DEV__ && !(appId && apiKey)) { - throw new Error( - '[insights middleware]: could not extract Algolia credentials from searchClient' - ); - } + warning( + Boolean(appId && apiKey), + 'could not extract Algolia credentials from searchClient in insights middleware.' + ); let queuedUserToken: string | undefined = undefined; let userTokenBeforeInit: string | undefined = undefined; @@ -122,7 +121,12 @@ export function createInsightsMiddleware< // Otherwise, the `init` call might override it with anonymous user token. userTokenBeforeInit = userToken; }); - insightsClient('init', { appId, apiKey, ...insightsInitParams }); + insightsClient('init', { + appId, + apiKey, + useCookie: true, + ...insightsInitParams, + }); let initialParameters: PlainSearchParameters; let helper: AlgoliaSearchHelper; @@ -196,21 +200,16 @@ export function createInsightsMiddleware< if (onEvent) { onEvent(event, _insightsClient as TInsightsClient); } else if (event.insightsMethod) { - const hasUserToken = Boolean( - (helper.state as PlainSearchParameters).userToken - ); - if (hasUserToken) { - insightsClient(event.insightsMethod, event.payload); - } else { - warning( - false, - ` + insightsClient(event.insightsMethod, event.payload); + + warning( + Boolean((helper.state as PlainSearchParameters).userToken), + ` Cannot send event to Algolia Insights because \`userToken\` is not set. See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-further/send-insights-events/js/#setting-the-usertoken ` - ); - } + ); } else { warning( false,