-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.spec.js
69 lines (60 loc) · 1.98 KB
/
test.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React from "react";
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { Provider } from "react-redux";
import { skipToken } from "@reduxjs/toolkit/query";
import { configureStore } from "@reduxjs/toolkit";
beforeEach(() => {
const api = createApi({
reducerPath: "my-api",
baseQuery: fetchBaseQuery({ baseUrl: "https://example.test" }),
endpoints: (build) => ({
helloWorld: build.query({
queryFn() {
return { data: "hello world" };
},
}),
}),
});
const store = configureStore({
reducer: { [api.reducerPath]: api.reducer },
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat([api.middleware]),
});
function Component() {
const [searchString, setSearchString] = React.useState("");
const { data } = api.endpoints.helloWorld.useQuery(
searchString || skipToken
);
return (
<>
<input
placeholder="search here"
onChange={(e) => setSearchString(e.target.value)}
/>
<p>{data ? "Hello world" : null}</p>
</>
);
}
render(
<Provider store={store}>
<Component />
</Provider>
);
});
it("with real timers - this passes", async () => {
jest.useRealTimers();
const addressSearchBar = await screen.findByPlaceholderText("search here");
await userEvent.click(addressSearchBar);
await userEvent.paste("my search string");
await screen.findByText("Hello world", undefined, { timeout: 2000 });
});
it("with fake timers - this fails", async () => {
jest.useFakeTimers();
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
const addressSearchBar = await screen.findByPlaceholderText("search here");
await user.click(addressSearchBar);
await user.paste("my search string");
await screen.findByText("Hello world", undefined, { timeout: 2000 });
});