Skip to content

Commit

Permalink
fix(frontend): fix source blur in search
Browse files Browse the repository at this point in the history
handleRouteChange reset source in state to undefined
so we need to reset it to an empty string so we can compare new value
to the one in state and bail update if there no change
  • Loading branch information
lionelB committed Jun 24, 2019
1 parent 1d165e4 commit 510c787
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/code-du-travail-frontend/src/search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Search extends React.Component {
});
}
if (this.props.router.query.source) {
const source = this.props.router.query.source;
const source = this.props.router.query.source || "";
const excludeSources = getExcludeSources(source);
this.setState({ source, excludeSources });
}
Expand All @@ -62,7 +62,7 @@ class Search extends React.Component {
}
this.setState({
query: this.props.router.query.q,
source: this.props.router.query.source,
source: this.props.router.query.source || "",
coord: coord,
excludeSources: getExcludeSources(this.props.router.query.source || "")
});
Expand Down Expand Up @@ -100,6 +100,9 @@ class Search extends React.Component {
});
return;
case "source":
if (this.state.source === event.target.value) {
return;
}
this.setState({
source: event.target.value,
excludeSources: getExcludeSources(event.target.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@ describe("<search />", () => {
await waitForElement(() => getAllByRole("option"));
expect(container).toMatchSnapshot();
});

it("should not navigate when user change facet if query is empty", () => {
const { getBySelectText } = renderWithMock(<Search />);
const select = getBySelectText(/Tous contenus/i);
fireEvent.change(select, { target: { value: "faq" } });
expect(Router.pushRoute).not.toHaveBeenCalled();
});

it("should not navigate when user blur source facet", () => {
const { getBySelectText } = renderWithMock(<Search />, { query: { q } });
const select = getBySelectText(/Tous contenus/i);
fireEvent.focus(select);
fireEvent.blur(select);
expect(Router.pushRoute).not.toHaveBeenCalled();
});

it("should navigate when user change facet if query is filled", () => {
const { getBySelectText, getByLabelText } = renderWithMock(<Search />);
const input = getByLabelText(/rechercher/i);
Expand Down

0 comments on commit 510c787

Please sign in to comment.