Skip to content

Commit

Permalink
Rework the SearchHeadlessProvider to be a pass-through.
Browse files Browse the repository at this point in the history
Per Product's request, the `SearchHeadlessProvider` is now a mere
pass-through to the `SearchHeadlessContext.Provider`. Users will
need to create and initialize a `Headless` instance themselves.

The `acquireSessionId` utility will be moved to either the Component
Library or Headless, TBD. The Jest test that was added ensures the
`Provider` does not touch anything in the `Headless` instance.

TEST=auto

Ran all unit tests.
  • Loading branch information
tmeyer2115 committed Sep 1, 2022
1 parent 9df8dd9 commit 53ff309
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 93 deletions.
27 changes: 4 additions & 23 deletions src/SearchHeadlessProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
import { PropsWithChildren } from 'react';
import { provideHeadless, SearchHeadless, HeadlessConfig } from '@yext/search-headless';
import { SearchHeadless } from '@yext/search-headless';
import { SearchHeadlessContext } from './SearchHeadlessContext';
import acquireSessionId from './utils/acquireSessionId';
import packageJson from '../package.json';

const { version } = packageJson;

type Props = HeadlessConfig & {
verticalKey?: string,
sessionTrackingEnabled?: boolean
};
type Props = { searcher: SearchHeadless };

export function SearchHeadlessProvider(props: PropsWithChildren<Props>): JSX.Element {
const { children, verticalKey, sessionTrackingEnabled=true, ...searchConfig } = props;
const additionalHttpHeaders = {
'Client-SDK': {
ANSWERS_HEADLESS_REACT: version
}
};
const search: SearchHeadless = provideHeadless(searchConfig, additionalHttpHeaders);
const { children, searcher } = props;

verticalKey && search.setVertical(verticalKey);
search.setSessionTrackingEnabled(sessionTrackingEnabled);
if (sessionTrackingEnabled) {
const sessionId = acquireSessionId();
sessionId && search.setSessionId(sessionId);
}
return (
<SearchHeadlessContext.Provider value={search}>
<SearchHeadlessContext.Provider value={searcher}>
{children}
</SearchHeadlessContext.Provider>
);
Expand Down
22 changes: 0 additions & 22 deletions src/utils/acquireSessionId.ts

This file was deleted.

20 changes: 8 additions & 12 deletions tests/SearchHeadlessProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { SearchHeadlessProvider, SandboxEndpoints } from '../src';
import { SearchHeadlessProvider } from '../src';
import { render } from '@testing-library/react';
import { provideHeadless } from '@yext/search-headless';
import { provideHeadless, SearchHeadless } from '@yext/search-headless';

jest.mock('@yext/search-headless', () => ({
provideHeadless: jest.fn(() => ({
setSessionTrackingEnabled: jest.fn(),
setSessionId: jest.fn()
}))
provideHeadless: jest.fn(() => { return {}; })
}));

it('correctly passes through a search config with sandbox endpoints', () => {
it('does not change anything about the supplied Headless instance', () => {
const config = {
apiKey: '<apiKey>',
experienceKey: '<experienceKey>',
locale: 'en',
endpoints: SandboxEndpoints
locale: 'en'
};
const searcher: SearchHeadless = provideHeadless(config);

render(<SearchHeadlessProvider {...config}/>);
expect(provideHeadless).toHaveBeenCalledTimes(1);
expect(provideHeadless).toHaveBeenCalledWith(config, expect.anything());
expect(
() => render(<SearchHeadlessProvider searcher={searcher}></SearchHeadlessProvider>)).not.toThrowError();
});
36 changes: 0 additions & 36 deletions tests/utils/acquireSessionId.test.ts

This file was deleted.

0 comments on commit 53ff309

Please sign in to comment.