forked from glimmerjs/glimmer-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanagers-test.ts
413 lines (330 loc) · 12.5 KB
/
managers-test.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import type {
ComponentManager,
HelperManager,
InternalComponentManager,
InternalHelperManager,
ModifierManager,
} from '@glimmer/interfaces';
import type { CustomHelperManager } from '@glimmer/manager';
import {
componentCapabilities,
CustomComponentManager,
CustomModifierManager,
getInternalComponentManager,
getInternalHelperManager,
getInternalModifierManager,
helperCapabilities,
modifierCapabilities,
setComponentManager,
setHelperManager,
setInternalComponentManager,
setInternalHelperManager,
setInternalModifierManager,
setModifierManager,
} from '@glimmer/manager';
import { UNDEFINED_REFERENCE } from '@glimmer/reference';
import { createUpdatableTag } from '@glimmer/validator';
const { module, test } = QUnit;
module('Managers', () => {
module('Component', () => {
test('it works', (assert) => {
class CustomManager implements ComponentManager<unknown> {
capabilities = componentCapabilities('3.13');
constructor(public owner: object | undefined) {}
createComponent() {}
getContext() {}
}
let factory = (owner: object) => new CustomManager(owner);
let definition = setComponentManager(factory, {});
let instance = getInternalComponentManager(definition) as CustomComponentManager<
object,
unknown
>;
assert.ok(instance instanceof CustomComponentManager, 'internal manager is a custom manager');
assert.strictEqual(
instance['factory'],
factory,
'delegate is an instance of the custom manager'
);
});
test('it works with internal managers', (assert) => {
class TestInternalComponentManager implements InternalComponentManager {
constructor() {}
create() {}
getCapabilities() {
return {
dynamicLayout: false,
dynamicTag: false,
prepareArgs: false,
createArgs: false,
attributeHook: false,
elementHook: false,
dynamicScope: false,
createCaller: false,
updateHook: false,
createInstance: false,
wrapped: false,
willDestroy: false,
hasSubOwner: false,
};
}
getDebugName() {
return 'internal';
}
getDestroyable() {
return null;
}
getSelf() {
return UNDEFINED_REFERENCE;
}
}
let definition = setInternalComponentManager(new TestInternalComponentManager(), {});
let instance1 = getInternalComponentManager(definition) as TestInternalComponentManager;
assert.ok(
instance1 instanceof TestInternalComponentManager,
'manager is an instance of the custom manager'
);
});
if (import.meta.env.DEV) {
test('throws if multiple component managers associated with the same definition', (assert) => {
let definition = setInternalComponentManager({} as any, {});
assert.throws(() => {
setComponentManager(() => {
return {} as any;
}, definition);
}, /Attempted to set the same type of manager multiple times on a value. You can only associate one manager of each type/u);
});
test('throws a useful error when missing capabilities on non-internal managers', (assert) => {
let definition = setComponentManager(() => {
return {} as any;
}, {});
let manager = getInternalComponentManager(definition) as CustomComponentManager<
object,
unknown
>;
assert.throws(() => {
manager.create({}, {}, {} as any);
}, /Custom component managers must have a `capabilities` property /u);
});
test('throws a useful error when capabilities not made with buildCapabilities are used on non-internal managers', (assert) => {
let definition = setComponentManager(() => {
return {
capabilities: {},
} as any;
}, {});
let manager = getInternalComponentManager(definition) as CustomComponentManager<
object,
unknown
>;
assert.throws(() => {
manager.create({}, {}, {} as any);
}, /Custom component managers must have a `capabilities` property /u);
});
test('throws an error if used with primitive values', (assert) => {
assertPrimitiveUsage(assert, setInternalComponentManager);
});
}
});
module('Helper', () => {
test('it works', (assert) => {
class CustomManager implements HelperManager<unknown> {
capabilities = helperCapabilities('3.23', {
hasValue: true,
});
constructor(public owner: object | undefined) {}
createHelper() {}
}
let factory = (owner?: object) => new CustomManager(owner);
let definition = setHelperManager(factory, {});
let instance = getInternalHelperManager(definition) as CustomHelperManager<object>;
assert.strictEqual(typeof instance, 'object', 'manager is an internal manager');
assert.strictEqual(
typeof instance.getHelper({}),
'function',
'manager can generate helper function'
);
assert.strictEqual(instance['factory'], factory, 'manager has correct delegate factory');
});
test('it determines the default manager', (assert) => {
let myTestHelper = () => 0;
let instance = getInternalHelperManager(myTestHelper) as CustomHelperManager<object>;
assert.strictEqual(typeof instance, 'object', 'manager is an internal manager');
assert.strictEqual(
typeof instance.getHelper({}),
'function',
'manager can generate helper function'
);
assert.strictEqual(
instance['factory']({})?.getDebugName?.(myTestHelper),
'(helper function myTestHelper)'
);
});
test('it works with internal helpers', (assert) => {
let helper = () => {
return UNDEFINED_REFERENCE;
};
let definition = setInternalHelperManager(helper, {});
let instance1 = getInternalHelperManager(definition)!;
assert.strictEqual(instance1, helper, 'manager is the internal helper');
});
if (import.meta.env.DEV) {
test('throws if multiple helper managers associated with the same definition', (assert) => {
let definition = setInternalHelperManager(() => {
return {} as any;
}, {});
assert.throws(() => {
setHelperManager(() => {
return {} as any;
}, definition);
}, /Attempted to set the same type of manager multiple times on a value. You can only associate one manager of each type/u);
});
test('throws a useful error when missing capabilities on non-internal managers', (assert) => {
let definition = setHelperManager(() => {
return {} as any;
}, {});
let manager = getInternalHelperManager(definition) as InternalHelperManager<object>;
assert.throws(() => {
manager.getDelegateFor({});
}, /Custom helper managers must have a `capabilities` property /u);
});
test('throws a useful error when capabilities not made with buildCapabilities are used on non-internal managers', (assert) => {
let definition = setHelperManager(() => {
return {
capabilities: {},
} as any;
}, {});
let manager = getInternalHelperManager(definition) as InternalHelperManager<object>;
assert.throws(() => {
manager.getDelegateFor({});
}, /Custom helper managers must have a `capabilities` property /u);
});
test('throws an error if used with primitive values', (assert) => {
assertPrimitiveUsage(assert, setInternalHelperManager);
});
}
});
module('Modifier', () => {
test('it works', (assert) => {
class CustomManager implements ModifierManager<unknown> {
capabilities = modifierCapabilities('3.22');
constructor(public owner: object | undefined) {}
createModifier() {}
installModifier() {}
updateModifier() {}
destroyModifier() {}
}
let factory = (owner: object) => new CustomManager(owner);
let definition = setModifierManager(factory, {});
let instance = getInternalModifierManager(definition) as CustomModifierManager<
object,
unknown
>;
assert.ok(instance instanceof CustomModifierManager, 'internal manager is a custom manager');
assert.strictEqual(
instance['factory'],
factory,
'internal manager has custom manager factory'
);
});
test('it works with internal managers', (assert) => {
class TestInternalModifierManager {
constructor() {}
create() {}
getDebugName() {
return 'internal';
}
getDebugInstance() {
return null;
}
getDestroyable() {
return null;
}
getTag() {
return createUpdatableTag();
}
install() {}
update() {}
}
let definition = setInternalModifierManager(new TestInternalModifierManager(), {});
let instance1 = getInternalModifierManager(definition) as TestInternalModifierManager;
assert.ok(
instance1 instanceof TestInternalModifierManager,
'manager is an instance of the custom manager'
);
});
if (import.meta.env.DEV) {
test('throws if multiple modifier managers associated with the same definition', (assert) => {
let definition = setModifierManager(() => {
return {} as any;
}, {});
assert.throws(() => {
setModifierManager(() => {
return {} as any;
}, definition);
}, /Attempted to set the same type of manager multiple times on a value. You can only associate one manager of each type/u);
});
test('throws a useful error when missing capabilities on non-internal managers', (assert) => {
let definition = setModifierManager(() => {
return {} as any;
}, {});
let manager = getInternalModifierManager(definition);
assert.throws(() => {
manager.create({}, {} as any, {}, {} as any);
}, /Custom modifier managers must have a `capabilities` property /u);
});
test('throws a useful error when capabilities not made with buildCapabilities are used on non-internal managers', (assert) => {
let definition = setModifierManager(() => {
return {
capabilities: {},
} as any;
}, {});
let manager = getInternalModifierManager(definition);
assert.throws(() => {
manager.create({}, {} as any, {}, {} as any);
}, /Custom modifier managers must have a `capabilities` property /u);
});
test('throws an error if used with primitive values', (assert) => {
assertPrimitiveUsage(assert, setModifierManager);
});
}
});
test('Can set different types of managers', (assert) => {
let manager = {} as any;
let definition = {};
setInternalComponentManager(manager, definition);
setInternalModifierManager(manager, definition);
setInternalHelperManager(manager, definition);
assert.strictEqual(manager, getInternalComponentManager(definition), 'component manager works');
assert.strictEqual(manager, getInternalModifierManager(definition), 'modifier manager works');
assert.strictEqual(manager, getInternalHelperManager(definition), 'helper manager works');
});
});
function assertPrimitiveUsage(assert: Assert, setManager: any) {
if (!import.meta.env.DEV) {
assert.expect(0);
return;
}
assert.throws(() => {
setManager(() => ({}) as any, null as any);
}, /Attempted to set a manager on a non-object value/u);
assert.throws(() => {
setManager(() => ({}) as any, undefined as any);
}, /Attempted to set a manager on a non-object value/u);
assert.throws(() => {
setManager(() => ({}) as any, true as any);
}, /Attempted to set a manager on a non-object value/u);
assert.throws(() => {
setManager(() => ({}) as any, false as any);
}, /Attempted to set a manager on a non-object value/u);
assert.throws(() => {
setManager(() => ({}) as any, 123 as any);
}, /Attempted to set a manager on a non-object value/u);
assert.throws(() => {
setManager(() => ({}) as any, 'foo' as any);
}, /Attempted to set a manager on a non-object value/u);
if (typeof Symbol === 'function') {
assert.throws(() => {
setManager(() => ({}) as any, Symbol('foo') as any);
}, /Attempted to set a manager on a non-object value/u);
}
}