Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(configure): pass the latest state to onStateChange #4555

Merged
merged 2 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/connectors/configure/connectConfigure.ts
Expand Up @@ -88,11 +88,11 @@ const connectConfigure: ConfigureConnector = function connectConfigure(
new algoliasearchHelper.SearchParameters(searchParameters)
);

// Trigger a search with the resolved search parameters
helper.setState(nextSearchParameters).search();

// Update original `widgetParams.searchParameters` to the new refined one
widgetParams.searchParameters = searchParameters;

// Trigger a search with the resolved search parameters
helper.setState(nextSearchParameters).search();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When search happens, it will trigger onStateChange.

image

Before this change, the widgetParams was outdated at this point, now it's updated first, then the search is triggered.

};
}

Expand Down
46 changes: 46 additions & 0 deletions src/lib/__tests__/InstantSearch-integration-test.ts
@@ -0,0 +1,46 @@
import { getByText, fireEvent } from '@testing-library/dom';
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
import instantsearch from '../../index.es';
import { configure } from '../../widgets';
import { connectConfigure } from '../../connectors';
import { createSearchClient } from '../../../test/mock/createSearchClient';

describe('configure', () => {
it('provides up-to-date uiState to onStateChange', () => {
const container = document.createElement('div');
const onStateChange = jest.fn();
const search = instantsearch({
indexName: 'instant_search',
searchClient: createSearchClient(),
onStateChange({ uiState, setUiState }) {
onStateChange(uiState);
setUiState(uiState);
},
});
const customComp = connectConfigure(({ refine }, isFirstRendering) => {
if (isFirstRendering) {
const button = document.createElement('button');
button.setAttribute('type', 'button');
button.textContent = 'click me';
container.appendChild(button);
container.querySelector('button')!.addEventListener('click', () => {
refine({ hitsPerPage: 4 });
});
}
});
search.addWidgets([
configure({
hitsPerPage: 10,
}),
customComp({ searchParameters: {} }),
]);

search.start();
expect(onStateChange).not.toHaveBeenCalled();

fireEvent.click(getByText(container, 'click me'));
expect(onStateChange).toHaveBeenCalledTimes(1);
expect(onStateChange).toHaveBeenCalledWith({
instant_search: { configure: { hitsPerPage: 4 } },
});
});
});