Skip to content

Commit

Permalink
test(headless): more rtk2 test refactor (#3666)
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe authored Mar 5, 2024
1 parent a5c846b commit 2999289
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 354 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {updateFacetOptions} from '../../../../features/facet-options/facet-optio
import {
registerDateFacet,
updateDateFacetValues,
validateManualDateRanges,
} from '../../../../features/facets/range-facets/date-facet-set/date-facet-actions';
import {dateFacetSetReducer as dateFacetSet} from '../../../../features/facets/range-facets/date-facet-set/date-facet-set-slice';
import {executeSearch} from '../../../../features/search/search-actions';
Expand All @@ -11,8 +12,10 @@ import {SearchAppState} from '../../../../state/search-app-state';
import {buildMockDateFacetResponse} from '../../../../test/mock-date-facet-response';
import {buildMockDateFacetSlice} from '../../../../test/mock-date-facet-slice';
import {buildMockDateFacetValue} from '../../../../test/mock-date-facet-value';
import {MockSearchEngine} from '../../../../test/mock-engine';
import {buildMockSearchAppEngine} from '../../../../test/mock-engine';
import {
buildMockSearchEngine,
MockedSearchEngine,
} from '../../../../test/mock-engine-v2';
import {createMockState} from '../../../../test/mock-state';
import * as FacetIdDeterminor from '../../../core/facets/_common/facet-id-determinor';
import {buildDateRange} from '../../../core/facets/range-facet/date-facet/date-range';
Expand All @@ -23,21 +26,28 @@ import {
DateFilterOptions,
} from './headless-date-filter';

jest.mock('../../../../features/facet-options/facet-options-actions');
jest.mock(
'../../../../features/facets/range-facets/date-facet-set/date-facet-actions'
);
jest.mock('../../../../features/search/search-actions');

describe('date filter', () => {
const facetId = '1';
let options: DateFilterOptions;
let initialState: DateFilterInitialState | undefined;
let state: SearchAppState;
let engine: MockSearchEngine;
let engine: MockedSearchEngine;
let dateFacet: DateFilter;

function initDateFilter() {
engine = buildMockSearchAppEngine({state});
engine = buildMockSearchEngine(state);
dateFacet = buildDateFilter(engine, {options, initialState});
}

beforeEach(() => {
initialState = undefined;
(updateDateFacetValues as unknown as jest.Mock).mockReturnValue(() => {});

options = {
facetId,
Expand All @@ -54,8 +64,16 @@ describe('date filter', () => {
initialState = {
range: buildDateRange({start: 1616679091000, end: 1616592691000}),
};
expect(() => initDateFilter()).toThrow(
'The start value is greater than the end value for the date range'
initDateFilter();
expect(validateManualDateRanges).toHaveBeenCalledWith(
expect.objectContaining({
currentValues: expect.arrayContaining([
expect.objectContaining({
start: '2021/03/25@22:16:31',
end: '2021/03/24@22:16:31',
}),
]),
})
);
});

Expand All @@ -79,15 +97,14 @@ describe('date filter', () => {
});

it('registers a date facet with the passed options', () => {
const action = registerDateFacet({
expect(registerDateFacet).toHaveBeenCalledWith({
facetId,
generateAutomaticRanges: false,
currentValues: initialState?.range
? [{...initialState.range, endInclusive: true, state: 'selected'}]
: [],
...options,
});
expect(engine.actions).toContainEqual(action);
});

it('when an option is invalid, it throws an error', () => {
Expand All @@ -102,28 +119,18 @@ describe('date filter', () => {
const value = buildMockDateFacetValue({});
dateFacet.setRange(value);

const action = updateDateFacetValues({
expect(updateDateFacetValues).toHaveBeenCalledWith({
facetId,
values: [
{
...value,
state: 'selected',
numberOfResults: 0,
endInclusive: true,
},
{...value, state: 'selected', numberOfResults: 0, endInclusive: true},
],
});
expect(engine.actions).toContainEqual(action);
});

it('dispatches a search', () => {
const value = buildMockDateFacetValue();
dateFacet.setRange(value);

const action = engine.actions.find(
(a) => a.type === executeSearch.pending.type
);
expect(action).toBeTruthy();
expect(executeSearch).toHaveBeenCalled();
});

it('should return true when range is valid', () => {
Expand All @@ -132,34 +139,21 @@ describe('date filter', () => {
);
expect(dateFacet.setRange(value)).toBe(true);
});

it('should return false when range start value is greater than range end value', () => {
const value = buildMockDateFacetValue(
buildDateRange({start: 1616679091000, end: 1616592691000})
);
expect(dateFacet.setRange(value)).toBe(false);
});
});

describe('#clear', () => {
beforeEach(() => dateFacet.clear());

it('dispatches #updateDateFacetValues with the facet id and an empty array', () => {
expect(engine.actions).toContainEqual(
updateDateFacetValues({facetId, values: []})
);
expect(updateDateFacetValues).toHaveBeenCalledWith({facetId, values: []});
});

it('dispatches a #updateFacetOptions action with #freezeFacetOrder true', () => {
expect(engine.actions).toContainEqual(updateFacetOptions());
expect(updateFacetOptions).toHaveBeenCalledWith();
});

it('dispatches a search', () => {
const action = engine.actions.find(
(a) => a.type === executeSearch.pending.type
);

expect(engine.actions).toContainEqual(action);
expect(executeSearch).toHaveBeenCalled();
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import {loadCollection} from '../../features/folding/folding-actions';
import {fetchMoreResults} from '../../features/search/search-actions';
import {SearchAppState} from '../../state/search-app-state';
import {buildMockSearchAppEngine} from '../../test/mock-engine';
import {MockSearchEngine} from '../../test/mock-engine';
import {
MockedSearchEngine,
buildMockSearchEngine,
} from '../../test/mock-engine-v2';
import {buildMockResult} from '../../test/mock-result';
import {createMockState} from '../../test/mock-state';
import {
FoldedResultList,
buildFoldedResultList,
} from './headless-folded-result-list';

jest.mock('../../features/folding/folding-actions');
jest.mock('../../features/search/search-actions');

describe('folded result list', () => {
let state: SearchAppState;
let engine: MockSearchEngine;
let engine: MockedSearchEngine;
let foldedResultList: FoldedResultList;

function initFoldedResultList() {
engine = buildMockSearchAppEngine({state});
engine = buildMockSearchEngine(state);
foldedResultList = buildFoldedResultList(engine);
}

Expand All @@ -34,11 +39,10 @@ describe('folded result list', () => {
result: buildMockResult(),
});

expect(engine.findAsyncAction(loadCollection.pending)).toBeTruthy();
expect(loadCollection).toHaveBeenCalled();
});
it('#fetchMoreResults dispatches the search #fetchMoreResults action', () => {
foldedResultList.fetchMoreResults();

expect(engine.findAsyncAction(fetchMoreResults.pending)).toBeTruthy();
expect(fetchMoreResults).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,35 @@ import {updateResponseFormat} from '../../features/generated-answer/generated-an
import {GeneratedResponseFormat} from '../../features/generated-answer/generated-response-format';
import {executeSearch} from '../../features/search/search-actions';
import {
buildMockSearchAppEngine,
MockSearchEngine,
} from '../../test/mock-engine';
buildMockSearchEngine,
MockedSearchEngine,
} from '../../test/mock-engine-v2';
import {createMockState} from '../../test/mock-state';
import {
buildGeneratedAnswer,
GeneratedAnswer,
GeneratedAnswerProps,
} from './headless-generated-answer';

jest.mock('../../features/generated-answer/generated-answer-actions');
jest.mock('../../features/search/search-actions');

describe('generated answer', () => {
let generatedAnswer: GeneratedAnswer;
let engine: MockSearchEngine;
let engine: MockedSearchEngine;

function initGeneratedAnswer(props: GeneratedAnswerProps = {}) {
generatedAnswer = buildGeneratedAnswer(engine, props);
}

function findAction(actionType: string) {
return engine.actions.find((a) => a.type === actionType);
}

beforeEach(() => {
engine = buildMockSearchAppEngine();
engine = buildMockSearchEngine(createMockState());
initGeneratedAnswer();
});

it('#retry dispatches #executeSearch', () => {
generatedAnswer.retry();
const action = engine.findAsyncAction(executeSearch.pending);

expect(action).toBeTruthy();
expect(executeSearch).toHaveBeenCalled();
});

describe('#rephrase', () => {
Expand All @@ -42,18 +40,12 @@ describe('generated answer', () => {

it('dispatches the update action', () => {
generatedAnswer.rephrase(responseFormat);

const action = findAction(updateResponseFormat.type);
expect(action).toBeDefined();
expect(action).toHaveProperty('payload', responseFormat);
expect(updateResponseFormat).toHaveBeenCalledWith(responseFormat);
});

it('dispatches #executeSearch', () => {
generatedAnswer.rephrase(responseFormat);

const action = engine.findAsyncAction(executeSearch.pending);

expect(action).toBeTruthy();
expect(executeSearch).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import {configuration} from '../../app/common-reducers';
import {insightConfigurationReducer as insightConfiguration} from '../../features/insight-configuration/insight-configuration-slice';
import {fetchInterface} from '../../features/insight-interface/insight-interface-actions';
import {insightInterfaceReducer as insightInterface} from '../../features/insight-interface/insight-interface-slice';
import {searchHubReducer as searchHub} from '../../features/search-hub/search-hub-slice';
import {
buildMockInsightEngine,
MockInsightEngine,
} from '../../test/mock-engine';
MockedInsightEngine,
} from '../../test/mock-engine-v2';
import {buildMockInsightState} from '../../test/mock-insight-state';
import {buildInsightInterface, InsightInterface} from './insight-interface';

jest.mock('../../features/insight-interface/insight-interface-actions');

describe('Insight Interface', () => {
let engine: MockInsightEngine;
let engine: MockedInsightEngine;
let controller: InsightInterface;

function initInsightInterface() {
controller = buildInsightInterface(engine);
}

beforeEach(() => {
engine = buildMockInsightEngine();
engine = buildMockInsightEngine(buildMockInsightState());
initInsightInterface();
});

Expand All @@ -33,12 +37,7 @@ describe('Insight Interface', () => {
describe('#fetch', () => {
it('should dispatch a #fetchInterface action', () => {
controller.fetch();

expect(engine.actions).toContainEqual(
expect.objectContaining({
type: 'insight/interface/fetch/pending',
})
);
expect(fetchInterface).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import {executeSearch} from '../../../features/insight-search/insight-search-actions';
import {updateNumberOfResults} from '../../../features/pagination/pagination-actions';
import {
MockInsightEngine,
MockedInsightEngine,
buildMockInsightEngine,
} from '../../../test/mock-engine';
} from '../../../test/mock-engine-v2';
import {buildMockInsightState} from '../../../test/mock-insight-state';
import {
ResultsPerPage,
ResultsPerPageProps,
buildResultsPerPage,
} from './headless-insight-results-per-page';

jest.mock('../../../features/pagination/pagination-actions');
jest.mock('../../../features/insight-search/insight-search-actions');

describe('InsightResultsPerPage', () => {
let engine: MockInsightEngine;
let engine: MockedInsightEngine;
let props: ResultsPerPageProps;
let resultsPerPage: ResultsPerPage;

Expand All @@ -20,7 +24,7 @@ describe('InsightResultsPerPage', () => {
}

beforeEach(() => {
engine = buildMockInsightEngine({});
engine = buildMockInsightEngine(buildMockInsightState());
props = {
initialState: {},
};
Expand All @@ -31,16 +35,11 @@ describe('InsightResultsPerPage', () => {
it('calling #set updates the number of results to the passed value', () => {
const num = 10;
resultsPerPage.set(num);

expect(engine.actions).toContainEqual(updateNumberOfResults(num));
expect(updateNumberOfResults).toHaveBeenCalledWith(num);
});

it('calling #set executes an executeSearch', () => {
resultsPerPage.set(10);

const action = engine.actions.find(
(a) => a.type === executeSearch.pending.type
);
expect(action).toBeTruthy();
expect(executeSearch).toHaveBeenCalled();
});
});
Loading

0 comments on commit 2999289

Please sign in to comment.