Skip to content

Commit fd29df1

Browse files
committed
fix test
1 parent 6cc1e6c commit fd29df1

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/App.spec.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,6 @@ describe('Reducer Test', () => {
4242
});
4343

4444
describe('App', () => {
45-
const promise = Promise.resolve({
46-
data: {
47-
hits: [
48-
{ objectID: '1', title: 'a' },
49-
{ objectID: '2', title: 'b' },
50-
],
51-
},
52-
});
53-
54-
beforeEach(() => {
55-
axios.get = jest.fn(() => promise);
56-
});
57-
58-
afterEach(() => {
59-
axios.get.mockClear();
60-
});
61-
6245
test('snapshot renders', () => {
6346
const component = renderer.create(<App />);
6447
let tree = component.toJSON();
@@ -101,22 +84,57 @@ describe('App', () => {
10184
expect(counterWrapper.find('p').text()).toBe('-1');
10285
});
10386

104-
it('fetches async data', () => {
87+
it('fetches async data', done => {
88+
const promise = new Promise((resolve, reject) =>
89+
setTimeout(
90+
() =>
91+
resolve({
92+
data: {
93+
hits: [
94+
{ objectID: '1', title: 'a' },
95+
{ objectID: '2', title: 'b' },
96+
],
97+
},
98+
}),
99+
100
100+
)
101+
);
102+
103+
axios.get = jest.fn(() => promise);
104+
105105
const wrapper = mount(<App />);
106106

107107
expect(wrapper.find('li').length).toEqual(0);
108108

109109
promise.then(() => {
110+
wrapper.update();
110111
expect(wrapper.find('li').length).toEqual(2);
112+
113+
axios.get.mockClear();
114+
115+
done();
111116
});
112117
});
113118

114-
it('fetches async data but fails', () => {
119+
it('fetches async data but fails', done => {
120+
const promise = new Promise((resolve, reject) =>
121+
setTimeout(() => reject(new Error('Whoops!')), 100)
122+
);
123+
124+
axios.get = jest.fn(() => promise);
125+
115126
const wrapper = mount(<App />);
116127

117-
promise.then(() => {
118-
expect(wrapper.find('li').length).toEqual(0);
119-
expect(wrapper.find('.error').length).toEqual(1);
128+
promise.catch(() => {
129+
setImmediate(() => {
130+
wrapper.update();
131+
132+
expect(wrapper.find('li').length).toEqual(0);
133+
expect(wrapper.find('.error').length).toEqual(1);
134+
135+
axios.get.mockClear();
136+
done();
137+
});
120138
});
121139
});
122140
});

0 commit comments

Comments
 (0)