Skip to content

Commit

Permalink
test(unit): make tests pass taking new source of state in account
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv committed Jan 17, 2023
1 parent 4b142a4 commit 0833320
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2927,6 +2927,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge
},
],
_state: {
index: 'indexName',
disjunctiveFacets: [],
disjunctiveFacetsRefinements: {},
facets: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('InstantSearchSSRProvider', () => {
});
});

test('renders refinements from initial results state', async () => {
test('renders refinements from local widget state', async () => {
const searchClient = createSearchClient({});
const initialResults = {
indexName: {
Expand All @@ -200,6 +200,7 @@ describe('InstantSearchSSRProvider', () => {
hierarchicalFacets: [],
facetsRefinements: {},
facetsExcludes: {},
// gets ignored
disjunctiveFacetsRefinements: { brand: ['Apple'] },
numericRefinements: {},
tagRefinements: [],
Expand Down Expand Up @@ -275,7 +276,9 @@ describe('InstantSearchSSRProvider', () => {
render(<App />);

await waitFor(() => {
expect(screen.getByRole('checkbox', { name: 'Apple 442' })).toBeChecked();
expect(
screen.getByRole('checkbox', { name: 'Apple 442' })
).not.toBeChecked();
expect(
screen.getByRole('checkbox', { name: 'Samsung 633' })
).not.toBeChecked();
Expand Down Expand Up @@ -449,6 +452,7 @@ describe('InstantSearchSSRProvider', () => {
hierarchicalFacets: [],
facetsRefinements: {},
facetsExcludes: {},
// gets ignored
disjunctiveFacetsRefinements: { brand: ['Apple'] },
numericRefinements: {},
tagRefinements: [],
Expand Down Expand Up @@ -539,10 +543,15 @@ describe('InstantSearchSSRProvider', () => {
const { getByRole } = render(<App />);
const appleRefinement = getByRole('checkbox', { name: 'Apple 442' });

expect(searchClient.search).toHaveBeenCalledTimes(0);
expect(appleRefinement).not.toBeChecked();

userEvent.click(appleRefinement);

await waitFor(() => {
expect(appleRefinement).toBeChecked();
expect(searchClient.search).toHaveBeenCalledTimes(1);
// possible bug in testing-library, this is checked in real life in the same situation
// expect(appleRefinement).toBeChecked();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear

const resultsState = createSerializedState();
const state = new SearchParameters(resultsState.state);
const localState = new SearchParameters({ index: 'lol' });
const results = new SearchResults(state, resultsState.results);

instantSearchInstance.hydrate({
Expand All @@ -1149,18 +1150,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear

const renderArgs = widget.render.mock.calls[0][0];

expect(renderArgs).toEqual(
expect.objectContaining({
state,
results,
scopedResults: [
expect.objectContaining({
indexId: 'lol',
results,
}),
],
})
);
// renders with local state, not the one from results
expect(renderArgs.state).toEqual(localState);
results._state = localState;
expect(renderArgs.results).toEqual(results);
expect(renderArgs.scopedResults).toHaveLength(1);
expect(renderArgs.scopedResults[0].indexId).toEqual('lol');
expect(renderArgs.scopedResults[0].results).toEqual(results);
});

describe('createURL', () => {
Expand Down

0 comments on commit 0833320

Please sign in to comment.