-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
71 lines (64 loc) · 1.81 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const {React, DummyComponent, DummyComponentWithEffect} = require('.');
test('should render properly the initial value count', () => {
// Arrange
const bck = console.log;
const mockLog = jest.fn();
console.log = mockLog;
// Act
React.render(DummyComponent);
// Assert
assertConsoleHasCalledTimes(1);
assertConsoleBeenCalledWith("<div>Count: 1</div>");
console.log = bck;
});
test('should render properly the state after click in the component', () => {
// Arrange
const bck = console.log;
const mockLog = jest.fn();
console.log = mockLog;
// Act
let app;
app = React.render(DummyComponent);
app.click();
app.click();
React.render(DummyComponent);
// Assert
assertConsoleHasCalledTimes(2);
assertConsoleBeenCalledWith("<div>Count: 2</div>", 1);
console.log = bck;
});
test('should render properly the state after click in the component', () => {
// Arrange
const bck = console.log;
const mockLog = jest.fn();
console.log = mockLog;
// Act
let app;
app = React.render(DummyComponent);
app.click();
app.click();
React.render(DummyComponent);
// Assert
assertConsoleHasCalledTimes(2);
assertConsoleBeenCalledWith("<div>Count: 3</div>", 1);
console.log = bck;
});
test('should render properly with useEffect', () => {
// Arrange
const bck = console.log;
const mockLog = jest.fn();
console.log = mockLog;
// Act
React.render(DummyComponentWithEffect);
// Assert
assertConsoleHasCalledTimes(2);
assertConsoleBeenCalledWith("Hello from side effect", 0);
assertConsoleBeenCalledWith("<div>Render component!</div>", 1);
console.log = bck;
});
function assertConsoleHasCalledTimes(times) {
expect(console.log.mock.calls.length).toEqual(times);
}
function assertConsoleBeenCalledWith(obj, index = 0) {
expect(console.log.mock.calls[index][0]).toEqual(obj);
}