Skip to content

Commit 224f87d

Browse files
committed
Modified by new API
1 parent f7bab19 commit 224f87d

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

__mocks__/index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ export default {
3030
} else {
3131
filtered = all;
3232
}
33-
return Promise.resolve({
33+
return {
34+
status: 200,
3435
data: filtered,
35-
});
36+
};
3637
},
3738
'/api/users/mockjs': ({ params }) => {
3839
const all = Mock.mock({
@@ -60,15 +61,30 @@ export default {
6061
} else {
6162
filtered = all;
6263
}
63-
return Promise.resolve({
64+
return {
65+
status: 200,
6466
data: filtered,
65-
});
67+
};
6668
},
6769
'/api/users/{userId}': ({ urlparams }) => {
68-
return Promise.resolve({
70+
return {
71+
status: 200,
6972
data: {
7073
userId: urlparams.userId,
7174
},
72-
});
75+
};
76+
},
77+
'POST /api/users': () => {
78+
return {
79+
status: 201,
80+
};
81+
},
82+
'PUT /api/users/{userId}': ({ urlparams }) => {
83+
return {
84+
status: 204,
85+
data: {
86+
userId: urlparams.userId,
87+
},
88+
};
7389
}
7490
}

test/index.test.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,102 @@ import FetchMock, { Mock } from '../';
55
const fetch = new FetchMock(require('../__mocks__')).fetch;
66
describe('test fetch mock', () => {
77
it('fetch /api/users data', async () => {
8-
const { data } = await fetch('/api/users');
8+
const { status, data } = await fetch('/api/users');
9+
expect(status).to.be.eql(200);
910
expect(data).not.to.be(undefined);
1011
expect(data).not.to.be.empty();
1112
expect(data).to.be.an('array');
1213
expect(data).to.have.length(2);
1314
});
1415

1516
it('fetch /api/users?a=b', async () => {
16-
const { data } = await fetch('/api/users');
17+
const { status, data } = await fetch('/api/users');
18+
expect(status).to.be.eql(200);
1719
expect(data).not.to.be(undefined);
1820
expect(data).not.to.be.empty();
1921
expect(data).to.be.an('array');
2022
expect(data).to.have.length(2);
2123
});
2224

2325
it('fetch /api/users with url parameters', async () => {
24-
const { data } = await fetch('/api/users?name=John');
26+
const { status, data } = await fetch('/api/users?name=John');
27+
expect(status).to.be.eql(200);
2528
expect(data).not.to.be(undefined);
2629
expect(data).not.to.be.empty();
2730
expect(data).to.be.an('array');
2831
expect(data).to.have.length(1);
2932
});
3033

3134
it('fetch /api/users with post parameters', async () => {
32-
const { data } = await fetch('/api/users', {
33-
method: 'POST',
35+
const { status, data } = await fetch('/api/users', {
36+
method: 'GET',
3437
headers: {
3538
'Content-Type': 'application/json',
3639
},
3740
body: JSON.stringify({
3841
name: 'John',
3942
}),
4043
});
44+
expect(status).to.be.eql(200);
4145
expect(data).not.to.be(undefined);
4246
expect(data).not.to.be.empty();
4347
expect(data).to.be.an('array');
4448
expect(data).to.have.length(1);
4549
});
4650

4751
it('fetch /api/users/{userId}', async () => {
48-
const { data } = await fetch('/api/users/123');
52+
const { status, data } = await fetch('/api/users/123');
53+
expect(status).to.be.eql(200);
4954
expect(data).not.to.be(undefined);
5055
expect(data).not.to.be.empty();
5156
expect(data).to.be.property('userId', '123');
5257
});
5358

5459
it('fetch /api/users/mockjs with mockjs', async () => {
55-
const { data } = await fetch('/api/users/mockjs');
60+
const { status, data } = await fetch('/api/users/mockjs');
61+
expect(status).to.be.eql(200);
5662
expect(data).not.to.be(undefined);
5763
expect(data).not.to.be.empty();
5864
expect(data).to.be.an('array');
5965
expect(data).to.have.length(2);
6066
});
6167

6268
it('fetch /api/users/mockjs with mockjs', async () => {
63-
const { data } = await fetch('/api/users/mockjs');
69+
const { status, data } = await fetch('/api/users/mockjs');
70+
expect(status).to.be.eql(200);
6471
expect(data).not.to.be(undefined);
6572
expect(data).not.to.be.empty();
6673
expect(data).to.be.an('array');
6774
expect(data).to.have.length(2);
6875
});
76+
77+
it('post /api/users', async () => {
78+
const { status } = await fetch('/api/users', {
79+
method: 'POST',
80+
headers: {
81+
'Content-Type': 'application/json',
82+
},
83+
body: JSON.stringify({
84+
name: 'John',
85+
}),
86+
});
87+
expect(status).to.be.eql(201);
88+
});
89+
90+
it('put /api/users/123', async () => {
91+
const { status, data } = await fetch('/api/users/123', {
92+
method: 'PUT',
93+
headers: {
94+
'Content-Type': 'application/json',
95+
},
96+
body: JSON.stringify({
97+
name: 'John2',
98+
}),
99+
});
100+
expect(status).to.be.eql(204);
101+
expect(data).not.to.be(undefined);
102+
expect(data).not.to.be.empty();
103+
expect(data).to.be.an('object');
104+
expect(data.userId).to.be.eql(123);
105+
});
69106
});

0 commit comments

Comments
 (0)