Skip to content

Commit c391b47

Browse files
committed
Fix jest tests
1 parent f437bcb commit c391b47

File tree

2 files changed

+72
-45
lines changed

2 files changed

+72
-45
lines changed

src/gitMenuCommands.ts

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,41 @@ export function addCommands(
177177
}
178178
});
179179

180+
/** Add git push command */
181+
commands.addCommand(CommandIDs.gitPush, {
182+
label: 'Push to Remote',
183+
caption: 'Push code to remote repository',
184+
isEnabled: () => model.pathRepository !== null,
185+
execute: async () => {
186+
await Private.showGitOperationDialog(model, Operation.Push).catch(
187+
reason => {
188+
console.error(
189+
`Encountered an error when pushing changes. Error: ${reason}`
190+
);
191+
}
192+
);
193+
}
194+
});
195+
196+
/** Add git pull command */
197+
commands.addCommand(CommandIDs.gitPull, {
198+
label: 'Pull from Remote',
199+
caption: 'Pull latest code from remote repository',
200+
isEnabled: () => model.pathRepository !== null,
201+
execute: async () => {
202+
await Private.showGitOperationDialog(model, Operation.Pull).catch(
203+
reason => {
204+
console.error(
205+
`Encountered an error when pulling changes. Error: ${reason}`
206+
);
207+
}
208+
);
209+
}
210+
});
211+
}
212+
213+
/* eslint-disable no-inner-declarations */
214+
namespace Private {
180215
/**
181216
* Displays an error dialog when a Git operation fails.
182217
*
@@ -185,7 +220,7 @@ export function addCommands(
185220
* @param operation - Git operation name
186221
* @returns Promise for displaying a dialog
187222
*/
188-
async function showGitOperationDialog(
223+
export async function showGitOperationDialog(
189224
model: IGitExtension,
190225
operation: Operation
191226
): Promise<void> {
@@ -218,32 +253,5 @@ export function addCommands(
218253
retry = true;
219254
}
220255
}
221-
222-
/** Add git push command */
223-
commands.addCommand(CommandIDs.gitPush, {
224-
label: 'Push to Remote',
225-
caption: 'Push code to remote repository',
226-
isEnabled: () => model.pathRepository !== null,
227-
execute: async () => {
228-
await showGitOperationDialog(model, Operation.Push).catch(reason => {
229-
console.error(
230-
`Encountered an error when pushing changes. Error: ${reason}`
231-
);
232-
});
233-
}
234-
});
235-
236-
/** Add git pull command */
237-
commands.addCommand(CommandIDs.gitPull, {
238-
label: 'Pull from Remote',
239-
caption: 'Pull latest code from remote repository',
240-
isEnabled: () => model.pathRepository !== null,
241-
execute: async () => {
242-
await showGitOperationDialog(model, Operation.Pull).catch(reason => {
243-
console.error(
244-
`Encountered an error when pulling changes. Error: ${reason}`
245-
);
246-
});
247-
}
248-
});
249256
}
257+
/* eslint-enable no-inner-declarations */

tests/test-components/Toolbar.spec.tsx

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import { refreshIcon } from '@jupyterlab/ui-components';
2+
import * as commands from '@lumino/commands';
23
import { shallow } from 'enzyme';
34
import 'jest';
45
import * as React from 'react';
56
import { ActionButton } from '../../src/components/ActionButton';
67
import { Toolbar } from '../../src/components/Toolbar';
78
import * as git from '../../src/git';
9+
import { CommandIDs } from '../../src/gitMenuCommands';
810
import { GitExtension } from '../../src/model';
911
import { pullIcon, pushIcon } from '../../src/style/icons';
1012
import {
1113
toolbarButtonClass,
1214
toolbarMenuButtonClass
1315
} from '../../src/style/Toolbar';
1416

17+
jest.mock('@lumino/commands');
1518
jest.mock('../../src/git');
1619

17-
async function createModel() {
18-
const model = new GitExtension();
20+
async function createModel(commands?: commands.CommandRegistry) {
21+
const app = {
22+
commands,
23+
shell: null as any
24+
};
25+
const model = new GitExtension(app as any);
1926

2027
jest.spyOn(model, 'currentBranch', 'get').mockReturnValue({
2128
is_current_branch: true,
@@ -303,17 +310,25 @@ describe('Toolbar', () => {
303310

304311
describe('pull changes', () => {
305312
let model: GitExtension;
313+
let mockedExecute: any;
306314

307315
beforeEach(async () => {
316+
const mockedCommands = commands as jest.Mocked<typeof commands>;
317+
mockedExecute = jest.fn();
318+
mockedCommands.CommandRegistry.mockImplementation(() => {
319+
return {
320+
execute: mockedExecute
321+
} as any;
322+
});
323+
const registry = new commands.CommandRegistry();
324+
308325
const mock = git as jest.Mocked<typeof git>;
309326
mock.httpGitRequest.mockImplementation(request);
310327

311-
model = await createModel();
328+
model = await createModel(registry);
312329
});
313330

314331
it('should pull changes when the button to pull the latest changes is clicked', () => {
315-
const spy = jest.spyOn(GitExtension.prototype, 'pull');
316-
317332
const props = {
318333
model: model,
319334
branching: false,
@@ -323,26 +338,32 @@ describe('Toolbar', () => {
323338
const button = node.find(`.${toolbarButtonClass}`).first();
324339

325340
button.simulate('click');
326-
expect(spy).toHaveBeenCalledTimes(1);
327-
expect(spy).toHaveBeenCalledWith(undefined);
328-
329-
spy.mockRestore();
341+
expect(mockedExecute).toHaveBeenCalledTimes(1);
342+
expect(mockedExecute).toHaveBeenCalledWith(CommandIDs.gitPull);
330343
});
331344
});
332345

333346
describe('push changes', () => {
334347
let model: GitExtension;
348+
let mockedExecute: any;
335349

336350
beforeEach(async () => {
351+
const mockedCommands = commands as jest.Mocked<typeof commands>;
352+
mockedExecute = jest.fn();
353+
mockedCommands.CommandRegistry.mockImplementation(() => {
354+
return {
355+
execute: mockedExecute
356+
} as any;
357+
});
358+
const registry = new commands.CommandRegistry();
359+
337360
const mock = git as jest.Mocked<typeof git>;
338361
mock.httpGitRequest.mockImplementation(request);
339362

340-
model = await createModel();
363+
model = await createModel(registry);
341364
});
342365

343366
it('should push changes when the button to push the latest changes is clicked', () => {
344-
const spy = jest.spyOn(GitExtension.prototype, 'push');
345-
346367
const props = {
347368
model: model,
348369
branching: false,
@@ -352,10 +373,8 @@ describe('Toolbar', () => {
352373
const button = node.find(`.${toolbarButtonClass}`).at(1);
353374

354375
button.simulate('click');
355-
expect(spy).toHaveBeenCalledTimes(1);
356-
expect(spy).toHaveBeenCalledWith(undefined);
357-
358-
spy.mockRestore();
376+
expect(mockedExecute).toHaveBeenCalledTimes(1);
377+
expect(mockedExecute).toHaveBeenCalledWith(CommandIDs.gitPush);
359378
});
360379
});
361380

0 commit comments

Comments
 (0)