-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bunny.test.js
56 lines (46 loc) · 1.43 KB
/
Bunny.test.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
import React from "react";
import { render, act, waitFor } from "@testing-library/react";
import { Bunny } from "../Bunny";
let subject;
let refreshTime = 5000;
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
delete global.fetch;
});
describe("Bunny", () => {
beforeEach(() => {
global.fetch = jest.fn().mockResolvedValue({
status: 200,
ok: true,
json: () => Promise.resolve({ media: { poster: "hello" } }),
});
});
describe("when fetching", () => {
beforeEach(() => {
subject = <Bunny isFetching={true} refreshTime={refreshTime} />;
});
it("fetches an image on initial render", async () => {
render(subject);
await waitFor(() => expect(global.fetch).toHaveBeenCalledTimes(1));
});
it("gets a new image on the configured interval", async () => {
render(subject);
await waitFor(() => expect(global.fetch).toHaveBeenCalledTimes(1));
act(() => jest.advanceTimersByTime(refreshTime));
await waitFor(() => expect(global.fetch).toHaveBeenCalledTimes(2));
});
});
describe("when not fetching", () => {
beforeEach(() => {
subject = <Bunny isFetching={false} refreshTime={refreshTime} />;
});
it("does not fetch images", async () => {
render(subject);
act(() => jest.advanceTimersByTime(refreshTime));
await waitFor(() => expect(global.fetch).not.toHaveBeenCalled());
});
});
});