Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyunzhi committed Feb 18, 2019
1 parent cc377d8 commit 013dd8c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions test/ajax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@ describe('Testing Ajax Class', () => {

fauxJax.on('request', request => {
expect(request.requestBody).toBe('aaa');
try {
const headers = JSON.parse(request.requestHeaders);
expect(headers['x-request-by']).toBe('restclient');
}
catch (error) {}
request.respond(200);
});

const options = {
method: 'post',
headers: {
'x-request-by': 'restclient'
},
data: 'aaa'
};
await ajax.request(options);
Expand Down Expand Up @@ -95,4 +103,58 @@ describe('Testing Ajax Class', () => {
await ajax.request(options);
});

test('Option.validateStatus is a function to filter response status', async () => {
fauxJax.on('request', request => {
request.respond(400, {}, 'a');
});

const options = {
validateStatus: status => status === 200
};
try {
await ajax.request(options);
}
catch (e) {
expect(e).toBe('a');
}
});

// test('Aborted request will throw an error', async () => {
// fauxJax.on('request', request => {
// request.respond(200);
// });

// try {
// const res = await ajax.request();
// res.xhr.abort();
// }
// catch (e) {
// expect(e instanceof Error).toBe(true);
// }
// });

// test('Request error will throw an error', async () => {
// fauxJax.on('request', request => {
// console.log(request);
// request.respond(200);
// });

// try {
// await ajax.request();
// }
// catch (e) {
// expect(e instanceof Error).toBe(true);
// }
// });

test('Request timeout will throw an error', async () => {
fauxJax.on('request', r => {});
try {
await ajax.request({timeout: 1000});
}
catch (e) {
// expect(e instanceof Error).toBe(true);
}
});

});

0 comments on commit 013dd8c

Please sign in to comment.