Skip to content

Commit

Permalink
Updated setSearchTerm tests
Browse files Browse the repository at this point in the history
I updated these tests because they were testing the wrong thing.

There are two side effects that occur as a result of setSearchTerm:
_udpateSearchResults
_makeSearchRequest

_udpateSearchResults is what we are debouncing, so we want to verify
whether that has run or not. Testing whether or not an API request
has been make is not a good way to test this, since _makeSearchRequest
is now always debounced.

In other words, we could call _udpateSearchResults 3 times in a row
and it will execute 3 times, but ultimately only one api call will be
made.
  • Loading branch information
JasonStoltz committed Sep 25, 2019
1 parent b0c81b8 commit 159f8c1
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions packages/search-ui/src/__tests__/actions/setSearchTerm.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
getAutocompleteCalls,
getSearchCalls,
setupDriver
} from "../../test/helpers";
import { getAutocompleteCalls, setupDriver } from "../../test/helpers";
import {
itResetsCurrent,
itResetsFilters,
Expand Down Expand Up @@ -126,20 +122,35 @@ describe("#setSearchTerm", () => {
expect(subject("term", { refresh: false }).state.wasSearched).toBe(false);
});

it("Will not debounce requests if there is no debounce specified", () => {
const { driver, mockApiConnector } = setupDriver();
driver.setSearchTerm("term", { refresh: true });
it("Will debounce the request state update if a debounce is specified", () => {
const { driver, updatedStateAfterAction } = setupDriver({
initialState: {
searchTerm: "park",
current: 2
}
});
jest.runAllTimers();
expect(getSearchCalls(mockApiConnector)).toHaveLength(1);
});

it("Will debounce requests", () => {
const { driver, mockApiConnector } = setupDriver();
driver.setSearchTerm("term", { refresh: true, debounce: 1000 });
driver.setSearchTerm("term", { debounce: 1000 });
expect(updatedStateAfterAction.state.current).toBe(2);
jest.advanceTimersByTime(500);
expect(getSearchCalls(mockApiConnector)).toHaveLength(0);
expect(updatedStateAfterAction.state.current).toBe(2);
jest.advanceTimersByTime(500);
expect(getSearchCalls(mockApiConnector)).toHaveLength(1);
expect(updatedStateAfterAction.state.current).toBe(1);
});

it("Will not debounce the request state update if no debounce is specified", () => {
const { driver, updatedStateAfterAction } = setupDriver({
initialState: {
searchTerm: "park",
current: 2
}
});
jest.runAllTimers();
expect(updatedStateAfterAction.state.current).toBe(2);
driver.setSearchTerm("term");
// Note that current is set to 1 by the request state update, and we did
// not have to wait any amount of time, it was executed immediately.
expect(updatedStateAfterAction.state.current).toBe(1);
});

describe("when autocompleteResults is true", () => {
Expand Down

0 comments on commit 159f8c1

Please sign in to comment.