-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestionnaireApi.test.ts
98 lines (79 loc) · 2.88 KB
/
questionnaireApi.test.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { getQuestionnaire, postQuestionnaire } from './../src/api/questionnaire';
import axios, { AxiosResponse } from 'axios';
/* These tests act as validation criteria for the acceptance criteria for the client-side UI form generation of Questionnaires
with validation, type and error checking as defined in our documentation
https://github.com/TiagoF99/CSC302-Patient-Questionnaire/blob/main/Documentation/a3-features.md */
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
describe('test questionnaire requests', function () {
it('test get questionnaire called with correct params', async () => {
const mockedGet = jest.spyOn(axios, 'get').mockImplementation(() => Promise.resolve({ data: [] }));
getQuestionnaire('dummy');
expect(mockedGet).toBeCalledWith('/api/questionnaire/dummy');
expect(axios.get).toHaveBeenCalledTimes(1);
});
it('test get questionnaire returns correct object', async () => {
var questionnaire = {
title: 'dummy title',
description: 'dummy desc',
id: '1',
item: [
{
linkId: '11',
text: 'text item',
type: 'string',
},
],
};
const mockedGet = jest.spyOn(axios, 'get').mockImplementation(() => Promise.resolve({ data: questionnaire }));
const data = await getQuestionnaire('dummy');
expect(data).toEqual({
description: 'dummy desc',
id: '1',
item: [
{
linkId: '11',
text: 'text item',
type: 'string',
},
],
title: 'dummy title',
});
});
it('test get questionnaire error is correct with default value', async () => {
const mockedGet = jest.spyOn(axios, 'get').mockRejectedValue(new Error('error'));
const data = await getQuestionnaire('dummy');
expect(data).toEqual({
description: '',
id: '',
item: [],
title: '',
});
});
it('test post questionnaire called with correct params', async () => {
const mockedGet = jest.spyOn(axios, 'post').mockImplementation(() => Promise.resolve({ data: [] }));
postQuestionnaire('id', {});
expect(mockedGet).toBeCalledWith('/api/questionnaire/id', {});
expect(axios.post).toHaveBeenCalledTimes(1);
});
it('test post questionnaire returns correct result', async () => {
var questionnaire = {
title: 'dummy title',
description: 'dummy desc',
id: '1',
item: [
{
linkId: '11',
text: 'text item',
type: 'string',
},
],
};
const mockedGet = jest.spyOn(axios, 'post').mockImplementation(() => Promise.resolve({ data: questionnaire }));
const res = await postQuestionnaire('id', {});
expect(mockedGet).toBeCalledWith('/api/questionnaire/id', {});
expect(axios.post).toHaveBeenCalledTimes(2);
expect(res.data.title).toEqual('dummy title');
expect(res.data.id).toEqual('1');
});
});