Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
yizhoucao committed Jan 18, 2019
1 parent d89c603 commit 0d8d91d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/utils/__snapshots__/httpUtil.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`get not 200 1`] = `"Error status 201 statusMessage"`;

exports[`post not 200 1`] = `"Error status 201 statusMessage"`;
57 changes: 57 additions & 0 deletions src/utils/httpUtil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import httpUtil from './httpUtil';

let error = '';
const res = { statusCode: 200, statusMessage: 'statusMessage' };
const body = 'body';
jest.mock('request', () =>
jest.fn((option: any, cb: any) => {
if (option) cb(error, res, body);
})
);

test('get', async () => {
expect(await httpUtil.get('url')).toBe('body');
});

test('get error', async () => {
error = 'error';
try {
await httpUtil.get('url');
} catch (error) {
expect(error).toBe('error');
}
});

test('get not 200', async () => {
error = '';
res.statusCode = 201;
try {
await httpUtil.get('url');
} catch (error) {
expect(error).toMatchSnapshot();
}
});

test('post', async () => {
res.statusCode = 200;
expect(await httpUtil.postJson('url', {})).toBe('body');
});

test('post error', async () => {
error = 'error';
try {
await httpUtil.postJson('url', {});
} catch (error) {
expect(error).toBe('error');
}
});

test('post not 200', async () => {
error = '';
res.statusCode = 201;
try {
await httpUtil.postJson('url', {});
} catch (error) {
expect(error).toMatchSnapshot();
}
});
2 changes: 1 addition & 1 deletion src/utils/httpUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class HttpUtil {
);
}

public postJson(url: string, json: object): Promise<object> {
public postJson(url: string, json: object): Promise<any> {
return new Promise((resolve, reject) =>
request(
{
Expand Down

0 comments on commit 0d8d91d

Please sign in to comment.