forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crumbs.spec.ts
243 lines (193 loc) · 6.72 KB
/
crumbs.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import expect = require('expect.js');
import {
DocumentManager, IDocumentManager
} from '@jupyterlab/docmanager';
import {
DocumentRegistry, IDocumentRegistry
} from '@jupyterlab/docregistry';
import {
ServiceManager
} from '@jupyterlab/services';
import {
Message, MessageLoop
} from '@phosphor/messaging';
import {
Widget
} from '@phosphor/widgets';
import {
simulate
} from 'simulate-event';
import {
BreadCrumbs, FileBrowserModel
} from '@jupyterlab/filebrowser';
const ITEM_CLASS = 'jp-BreadCrumbs-item';
class LogCrumbs extends BreadCrumbs {
methods: string[] = [];
events: string[] = [];
handleEvent(event: Event): void {
super.handleEvent(event);
this.events.push(event.type);
}
protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
this.methods.push('onAfterAttach');
}
protected onBeforeDetach(msg: Message): void {
super.onBeforeDetach(msg);
this.methods.push('onBeforeDetach');
}
protected onUpdateRequest(msg: Message): void {
super.onUpdateRequest(msg);
this.methods.push('onUpdateRequest');
}
}
describe('filebrowser/model', () => {
let manager: IDocumentManager;
let serviceManager: ServiceManager.IManager;
let registry: IDocumentRegistry;
let model: FileBrowserModel;
let crumbs: LogCrumbs;
let first: string;
let second: string;
let third: string;
let path: string;
before((done) => {
let opener: DocumentManager.IWidgetOpener = {
open: widget => { /* no op */ }
};
registry = new DocumentRegistry();
serviceManager = new ServiceManager();
manager = new DocumentManager({
registry, opener,
manager: serviceManager
});
let contents = serviceManager.contents;
contents.newUntitled({ type: 'directory' }).then(cModel => {
first = cModel.name;
return contents.newUntitled({ path: cModel.path, type: 'directory' });
}).then(cModel => {
second = cModel.name;
return contents.newUntitled({ path: cModel.path, type: 'directory' });
}).then(cModel => {
third = cModel.name;
path = cModel.path;
}).then(done, done);
});
beforeEach((done) => {
model = new FileBrowserModel({ manager });
model.cd(path).then(() => {
crumbs = new LogCrumbs({ model });
}).then(done, done);
});
afterEach(() => {
model.dispose();
});
describe('BreadCrumbs', () => {
describe('#constructor()', () => {
it('should create a new BreadCrumbs instance', () => {
let bread = new BreadCrumbs({ model });
expect(bread).to.be.a(BreadCrumbs);
let items = crumbs.node.getElementsByClassName(ITEM_CLASS);
expect(items.length).to.be(1);
});
it('should add the jp-BreadCrumbs class', () => {
expect(crumbs.hasClass('jp-BreadCrumbs')).to.be(true);
});
});
describe('#handleEvent()', () => {
context('click', () => {
it('should switch to the parent directory', (done) => {
Widget.attach(crumbs, document.body);
MessageLoop.sendMessage(crumbs, Widget.Msg.UpdateRequest);
let items = crumbs.node.getElementsByClassName(ITEM_CLASS);
expect(items.length).to.be(4);
model.pathChanged.connect(() => {
MessageLoop.sendMessage(crumbs, Widget.Msg.UpdateRequest);
items = crumbs.node.getElementsByClassName(ITEM_CLASS);
expect(items.length).to.be(3);
done();
});
expect(items[2].textContent).to.be(second);
simulate(items[2], 'click');
});
it('should switch to the home directory', (done) => {
Widget.attach(crumbs, document.body);
MessageLoop.sendMessage(crumbs, Widget.Msg.UpdateRequest);
let items = crumbs.node.getElementsByClassName(ITEM_CLASS);
model.pathChanged.connect(() => {
MessageLoop.sendMessage(crumbs, Widget.Msg.UpdateRequest);
items = crumbs.node.getElementsByClassName(ITEM_CLASS);
expect(items.length).to.be(1);
expect(model.path).to.be('');
done();
});
simulate(items[0], 'click');
});
it('should switch to the grandparent directory', (done) => {
Widget.attach(crumbs, document.body);
MessageLoop.sendMessage(crumbs, Widget.Msg.UpdateRequest);
let items = crumbs.node.getElementsByClassName(ITEM_CLASS);
model.pathChanged.connect(() => {
MessageLoop.sendMessage(crumbs, Widget.Msg.UpdateRequest);
items = crumbs.node.getElementsByClassName(ITEM_CLASS);
expect(items.length).to.be(2);
expect(model.path).to.be(first);
done();
});
simulate(items[1], 'click');
});
it('should refresh the current directory', (done) => {
Widget.attach(crumbs, document.body);
MessageLoop.sendMessage(crumbs, Widget.Msg.UpdateRequest);
let items = crumbs.node.getElementsByClassName(ITEM_CLASS);
model.refreshed.connect(() => {
MessageLoop.sendMessage(crumbs, Widget.Msg.UpdateRequest);
items = crumbs.node.getElementsByClassName(ITEM_CLASS);
expect(items.length).to.be(4);
expect(model.path).to.be(path);
done();
});
expect(items[3].textContent).to.be(third);
simulate(items[3], 'click');
});
});
});
describe('#onAfterAttach()', () => {
it('should post an update request', (done) => {
Widget.attach(crumbs, document.body);
expect(crumbs.methods).to.contain('onAfterAttach');
requestAnimationFrame(() => {
expect(crumbs.methods).to.contain('onUpdateRequest');
done();
});
});
it('should add event listeners', () => {
Widget.attach(crumbs, document.body);
simulate(crumbs.node, 'click');
expect(crumbs.events).to.contain('click');
});
});
describe('#onBeforeDetach()', () => {
it('should remove event listeners', () => {
Widget.attach(crumbs, document.body);
Widget.detach(crumbs);
simulate(crumbs.node, 'click');
expect(crumbs.events).to.not.contain('click');
});
});
describe('#onUpdateRequest()', () => {
it('should be called when the model updates', (done) => {
model.cd('..').then(() => {
requestAnimationFrame(() => {
expect(crumbs.methods).to.contain('onUpdateRequest');
let items = crumbs.node.getElementsByClassName(ITEM_CLASS);
expect(items.length).to.be(3);
done();
});
}).catch(done);
});
});
});
});