Skip to content

Commit

Permalink
chore: eliminate network calls in npm test (#4453)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeaudoincoveo authored Sep 25, 2024
1 parent 92c5404 commit c6393a9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
36 changes: 35 additions & 1 deletion packages/headless-react/src/ssr/search-engine.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {NavigatorContextProvider} from '@coveo/headless/dist/definitions/app/navigatorContextProvider.js';
import {
getSampleSearchEngineConfiguration,
InferStaticState,
InferHydratedState,
defineResultList,
defineSearchBox,
Result,
NavigatorContextProvider,
} from '@coveo/headless/ssr';
import {act, render, renderHook, screen} from '@testing-library/react';
import {randomUUID} from 'crypto';
import {PropsWithChildren} from 'react';
import React from 'react';
import {
vi,
expect,
Expand Down Expand Up @@ -106,7 +109,38 @@ describe('Headless react SSR utils', () => {
} = engineDefinition;

function TestResultList() {
const generateMockResult: () => Result = () => {
return {
absentTerms: [],
clickUri: '',
excerpt: '',
excerptHighlights: [],
firstSentences: '',
firstSentencesHighlights: [],
flags: '',
hasHtmlVersion: false,
isRecommendation: false,
isTopResult: false,
isUserActionView: false,
percentScore: 0,
printableUri: '',
printableUriHighlights: [],
rankingInfo: null,
raw: {urihash: ''},
score: 0,
searchUid: '',
summary: null,
summaryHighlights: [],
title: '',
titleHighlights: [],
uniqueId: randomUUID(),
uri: '',
};
};

const {state} = controllers.useResultList();

state.results = Array.from({length: numResults}, generateMockResult);
return (
<ul>
{state.results.map((result) => (
Expand Down
6 changes: 4 additions & 2 deletions packages/headless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@
"build:bundles": "node esbuild.mjs",
"build:definitions": "tsc -p src/tsconfig.build.json -d --emitDeclarationOnly --declarationDir dist/definitions",
"clean": "rimraf -rf dist/*",
"test": "vitest run",
"test:watch": "vitest",
"test": "vitest run --exclude \"src/integration-tests/**\"",
"test:watch": "vitest --exclude \"src/integration-tests/**\"",
"integration-test": "vitest run --poolOptions.threads.singleThread src/integration-tests/**",
"integration-test:watch": "vitest --poolOptions.threads.singleThread src/integration-tests/**",
"publish:npm": "npm run-script -w=@coveo/release npm-publish",
"publish:bump": "npm run-script -w=@coveo/release bump",
"promote:npm:latest": "node ../../scripts/deploy/update-npm-tag.mjs latest",
Expand Down
31 changes: 12 additions & 19 deletions packages/headless/src/app/search-engine/search-engine.ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ describe('SSR', () => {
);
}

function getResultsCount(state: AnyState) {
return state.controllers.resultList.state.results.length;
}

beforeEach(() => {
engineDefinition = defineSearchEngine({
configuration: {
Expand Down Expand Up @@ -147,27 +143,27 @@ describe('SSR', () => {
});

it('fetches initial state of engine', async () => {
const {fetchStaticState} = engineDefinition;
const fetchStaticState = vi.mocked(engineDefinition.fetchStaticState);
const staticState = await fetchStaticState();
expect(staticState).toBeTruthy();
expect(getResultsCount(staticState)).toBe(defaultNumberOfResults);
expect(getResultsPerPage(staticState)).toBe(defaultNumberOfResults);
});

describe('with a static state', () => {
let staticState: StaticState;
beforeEach(async () => {
const {fetchStaticState} = engineDefinition;
const fetchStaticState = vi.mocked(engineDefinition.fetchStaticState);
staticState = await fetchStaticState();
});

it('hydrates engine and fetches results using hydrated engine', async () => {
const {hydrateStaticState} = engineDefinition;
it('hydrates engine', async () => {
const hydrateStaticState = vi.mocked(
engineDefinition.hydrateStaticState
);
const hydratedState = await hydrateStaticState(staticState);
expect(hydratedState.engine.state.configuration.organizationId).toEqual(
getSampleSearchEngineConfiguration().organizationId
);
expect(getResultsCount(hydratedState)).toBe(defaultNumberOfResults);
expect(getResultsPerPage(hydratedState)).toBe(defaultNumberOfResults);
});
});
Expand All @@ -176,7 +172,7 @@ describe('SSR', () => {
const newNumberOfResults = 6;

async function fetchBuildResultWithNewNumberOfResults() {
const {build} = engineDefinition;
const build = vi.mocked(engineDefinition.build);
const buildResult = await build();
const {registerNumberOfResults} = loadPaginationActions(
buildResult.engine
Expand All @@ -189,33 +185,30 @@ describe('SSR', () => {
}

it('fetches initial state of engine from build result', async () => {
const {fetchStaticState} = engineDefinition;
const fetchStaticState = vi.mocked(engineDefinition.fetchStaticState);
const staticState = await fetchStaticState.fromBuildResult({
buildResult: await fetchBuildResultWithNewNumberOfResults(),
});
expect(staticState).toBeTruthy();
expect(getResultsCount(staticState)).toBe(newNumberOfResults);
expect(getResultsPerPage(staticState)).toBe(newNumberOfResults);
});

describe('with the default static state', () => {
let staticState: StaticState;
beforeEach(async () => {
const {fetchStaticState} = engineDefinition;
const fetchStaticState = vi.mocked(engineDefinition.fetchStaticState);
staticState = await fetchStaticState();
});

it('hydrates engine from build result', async () => {
const {hydrateStaticState} = engineDefinition;
const hydrateStaticState = vi.mocked(
engineDefinition.hydrateStaticState
);
const buildResult = await fetchBuildResultWithNewNumberOfResults();
const hydratedState = await hydrateStaticState.fromBuildResult({
buildResult: buildResult,
searchAction: staticState.searchAction,
});
expect(getResultsCount(staticState)).toBe(defaultNumberOfResults);
expect(getResultsCount(hydratedState)).toBe(
getResultsCount(staticState)
);
expect(getResultsPerPage(hydratedState)).toBe(newNumberOfResults);
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/headless/src/ssr.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type {SortDefinition} from './controllers/sort/headless-sort.ssr.js';
export type {StandaloneSearchBoxDefinition} from './controllers/standalone-search-box/headless-standalone-search-box.ssr.js';
export type {StaticFilterDefinition} from './controllers/static-filter/headless-static-filter.ssr.js';
export type {TabDefinition} from './controllers/tab/headless-tab.ssr.js';
export type {NavigatorContextProvider} from './app/navigatorContextProvider.js';

// export type
export type {CoreEngine, ExternalEngineOptions} from './app/engine.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/samples/headless-commerce-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "nx build",
"build:client": "vite build",
"preview": "vite preview",
"test": "vitest"
"test": "vitest run"
},
"browserslist": {
"production": [
Expand Down

0 comments on commit c6393a9

Please sign in to comment.