Skip to content

Commit 48df078

Browse files
committed
Start unit testing
1 parent 2317224 commit 48df078

File tree

11 files changed

+235
-51
lines changed

11 files changed

+235
-51
lines changed

.eslintignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@ node_modules
22
dist
33
coverage
44
**/*.d.ts
5+
<<<<<<< HEAD
56
tests
67
_temp_extension
8+
=======
9+
babel.config.js
10+
jest.config.js
11+
>>>>>>> Start unit testing
File renamed without changes.

jest.config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
roots: ['<rootDir>'],
5+
verbose: true,
6+
preset: 'ts-jest/presets/js-with-babel',
7+
// moduleNameMapper: {
8+
// '\\.(css|less|sass|scss)$': '<rootDir>/',
9+
// '\\.(gif|ttf|eot)$': '@jupyterlab/testutils/lib/jest-file-mock.js'
10+
// },
11+
transform: {
12+
// '\\.svg$': 'jest-raw-loader',
13+
// '^.+\\.md?$': 'markdown-loader-jest',
14+
'^.+\\.tsx?$': 'ts-jest'
15+
},
16+
setupFiles: ['<rootDir>/testutils/jest-setup-files.js'],
17+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
18+
testPathIgnorePatterns: ['/lib/', '/node_modules/'],
19+
coverageReporters: ['json', 'lcov', 'text', 'html'],
20+
coverageDirectory: path.join('./', 'coverage'),
21+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
22+
globals: {
23+
'ts-jest': {
24+
tsconfig: './tsconfig.test.json'
25+
}
26+
}
27+
};

jest.config.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
"eslint-plugin-prettier": "^3.1.2",
7676
"eslint-plugin-react": "^7.20.4",
7777
"husky": "^4.2.5",
78-
"jest": "^24",
78+
"jest": "^26.6.3",
79+
"jest-fetch-mock": "^3.0.3",
7980
"lint-staged": "^10.2.13",
8081
"mkdirp": "^1.0.3",
8182
"npm-run-all": "^4.1.5",
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// unit test
2+
import { CodeSnippetWidgetModel } from '../src/CodeSnippetWidgetModel';
3+
import { ICodeSnippet } from '../src/CodeSnippetContentsService';
4+
5+
const codeSnippets: ICodeSnippet[] = [
6+
{
7+
name: 'test_snippet',
8+
description: 'testing code snippet widget model',
9+
language: 'Python',
10+
code: ["print('hello world')"],
11+
id: 0,
12+
tags: ['test']
13+
},
14+
{
15+
name: 'test_snippet_two',
16+
description: 'testing code snippet widget model',
17+
language: 'Python',
18+
code: ["print('hello world again!')"],
19+
id: 1
20+
}
21+
];
22+
23+
const codeSnippetWidgetModel = new CodeSnippetWidgetModel(codeSnippets);
24+
25+
test('creates the code snippet widget model', () => {
26+
expect(codeSnippetWidgetModel).toBeInstanceOf(CodeSnippetWidgetModel);
27+
});
28+
29+
describe('test getter and setter', () => {
30+
it('test get snippet', () => {
31+
const snippets = codeSnippetWidgetModel.snippets;
32+
33+
expect(snippets).toBe(codeSnippets);
34+
});
35+
36+
it('test set snippet', () => {
37+
const newSnippets = [
38+
{
39+
name: 'test_snippet_one',
40+
description: 'testing code snippet widget model',
41+
language: 'Python',
42+
code: ["print('hello world')"],
43+
id: 0,
44+
tags: ['test']
45+
},
46+
{
47+
name: 'test_snippet_two',
48+
description: 'testing code snippet widget model',
49+
language: 'Python',
50+
code: ["print('hello world again!')"],
51+
id: 1
52+
}
53+
];
54+
codeSnippetWidgetModel.snippets = newSnippets;
55+
expect(codeSnippetWidgetModel.snippets).toBe(newSnippets);
56+
});
57+
});
58+
59+
// describe('test snippet manipulation', () => {
60+
// it('test add snippet', () => {
61+
// const newSnippet = {
62+
// name: 'test_snippet_three',
63+
// description: 'testing code snippet widget model',
64+
// language: 'Python',
65+
// code: ["print('testing')"],
66+
// id: 2
67+
// };
68+
// codeSnippetWidgetModel.addSnippet(newSnippet);
69+
// });
70+
// });

test/sample.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
test('two plus two is four', () => {
2+
expect(2 + 2).toBe(4);
3+
});
4+
5+
// mock function
6+
// const add = jest.fn();
7+
8+
// import { cleanup } from '@testing-library/react';
9+
// // import { CodeSnippetDisplay } from '../src/CodeSnippetDisplay';
10+
// // import { ICodeSnippet } from '../src/CodeSnippetContentsService';
11+
// import { JupyterFrontEnd } from '@jupyterlab/application';
12+
// import { Widget } from '@lumino/widgets';
13+
// import { IEditorServices } from '@jupyterlab/codeeditor';
14+
// import { CodeSnippetWidget } from '../src/CodeSnippetWidget';
15+
16+
// afterEach(cleanup);
17+
18+
// it('renders without crashing', () => {
19+
// let app: JupyterFrontEnd;
20+
// let editorServices: IEditorServices;
21+
// const getCurrentWidget = (): Widget => {
22+
// return app.shell.currentWidget;
23+
// };
24+
25+
// const codeSnippetWidget = new CodeSnippetWidget(
26+
// getCurrentWidget,
27+
// app,
28+
// editorServices
29+
// );
30+
31+
// expect(codeSnippetWidget).toBeInstanceOf(CodeSnippetWidget);
32+
// });
33+
34+
// const openCodeSnippetEditor = (args: ICodeSnippetEditorMetadata): void => {
35+
// // codeSnippetEditors are in the main area
36+
// const widgetId = `jp-codeSnippet-editor-${args.id}`;
37+
38+
// const openEditor = find(
39+
// app.shell.widgets('main'),
40+
// (widget: Widget, _: number) => {
41+
// return widget.id === widgetId;
42+
// }
43+
// );
44+
// if (openEditor) {
45+
// app.shell.activateById(widgetId);
46+
// return;
47+
// }
48+
49+
// const codeSnippetEditor = new CodeSnippetEditor(
50+
// contentsService,
51+
// editorServices,
52+
// tracker,
53+
// codeSnippetWidget,
54+
// args
55+
// );
56+
57+
// codeSnippetEditor.id = widgetId;
58+
// codeSnippetEditor.addClass(widgetId);
59+
// codeSnippetEditor.title.label =
60+
// args.name === ''
61+
// ? 'New Code Snippet'
62+
// : '[' + args.language + '] ' + args.name;
63+
// codeSnippetEditor.title.closable = true;
64+
// codeSnippetEditor.title.icon = editorIcon;
65+
66+
// if (!tracker.has(codeSnippetEditor)) {
67+
// tracker.add(codeSnippetEditor);
68+
// }
69+
70+
// if (!codeSnippetEditor.isAttached) {
71+
// app.shell.add(codeSnippetEditor, 'main', {
72+
// mode: 'tab-after'
73+
// });
74+
// }
75+
76+
// // Activate the code Snippet Editor
77+
// app.shell.activateById(codeSnippetEditor.id);
78+
// };
79+
80+
// it('renders without crashing', () => {
81+
82+
// const codeSnippet: ICodeSnippet = {
83+
// name: 'test',
84+
// description: 'testing CodeSnippetDisplay component',
85+
// id: 0,
86+
// language: 'Python',
87+
// code: []
88+
// }
89+
90+
// const test = new CodeSnippetDisplay()
91+
// odeSnippets={codeSnippets}
92+
// app={this.app}
93+
// getCurrentWidget={this.getCurrentWidget}
94+
// openCodeSnippetEditor={this.openCodeSnippetEditor.bind(this)}
95+
// editorServices={this.editorServices}
96+
// _codeSnippetWidgetModel={this._codeSnippetWidgetModel}
97+
// updateCodeSnippets={this.updateCodeSnippets}
98+
99+
// render(<CodeSnippetDisplay />)
100+
101+
// });

test/test.spec.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

testutils/jest-setup-files.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global.fetch = require('jest-fetch-mock');

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
"target": "es2017",
2121
"types": []
2222
},
23-
"include": ["src/*", "src/*.tsx"]
23+
"include": ["src/*", "src/*.tsx", "test/*"]
2424
}

0 commit comments

Comments
 (0)