-
Notifications
You must be signed in to change notification settings - Fork 1
/
asynchronous.enzyme.test.js
52 lines (39 loc) · 1.7 KB
/
asynchronous.enzyme.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
import React from "react";
import { shallow } from "enzyme";
import AsynchronousFoo from "../../components/asynchronous";
describe("These test suite(s) is/are designed to demonstrate how async events(timeouts) can be tested", () => {
let asynchronousFooWrapper;
beforeEach(() => {
// Jest is able to mock out timers and intervals.
// https://jestjs.io/docs/en/timer-mocks
jest.useFakeTimers();
asynchronousFooWrapper = shallow(<AsynchronousFoo />);
});
describe("increaseSync", () => {
it("increaces `count` by one", () => {
asynchronousFooWrapper.find("button").first().simulate("click");
expect(asynchronousFooWrapper.find("p").text()).toEqual("1");
});
});
describe("decreaseSync", () => {
it("decreases `count` by one", () => {
asynchronousFooWrapper.find("button").at(1).simulate("click");
expect(asynchronousFooWrapper.find("p").text()).toEqual("-1");
});
});
describe("increaseAsync", () => {
it("increases `count` when `setTimeout()` expires", async () => {
asynchronousFooWrapper.find("button").at(2).simulate("click");
// Waiting for the specified ms to expire would make the tests sluggish.
jest.runAllTimers();
expect(asynchronousFooWrapper.find("p").text()).toEqual("1");
});
});
describe("decreaseAsync", () => {
it("decreases `count` when `setTimeout()` expires", async () => {
asynchronousFooWrapper.find("button").last().simulate("click");
jest.runAllTimers();
expect(asynchronousFooWrapper.find("p").text()).toEqual("-1");
});
});
});