-
Notifications
You must be signed in to change notification settings - Fork 94
/
index.spec.js
81 lines (74 loc) · 2.58 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
import "jest-dom/extend-expect"
import React from "react"
import { useAsync } from "react-async"
import { render, cleanup, waitForElement, fireEvent } from "@testing-library/react"
import DevTools from "./index"
import "../../../jest.setup"
const Async = props => {
const { data, error, isPending } = useAsync(props)
if (isPending) return "loading"
if (error) return error
if (data) return data
return null
}
afterEach(cleanup)
describe("DevTools", () => {
test("shows pending requests", async () => {
const promiseFn = () => new Promise(() => {}) // never resolve
const { getByText } = render(
<>
<DevTools />
<Async promiseFn={promiseFn} debugLabel="example" />
</>
)
await waitForElement(() => getByText("loading"))
await waitForElement(() => getByText("Pending"))
await waitForElement(() => getByText("example"))
})
test("shows fulfilled requests", async () => {
const promiseFn = () => Promise.resolve("done")
const { getByText } = render(
<>
<DevTools />
<Async promiseFn={promiseFn} debugLabel="example" />
</>
)
await waitForElement(() => getByText("done"))
await waitForElement(() => getByText("Fulfilled"))
await waitForElement(() => getByText("example"))
})
test("shows rejected requests", async () => {
const promiseFn = () => Promise.reject("oops")
const { getByText } = render(
<>
<DevTools />
<Async promiseFn={promiseFn} debugLabel="example" />
</>
)
await waitForElement(() => getByText("oops"))
await waitForElement(() => getByText("Rejected"))
await waitForElement(() => getByText("example"))
})
test("allows toggling interception of requests", async () => {
const promiseFn = () => Promise.resolve("done")
const { getByText, getByLabelText, queryByText, rerender } = render(<DevTools />)
fireEvent.click(getByLabelText("Pause new requests"))
rerender(
<>
<DevTools />
<Async promiseFn={promiseFn} debugLabel="example" />
</>
)
await waitForElement(() => getByText("Pending"))
expect(queryByText("Fulfilled")).not.toBeInTheDocument()
fireEvent.click(getByText("run"))
await waitForElement(() => getByText("Fulfilled"))
})
test("allows changing the latency", async () => {
const { getByRole, getByText } = render(<DevTools />)
await waitForElement(() => getByText("Latency:"))
await waitForElement(() => getByText("0 seconds"))
fireEvent.change(getByRole("slider"), { target: { value: 2 } })
await waitForElement(() => getByText("2 seconds"))
})
})