-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.test.js
51 lines (44 loc) · 1.55 KB
/
jest.test.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
const axios = require("axios");
const MockAdapter = require('axios-mock-adapter');
const { getDeviceModel, gistUrl } = require('./index');
describe('Mocking Sample', () => {
let axiosMock;
const deviceModel =
{
uid: '462-1000',
name: '4900',
manufacturer: 'OKUMA',
model: 'MA600',
building: 'A',
cell: 'INTBODIES',
location: 'Fort Worth',
coords: {},
businessUnit: 'Pressure Pumping',
coolantDevice: '4900-Coolant',
}
beforeAll(() => {
// setup the express server with the .mock for app.use
});
beforeEach(() => {
axiosMock = new MockAdapter(axios);
});
test('Valid Response', async () => {
axiosMock.onGet(gistUrl).reply(200, JSON.stringify(deviceModel));
const spy = jest.spyOn(axios, 'get');
const response = await getDeviceModel();
expect(spy).toBeCalled();
expect(response.success).toEqual(true);
expect(response.result.uid).toEqual('462-1000');
});
test('Invalid Response', async () => {
axiosMock.onGet(gistUrl).reply(200, JSON.stringify({ ...deviceModel, uid: 'Im not what you want me to be' }));
const spy = jest.spyOn(axios, 'get');
const response = await getDeviceModel();
expect(spy).toBeCalled();
expect(response.success).toEqual(false);
});
test('Invalid Status Code', async () => {
axiosMock.onGet(gistUrl).reply(500, JSON.stringify(deviceModel));
await expect(getDeviceModel()).rejects.toThrow();
});
});