forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgetfactory.spec.ts
151 lines (128 loc) · 5 KB
/
widgetfactory.spec.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { expect } from 'chai';
import { toArray } from '@lumino/algorithm';
import { ToolbarButton } from '@jupyterlab/apputils';
import { DocumentRegistry, Context } from '@jupyterlab/docregistry';
import {
INotebookModel,
NotebookPanel,
NotebookWidgetFactory
} from '@jupyterlab/notebook';
import { initNotebookContext, NBTestUtils } from '@jupyterlab/testutils';
const contentFactory = NBTestUtils.createNotebookPanelFactory();
const rendermime = NBTestUtils.defaultRenderMime();
function createFactory(
toolbarFactory?: (widget: NotebookPanel) => DocumentRegistry.IToolbarItem[]
): NotebookWidgetFactory {
return new NotebookWidgetFactory({
name: 'notebook',
fileTypes: ['notebook'],
rendermime,
toolbarFactory,
contentFactory,
mimeTypeService: NBTestUtils.mimeTypeService,
editorConfig: NBTestUtils.defaultEditorConfig
});
}
describe('@jupyterlab/notebook', () => {
describe('NotebookWidgetFactory', () => {
let context: Context<INotebookModel>;
beforeEach(async () => {
context = await initNotebookContext();
});
afterEach(() => {
context.dispose();
});
describe('#constructor()', () => {
it('should create a notebook widget factory', () => {
const factory = createFactory();
expect(factory).to.be.an.instanceof(NotebookWidgetFactory);
});
});
describe('#isDisposed', () => {
it('should get whether the factory has been disposed', () => {
const factory = createFactory();
expect(factory.isDisposed).to.equal(false);
factory.dispose();
expect(factory.isDisposed).to.equal(true);
});
});
describe('#dispose()', () => {
it('should dispose of the resources held by the factory', () => {
const factory = createFactory();
factory.dispose();
expect(factory.isDisposed).to.equal(true);
});
it('should be safe to call multiple times', () => {
const factory = createFactory();
factory.dispose();
factory.dispose();
expect(factory.isDisposed).to.equal(true);
});
});
describe('#editorConfig', () => {
it('should be the editor config passed into the constructor', () => {
const factory = createFactory();
expect(factory.editorConfig).to.equal(NBTestUtils.defaultEditorConfig);
});
it('should be settable', () => {
const factory = createFactory();
const newConfig = { ...NBTestUtils.defaultEditorConfig };
factory.editorConfig = newConfig;
expect(factory.editorConfig).to.equal(newConfig);
});
});
describe('#createNew()', () => {
it('should create a new `NotebookPanel` widget', () => {
const factory = createFactory();
const panel = factory.createNew(context);
expect(panel).to.be.an.instanceof(NotebookPanel);
});
it('should create a clone of the rendermime', () => {
const factory = createFactory();
const panel = factory.createNew(context);
expect(panel.content.rendermime).to.not.equal(rendermime);
});
it('should pass the editor config to the notebook', () => {
const factory = createFactory();
const panel = factory.createNew(context);
expect(panel.content.editorConfig).to.equal(
NBTestUtils.defaultEditorConfig
);
});
it('should populate the default toolbar items', () => {
const factory = createFactory();
const panel = factory.createNew(context);
const items = toArray(panel.toolbar.names());
expect(items).to.contain('save');
expect(items).to.contain('restart');
expect(items).to.contain('kernelStatus');
});
it('should populate the customized toolbar items', () => {
const toolbarFactory = () => [
{ name: 'foo', widget: new ToolbarButton() },
{ name: 'bar', widget: new ToolbarButton() }
];
const factory = createFactory(toolbarFactory);
const panel = factory.createNew(context);
const panel2 = factory.createNew(context);
expect(toArray(panel.toolbar.names())).to.deep.equal(['foo', 'bar']);
expect(toArray(panel2.toolbar.names())).to.deep.equal(['foo', 'bar']);
expect(toArray(panel.toolbar.children()).length).to.equal(2);
expect(toArray(panel2.toolbar.children()).length).to.equal(2);
});
it('should clone from the optional source widget', () => {
const factory = createFactory();
const panel = factory.createNew(context);
const clone = factory.createNew(panel.context, panel);
expect(clone).to.be.an.instanceof(NotebookPanel);
expect(clone.content.rendermime).to.equal(panel.content.rendermime);
expect(clone.content.editorConfig).to.equal(panel.content.editorConfig);
expect(clone.content.notebookConfig).to.equal(
panel.content.notebookConfig
);
});
});
});
});