forked from aurelia/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaurelia.spec.js
330 lines (271 loc) · 12 KB
/
aurelia.spec.js
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { Container } from 'aurelia-dependency-injection';
import { Loader } from 'aurelia-loader';
import { DOM, PLATFORM } from 'aurelia-pal';
import { BindingLanguage, ViewResources, ViewSlot, inlineView } from 'aurelia-templating';
import { Aurelia } from '../src/aurelia';
import { FrameworkConfiguration } from '../src/framework-configuration';
import './setup';
describe('aurelia', () => {
describe("constructor", () => {
it("should have good defaults", () => {
let mockLoader = {};
PLATFORM.Loader = function(){
return mockLoader;
}
let aurelia = new Aurelia();
expect(aurelia.loader).toBe(mockLoader);
expect(aurelia.container).toEqual(jasmine.any(Container));
expect(aurelia.resources).toEqual(jasmine.any(ViewResources));
expect(aurelia.use).toEqual(jasmine.any(FrameworkConfiguration));
expect(aurelia.started).toBeFalsy();
});
it("will take in a loader, container and resource registry", () => {
let mockLoader = jasmine.createSpy('loader');
let mockResources = jasmine.createSpy('viewResources');
let mockContainer = jasmine.createSpyObj('container', ['registerInstance', 'makeGlobal']);
let aurelia = new Aurelia(mockLoader, mockContainer, mockResources);
expect(aurelia.loader).toBe(mockLoader);
expect(aurelia.container).toBe(mockContainer);
expect(aurelia.resources).toBe(mockResources);
expect(aurelia.use).toEqual(jasmine.any(FrameworkConfiguration));
expect(aurelia.started).toBeFalsy();
//Lets check the container was called
expect(mockContainer.registerInstance).toHaveBeenCalledWith(Aurelia, aurelia);
expect(mockContainer.registerInstance).toHaveBeenCalledWith(Loader, mockLoader);
expect(mockContainer.registerInstance).toHaveBeenCalledWith(ViewResources, mockResources);
});
});
describe('start()', () => {
let aurelia, mockContainer, mockLoader, mockResources, mockPlugin, mockViewEngine;
beforeEach(() => {
mockLoader = jasmine.createSpy('loader');
mockResources = jasmine.createSpy('viewResources');
mockViewEngine = jasmine.createSpyObj("viewEngine", ["importViewResources"]);
mockViewEngine.importViewResources.and.returnValue(new Promise((resolve, error) => {
resolve();
}));
mockContainer = jasmine.createSpyObj('container', ['registerInstance', 'hasResolver', 'get', 'makeGlobal']);
mockContainer.hasResolver.and.returnValue(true);
mockContainer.get.and.returnValue(mockViewEngine);
mockPlugin = jasmine.createSpyObj('plugin', ['apply']);
mockPlugin.apply.and.returnValue(new Promise((resolve, error) => {
resolve();
}));
aurelia = new Aurelia(mockLoader, mockContainer, mockResources);
aurelia.use = mockPlugin;
});
it("will return if it's already started", (done) => {
aurelia.started = true;
aurelia.start()
.catch((reason) => expect(true).toBeFalsy(reason))
.then(done);
});
it("will fail if the plugin loader fails", (done) => {
mockPlugin.apply.and.returnValue(new Promise((resolve, error) => {
error();
}));
aurelia.start()
.then(() => expect(true).toBeFalsy("Startup should have failed"))
.catch(() => expect(mockPlugin.apply).toHaveBeenCalled())
.then(done);
});
//I'm going to assume start should fail in this case.
it("should check for a binding language and log an error if one is not set", (done) => {
mockContainer.hasResolver.and.returnValue(false);
aurelia.start()
.then(() => expect(true).toBeFalsy("Should have not started up"))
.catch(() => expect(mockContainer.hasResolver).toHaveBeenCalledWith(BindingLanguage))
.then(done);
});
it("should fire a custom event when started", (done) => {
var documentSpy = spyOn(document, "dispatchEvent").and.callThrough();
aurelia.start()
.then((result) => {
expect(result).toBe(aurelia);
expect(documentSpy).toHaveBeenCalled();
var event = documentSpy.calls.mostRecent().args[0];
expect(event).toEqual(jasmine.any(window.Event));
expect(event.type).toEqual("aurelia-started");
})
.catch(() => expect(true).toBeFalsy("Starting shouldn't have failed"))
.then(done);
});
});
describe('setRoot()', () => {
let aurelia, mockContainer, mockLoader, mockCompositionEngine, rootModel, composePromise, composeListener;
beforeEach(() => {
mockLoader = jasmine.createSpy("loader");
mockContainer = jasmine.createSpyObj("container", ["get", "registerInstance", 'makeGlobal']);
mockCompositionEngine = jasmine.createSpyObj("compositionEngine", ["compose"]);
rootModel = {};
composePromise = new Promise((resolve, error) => {
resolve(rootModel)
});
mockContainer.get.and.returnValue(mockCompositionEngine);
mockCompositionEngine.compose.and.returnValue(composePromise);
aurelia = new Aurelia(mockLoader, mockContainer);
});
afterEach(() => {
delete document.body.aurelia;
if (composeListener) {
document.removeEventListener("aurelia-composed", composeListener);
}
});
it("should try and find the element with an id of applicationHost if one is not supplied", (done) => {
let documentSpy = spyOn(document, "getElementById").and.returnValue(document.body);
aurelia.setRoot(rootModel)
.then((result) => {
expect(result).toBe(aurelia);
expect(aurelia.host).toBe(document.body);
expect(document.body.aurelia).toBe(aurelia);
expect(documentSpy).toHaveBeenCalledWith("applicationHost");
})
.catch((reason) => expect(false).toBeTruthy(reason))
.then(done);
});
it("should use the applicationHost if it's not a string as the host", (done) => {
//This wouldn't have succeeded because registerInstance checks the type
//But the function doesn't guard against applicationHost so this test is valid
let host = { firstChild:{} };
aurelia.setRoot(rootModel, host)
.then((result) => {
expect(result).toBe(aurelia);
expect(aurelia.host).toBe(host);
expect(host.aurelia).toBe(aurelia);
})
.catch((reason) => expect(false).toBeTruthy(reason))
.then(done);
});
it("should call the compose function of the composition instance with a well formed instruction", (done) => {
let attachedSpy;
let documentSpy = spyOn(document, "getElementById").and.returnValue(document.body);
mockCompositionEngine.compose.and.callFake((instruction) => {
attachedSpy = spyOn(instruction.viewSlot, 'attached');
return composePromise;
});
aurelia.setRoot(rootModel)
.then((result) => {
expect(result).toBe(aurelia);
expect(mockCompositionEngine.compose).toHaveBeenCalled();
let instruction = mockCompositionEngine.compose.calls.mostRecent().args[0];
expect(instruction.viewModel).toBe(rootModel);
expect(instruction.container).toBe(mockContainer);
expect(instruction.childContainer).toBe(mockContainer);
expect(instruction.viewSlot).toEqual(jasmine.any(ViewSlot));
})
.catch((reason) => expect(false).toBeTruthy(reason))
.then(done);
});
it("should fire a custom aurelia-composed event when it's done", (done) => {
let documentSpy = spyOn(document, "getElementById").and.returnValue(document.body);
composeListener = (event) => {
expect(event).toEqual(jasmine.any(window.Event));
expect(event.type).toEqual("aurelia-composed");
done();
};
//Can't do the same trick with aurelia-start because it waits till after the promise is resolved to fire the event
document.addEventListener("aurelia-composed", composeListener);
aurelia.setRoot(rootModel)
.catch((reason) => {
expect(false).toBeTruthy(reason);
done();
});
});
it('should accept view model class as root', (done) => {
const emptyMetadata = Object.freeze({});
const metadataContainerKey = '__metadata__';
Reflect.getOwnMetadata = function(metadataKey, target, targetKey) {
if (target.hasOwnProperty(metadataContainerKey)) {
return (target[metadataContainerKey][targetKey] || emptyMetadata)[metadataKey];
}
};
Reflect.defineMetadata = function(metadataKey, metadataValue, target, targetKey) {
let metadataContainer = target.hasOwnProperty(metadataContainerKey) ? target[metadataContainerKey] : (target[metadataContainerKey] = {});
let targetContainer = metadataContainer[targetKey] || (metadataContainer[targetKey] = {});
targetContainer[metadataKey] = metadataValue;
};
Reflect.metadata = function(metadataKey, metadataValue) {
return function(target, targetKey) {
Reflect.defineMetadata(metadataKey, metadataValue, target, targetKey);
};
};
let documentSpy = spyOn(document, "getElementById").and.returnValue(document.body);
@inlineView('<template>Hello</template>')
class App {}
aurelia = new Aurelia({});
aurelia.use.instance(BindingLanguage, {
inspectTextContent() {
return null;
}
})
aurelia.setRoot(App)
.then(aurelia => {
expect(documentSpy).toHaveBeenCalledWith("applicationHost");
expect(aurelia.root.viewModel.constructor).toBe(App);
})
.catch((ex) => {
expect(ex).toBeFalsy("It should have composed");
})
.then(() => {
Reflect.getOwnMetadata = null;
Reflect.defineMetadata = null;
Reflect.metadata = null;
done();
});
});
});
describe('enhance()', () => {
let aurelia, mockContainer, mockLoader, mockResources, mockPlugin, mockViewEngine, mockTemplatingEngine;
let rootStub = {
attached() {}
};
beforeEach(() => {
mockLoader = jasmine.createSpy('loader');
mockViewEngine = jasmine.createSpyObj("viewEngine", ['importViewResources', 'enhance']);
mockViewEngine.importViewResources.and.returnValue(new Promise((resolve, error) => {
resolve();
}));
mockViewEngine.enhance.and.returnValue(rootStub);
mockContainer = jasmine.createSpyObj('container', ['registerInstance', 'hasResolver', 'get', 'makeGlobal']);
mockContainer.hasResolver.and.returnValue(true);
mockContainer.get.and.returnValue(mockViewEngine);
mockResources = jasmine.createSpy('viewResources');
mockPlugin = jasmine.createSpyObj('plugin', ['apply']);
mockPlugin.apply.and.returnValue(new Promise((resolve, error) => {
resolve();
}));
aurelia = new Aurelia(mockLoader, mockContainer, mockResources);
aurelia.use = mockPlugin;
});
describe('when passing in no arguments', () => {
let result;
it('configures body as host', () => {
let documentSpy = spyOn(document, "querySelectorAll").and.returnValue([document.body]);
spyOn(aurelia, '_configureHost');
result = aurelia.enhance();
expect(aurelia._configureHost).toHaveBeenCalledWith(document.body);
});
});
describe('when passing in bindingContext and string for Id', () => {
let result;
it('configures body as host', () => {
let elId = 'Testing';
let fakeElement = DOM.createElement('div');
fakeElement.setAttribute('id', elId);
let documentSpy = spyOn(document, "getElementById").and.returnValue(fakeElement);
result = aurelia.enhance({}, elId);
expect(aurelia.host).toBe(fakeElement);
});
});
describe('when passing in bindingContext and an element', () => {
let result;
it('configures body as host', () => {
let elId = 'Testing';
let fakeElement = DOM.createElement('div');
fakeElement.setAttribute('id', fakeElement);
result = aurelia.enhance({}, fakeElement);
expect(aurelia.host).toBe(fakeElement);
});
});
});
});