Skip to content

Commit 0278d45

Browse files
committed
Rename test file and refactor
1 parent e20b011 commit 0278d45

File tree

1 file changed

+95
-73
lines changed

1 file changed

+95
-73
lines changed

tests/test-components/FileList.spec.tsx renamed to tests/test-components/GitPanel.spec.tsx

Lines changed: 95 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,91 @@
11
import * as apputils from '@jupyterlab/apputils';
22
import 'jest';
3-
import { FileList, IFileListProps } from '../../src/components/FileList';
4-
import { GitExtension } from '../../src/model';
3+
import { GitExtension as GitModel } from '../../src/model';
54
import * as git from '../../src/git';
5+
import { GitPanel, IGitSessionNodeProps } from '../../src/components/GitPanel';
66

77
jest.mock('../../src/git');
8-
9-
// Mock jupyterlab package
108
jest.mock('@jupyterlab/apputils');
119

12-
describe('FileList', () => {
13-
const props: IFileListProps = {
14-
model: null,
15-
renderMime: null,
16-
stagedFiles: [],
17-
unstagedFiles: [],
18-
untrackedFiles: [],
19-
settings: null
10+
function MockRequest(url: string, method: string, request: any) {
11+
let response: Response;
12+
let obj: any;
13+
switch (url) {
14+
case '/git/commit':
15+
response = new Response();
16+
break;
17+
case '/git/show_top_level':
18+
obj = {
19+
code: 0,
20+
top_repo_path: (request as any)['current_path']
21+
};
22+
response = new Response(JSON.stringify(obj));
23+
break;
24+
case '/git/server_root':
25+
obj = {
26+
server_root: '/foo'
27+
};
28+
response = new Response(JSON.stringify(obj));
29+
break;
30+
default:
31+
obj = {
32+
message: `No mock implementation for ${url}.`
33+
};
34+
response = new Response(JSON.stringify(obj), { status: 404 });
35+
}
36+
return Promise.resolve(response);
37+
}
38+
39+
/**
40+
* Returns a bare minimum "settings" object for use within the Git panel.
41+
*
42+
* @private
43+
* @returns mock settings
44+
*/
45+
function MockSettings() {
46+
return {
47+
changed: {
48+
connect: () => true,
49+
disconnect: () => true
50+
},
51+
composite: {}
2052
};
53+
}
2154

22-
describe('#commitAllStagedFiles()', () => {
23-
let fileList: FileList = null;
55+
describe('GitPanel', () => {
56+
describe('#commitStagedFiles()', () => {
57+
const props: IGitSessionNodeProps = {
58+
model: null,
59+
renderMime: null,
60+
settings: null
61+
};
2462

2563
beforeEach(async () => {
2664
jest.restoreAllMocks();
2765

28-
const fakePath = '/path/to/repo';
29-
const fakeRoot = '/foo';
30-
const mockGit = git as jest.Mocked<typeof git>;
31-
mockGit.httpGitRequest.mockImplementation((url, method, request) => {
32-
let response: Response;
33-
switch (url) {
34-
case '/git/commit':
35-
response = new Response();
36-
break;
37-
case '/git/show_top_level':
38-
response = new Response(
39-
JSON.stringify({
40-
code: 0,
41-
top_repo_path: (request as any)['current_path']
42-
})
43-
);
44-
break;
45-
case '/git/server_root':
46-
response = new Response(
47-
JSON.stringify({
48-
server_root: fakeRoot
49-
})
50-
);
51-
break;
52-
default:
53-
response = new Response(
54-
`{"message": "No mock implementation for ${url}."}`,
55-
{ status: 404 }
56-
);
57-
}
58-
return Promise.resolve(response);
59-
});
66+
const mock = git as jest.Mocked<typeof git>;
67+
mock.httpGitRequest.mockImplementation(MockRequest);
68+
6069
const app = {
6170
commands: {
6271
hasCommand: jest.fn().mockReturnValue(true)
6372
}
6473
};
65-
props.model = new GitExtension(app as any);
66-
props.model.pathRepository = fakePath;
74+
props.model = new GitModel(app as any);
75+
props.model.pathRepository = '/path/to/repo';
76+
77+
// @ts-ignore
78+
props.settings = MockSettings();
79+
6780
await props.model.ready;
68-
fileList = new FileList(props);
6981
});
7082

7183
it('should commit when commit message is provided', async () => {
72-
const spy = jest.spyOn(GitExtension.prototype, 'commit');
84+
const spy = jest.spyOn(GitModel.prototype, 'commit');
85+
7386
// Mock identity look up
7487
const identity = jest
75-
.spyOn(GitExtension.prototype, 'config')
88+
.spyOn(GitModel.prototype, 'config')
7689
.mockResolvedValue(
7790
new Response(
7891
JSON.stringify({
@@ -84,24 +97,28 @@ describe('FileList', () => {
8497
{ status: 201 }
8598
)
8699
);
87-
await fileList.commitAllStagedFiles('Initial commit');
100+
101+
const panel = new GitPanel(props);
102+
await panel.commitStagedFiles('Initial commit');
88103
expect(identity).toHaveBeenCalledTimes(1);
89104
expect(spy).toHaveBeenCalledTimes(1);
90105
expect(spy).toHaveBeenCalledWith('Initial commit');
91106
});
92107

93-
it('should NOT commit when commit message is empty', async () => {
94-
const spy = jest.spyOn(GitExtension.prototype, 'commit');
95-
await fileList.commitAllStagedFiles('');
108+
it('should not commit without a commit message', async () => {
109+
const spy = jest.spyOn(GitModel.prototype, 'commit');
110+
const panel = new GitPanel(props);
111+
await panel.commitStagedFiles('');
96112
expect(spy).not.toHaveBeenCalled();
97113
spy.mockRestore();
98114
});
99115

100-
it('should prompt for user identity if user.name is unset', async () => {
101-
const spy = jest.spyOn(GitExtension.prototype, 'commit');
116+
it('should prompt for user identity if user.name is not set', async () => {
117+
const spy = jest.spyOn(GitModel.prototype, 'commit');
118+
102119
// Mock identity look up
103120
const identity = jest
104-
.spyOn(GitExtension.prototype, 'config')
121+
.spyOn(GitModel.prototype, 'config')
105122
.mockImplementation(options => {
106123
let response: Response = null;
107124
if (options === undefined) {
@@ -118,8 +135,8 @@ describe('FileList', () => {
118135
}
119136
return Promise.resolve(response);
120137
});
121-
const mockApputils = apputils as jest.Mocked<typeof apputils>;
122-
mockApputils.showDialog.mockResolvedValue({
138+
const mock = apputils as jest.Mocked<typeof apputils>;
139+
mock.showDialog.mockResolvedValue({
123140
button: {
124141
accept: true,
125142
caption: '',
@@ -135,7 +152,8 @@ describe('FileList', () => {
135152
}
136153
});
137154

138-
await fileList.commitAllStagedFiles('Initial commit');
155+
const panel = new GitPanel(props);
156+
await panel.commitStagedFiles('Initial commit');
139157
expect(identity).toHaveBeenCalledTimes(2);
140158
expect(identity.mock.calls[0]).toHaveLength(0);
141159
expect(identity.mock.calls[1]).toEqual([
@@ -148,11 +166,12 @@ describe('FileList', () => {
148166
expect(spy).toHaveBeenCalledWith('Initial commit');
149167
});
150168

151-
it('should prompt for user identity if user.email is unset', async () => {
152-
const spy = jest.spyOn(GitExtension.prototype, 'commit');
169+
it('should prompt for user identity if user.email is not set', async () => {
170+
const spy = jest.spyOn(GitModel.prototype, 'commit');
171+
153172
// Mock identity look up
154173
const identity = jest
155-
.spyOn(GitExtension.prototype, 'config')
174+
.spyOn(GitModel.prototype, 'config')
156175
.mockImplementation(options => {
157176
let response: Response = null;
158177
if (options === undefined) {
@@ -169,8 +188,8 @@ describe('FileList', () => {
169188
}
170189
return Promise.resolve(response);
171190
});
172-
const mockApputils = apputils as jest.Mocked<typeof apputils>;
173-
mockApputils.showDialog.mockResolvedValue({
191+
const mock = apputils as jest.Mocked<typeof apputils>;
192+
mock.showDialog.mockResolvedValue({
174193
button: {
175194
accept: true,
176195
caption: '',
@@ -186,7 +205,8 @@ describe('FileList', () => {
186205
}
187206
});
188207

189-
await fileList.commitAllStagedFiles('Initial commit');
208+
const panel = new GitPanel(props);
209+
await panel.commitStagedFiles('Initial commit');
190210
expect(identity).toHaveBeenCalledTimes(2);
191211
expect(identity.mock.calls[0]).toHaveLength(0);
192212
expect(identity.mock.calls[1]).toEqual([
@@ -199,11 +219,12 @@ describe('FileList', () => {
199219
expect(spy).toHaveBeenCalledWith('Initial commit');
200220
});
201221

202-
it('should NOT commit if no user identity is set and the user reject the dialog', async () => {
203-
const spy = jest.spyOn(GitExtension.prototype, 'commit');
222+
it('should not commit if no user identity is set and the user rejects the dialog', async () => {
223+
const spy = jest.spyOn(GitModel.prototype, 'commit');
224+
204225
// Mock identity look up
205226
const identity = jest
206-
.spyOn(GitExtension.prototype, 'config')
227+
.spyOn(GitModel.prototype, 'config')
207228
.mockImplementation(options => {
208229
let response: Response = null;
209230
if (options === undefined) {
@@ -218,8 +239,8 @@ describe('FileList', () => {
218239
}
219240
return Promise.resolve(response);
220241
});
221-
const mockApputils = apputils as jest.Mocked<typeof apputils>;
222-
mockApputils.showDialog.mockResolvedValue({
242+
const mock = apputils as jest.Mocked<typeof apputils>;
243+
mock.showDialog.mockResolvedValue({
223244
button: {
224245
accept: false,
225246
caption: '',
@@ -232,7 +253,8 @@ describe('FileList', () => {
232253
value: null
233254
});
234255

235-
await fileList.commitAllStagedFiles('Initial commit');
256+
const panel = new GitPanel(props);
257+
await panel.commitStagedFiles('Initial commit');
236258
expect(identity).toHaveBeenCalledTimes(1);
237259
expect(identity).toHaveBeenCalledWith();
238260
expect(spy).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)