Skip to content

Commit 66711dc

Browse files
author
Frederic Collonval
committed
Correct jest mocks
1 parent dec39b7 commit 66711dc

File tree

1 file changed

+61
-33
lines changed

1 file changed

+61
-33
lines changed

tests/test-components/BranchHeader.spec.tsx

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import {
1414
smallBranchStyle
1515
} from '../../src/componentsStyle/BranchHeaderStyle';
1616

17+
// Mock jupyterlab package
18+
jest.mock('@jupyterlab/apputils');
19+
1720
describe('BranchHeader', () => {
1821
let props: IBranchHeaderProps = {
1922
currentFileBrowserPath: '/current/absolute/path',
@@ -55,17 +58,16 @@ describe('BranchHeader', () => {
5558
it('should commit when commit message is provided', async () => {
5659
const spy = jest.spyOn(Git.prototype, 'commit');
5760
// Mock identity look up
58-
const identity = jest.spyOn(Git.prototype, 'config').mockImplementation(
59-
() =>
60-
new Response(
61-
JSON.stringify({
62-
options: {
63-
'user.name': 'John Snow',
64-
'user.email': '[email protected]'
65-
}
66-
}),
67-
{ status: 201 }
68-
)
61+
const identity = jest.spyOn(Git.prototype, 'config').mockResolvedValue(
62+
new Response(
63+
JSON.stringify({
64+
options: {
65+
'user.name': 'John Snow',
66+
'user.email': '[email protected]'
67+
}
68+
}),
69+
{ status: 201 }
70+
)
6971
);
7072
await branchHeader.commitAllStagedFiles(
7173
'Initial commit',
@@ -93,8 +95,9 @@ describe('BranchHeader', () => {
9395
const identity = jest
9496
.spyOn(Git.prototype, 'config')
9597
.mockImplementation((path, options) => {
98+
let response: Response = null;
9699
if (options === undefined) {
97-
return new Response(
100+
response = new Response(
98101
JSON.stringify({
99102
options: {
100103
'user.email': '[email protected]'
@@ -103,20 +106,26 @@ describe('BranchHeader', () => {
103106
{ status: 201 }
104107
);
105108
} else {
106-
return new Response('', { status: 201 });
109+
response = new Response('', { status: 201 });
107110
}
111+
return Promise.resolve(response);
108112
});
109-
jest.spyOn(apputils, 'showDialog').mockReturnValue(
110-
Promise.resolve({
111-
button: {
112-
accept: true
113-
},
114-
value: {
115-
name: 'John Snow',
116-
117-
}
118-
})
119-
);
113+
const mockApputils = apputils as jest.Mocked<typeof apputils>;
114+
mockApputils.showDialog.mockResolvedValue({
115+
button: {
116+
accept: true,
117+
caption: '',
118+
className: '',
119+
displayType: 'default',
120+
iconClass: '',
121+
iconLabel: '',
122+
label: ''
123+
},
124+
value: {
125+
name: 'John Snow',
126+
127+
}
128+
});
120129

121130
await branchHeader.commitAllStagedFiles(
122131
'Initial commit',
@@ -145,8 +154,9 @@ describe('BranchHeader', () => {
145154
const identity = jest
146155
.spyOn(Git.prototype, 'config')
147156
.mockImplementation((path, options) => {
157+
let response: Response = null;
148158
if (options === undefined) {
149-
return new Response(
159+
response = new Response(
150160
JSON.stringify({
151161
options: {
152162
'user.name': 'John Snow'
@@ -155,12 +165,20 @@ describe('BranchHeader', () => {
155165
{ status: 201 }
156166
);
157167
} else {
158-
return new Response('', { status: 201 });
168+
response = new Response('', { status: 201 });
159169
}
170+
return Promise.resolve(response);
160171
});
161-
jest.spyOn(apputils, 'showDialog').mockReturnValue({
172+
const mockApputils = apputils as jest.Mocked<typeof apputils>;
173+
mockApputils.showDialog.mockResolvedValue({
162174
button: {
163-
accept: true
175+
accept: true,
176+
caption: '',
177+
className: '',
178+
displayType: 'default',
179+
iconClass: '',
180+
iconLabel: '',
181+
label: ''
164182
},
165183
value: {
166184
name: 'John Snow',
@@ -195,21 +213,31 @@ describe('BranchHeader', () => {
195213
const identity = jest
196214
.spyOn(Git.prototype, 'config')
197215
.mockImplementation((path, options) => {
216+
let response: Response = null;
198217
if (options === undefined) {
199-
return new Response(
218+
response = new Response(
200219
JSON.stringify({
201220
options: {}
202221
}),
203222
{ status: 201 }
204223
);
205224
} else {
206-
return new Response('', { status: 201 });
225+
response = new Response('', { status: 201 });
207226
}
227+
return Promise.resolve(response);
208228
});
209-
jest.spyOn(apputils, 'showDialog').mockReturnValue({
229+
const mockApputils = apputils as jest.Mocked<typeof apputils>;
230+
mockApputils.showDialog.mockResolvedValue({
210231
button: {
211-
accept: false
212-
}
232+
accept: false,
233+
caption: '',
234+
className: '',
235+
displayType: 'default',
236+
iconClass: '',
237+
iconLabel: '',
238+
label: ''
239+
},
240+
value: null
213241
});
214242

215243
await branchHeader.commitAllStagedFiles(

0 commit comments

Comments
 (0)