-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathApp.spec.js
147 lines (115 loc) · 3.35 KB
/
App.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import React from 'react';
import axios from 'axios';
import renderer from 'react-test-renderer';
import { mount } from 'enzyme';
import App, { dataReducer, Counter } from './App';
const list = ['a', 'b', 'c'];
describe('App', () => {
describe('Reducer', () => {
it('should set a list', () => {
const state = { list: [], error: null };
const newState = dataReducer(state, {
type: 'SET_LIST',
list,
});
expect(newState).toEqual({ list, error: null });
});
it('should reset the error if list is set', () => {
const state = { list: [], error: true };
const newState = dataReducer(state, {
type: 'SET_LIST',
list,
});
expect(newState).toEqual({ list, error: null });
});
it('should set the error', () => {
const state = { list: [], error: null };
const newState = dataReducer(state, {
type: 'SET_ERROR',
});
expect(newState.error).toBeTruthy();
});
});
test('snapshot renders', () => {
const component = renderer.create(<App />);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
it('renders the inner Counter', () => {
const wrapper = mount(<App />);
expect(wrapper.find(Counter).length).toEqual(1);
});
it('passes all props to Counter', () => {
const wrapper = mount(<App />);
const counterWrapper = wrapper.find(Counter);
expect(counterWrapper.find('p').text()).toEqual('0');
});
it('increments the counter', () => {
const wrapper = mount(<App />);
wrapper
.find('button')
.at(0)
.simulate('click');
const counterWrapper = wrapper.find(Counter);
expect(counterWrapper.find('p').text()).toBe('1');
});
it('decrements the counter', () => {
const wrapper = mount(<App />);
wrapper
.find('button')
.at(1)
.simulate('click');
const counterWrapper = wrapper.find(Counter);
expect(counterWrapper.find('p').text()).toBe('-1');
});
it('fetches async data', done => {
const promise = new Promise((resolve, reject) =>
setTimeout(
() =>
resolve({
data: {
hits: [
{ objectID: '1', title: 'a' },
{ objectID: '2', title: 'b' },
],
},
}),
100
)
);
axios.get = jest.fn(() => promise);
const wrapper = mount(<App />);
expect(wrapper.find('li').length).toEqual(0);
promise.then(() => {
setImmediate(() => {
wrapper.update();
expect(wrapper.find('li').length).toEqual(2);
axios.get.mockClear();
done();
});
});
});
it('fetches async data but fails', done => {
const promise = new Promise((resolve, reject) =>
setTimeout(() => reject(new Error('Whoops!')), 100)
);
axios.get = jest.fn(() => promise);
const wrapper = mount(<App />);
promise.catch(() => {
setImmediate(() => {
wrapper.update();
expect(wrapper.find('li').length).toEqual(0);
expect(wrapper.find('.error').length).toEqual(1);
axios.get.mockClear();
done();
});
});
});
});
describe('Counter', () => {
test('snapshot renders', () => {
const component = renderer.create(<Counter counter={1} />);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});