-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathstrict-mode-test.js
324 lines (254 loc) · 9.35 KB
/
strict-mode-test.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
import {
moduleFor,
ApplicationTestCase,
RenderingTestCase,
defineComponent,
defineSimpleHelper,
defineSimpleModifier,
} from 'internal-test-helpers';
import { Input, Textarea } from '@ember/component';
import { LinkTo } from '@ember/routing';
import { hash, array, concat, get, on, fn } from '@glimmer/runtime';
import GlimmerishComponent from '../../utils/glimmerish-component';
moduleFor(
'Strict Mode',
class extends RenderingTestCase {
'@test Can use a component in scope'() {
let Foo = defineComponent({}, 'Hello, world!');
let Bar = defineComponent({ Foo }, '<Foo/>');
this.registerComponent('bar', { ComponentClass: Bar });
this.render('<Bar/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use a custom helper in scope (in append position)'() {
let foo = defineSimpleHelper(() => 'Hello, world!');
let Bar = defineComponent({ foo }, '{{foo}}');
this.registerComponent('bar', { ComponentClass: Bar });
this.render('<Bar/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use a custom modifier in scope'() {
let foo = defineSimpleModifier((element) => (element.innerHTML = 'Hello, world!'));
let Bar = defineComponent({ foo }, '<div {{foo}}></div>');
this.registerComponent('bar', { ComponentClass: Bar });
this.render('<Bar/>');
this.assertHTML('<div>Hello, world!</div>');
this.assertStableRerender();
}
'@test Can shadow keywords'() {
let ifComponent = defineComponent({}, 'Hello, world!');
let Bar = defineComponent({ if: ifComponent }, '{{#if}}{{/if}}');
this.registerComponent('bar', { ComponentClass: Bar });
this.render('<Bar/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use constant values in ambiguous helper/component position'() {
let value = 'Hello, world!';
let Foo = defineComponent({ value }, '{{value}}');
this.registerComponent('foo', { ComponentClass: Foo });
this.render('<Foo/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use inline if and unless in strict mode templates'() {
let Foo = defineComponent({}, '{{if true "foo" "bar"}}{{unless true "foo" "bar"}}');
this.registerComponent('foo', { ComponentClass: Foo });
this.render('<Foo/>');
this.assertHTML('foobar');
this.assertStableRerender();
}
'@test Can use a dynamic component definition'() {
let Foo = defineComponent({}, 'Hello, world!');
let Bar = defineComponent(
{},
'<this.Foo/>',
class extends GlimmerishComponent {
Foo = Foo;
}
);
this.registerComponent('bar', { ComponentClass: Bar });
this.render('<Bar/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use a dynamic component definition (curly)'() {
let Foo = defineComponent({}, 'Hello, world!');
let Bar = defineComponent(
{},
'{{this.Foo}}',
class extends GlimmerishComponent {
Foo = Foo;
}
);
this.registerComponent('bar', { ComponentClass: Bar });
this.render('<Bar/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use a dynamic helper definition'() {
let foo = defineSimpleHelper(() => 'Hello, world!');
let Bar = defineComponent(
{},
'{{this.foo}}',
class extends GlimmerishComponent {
foo = foo;
}
);
this.registerComponent('bar', { ComponentClass: Bar });
this.render('<Bar/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use a curried dynamic helper'() {
let foo = defineSimpleHelper((value) => value);
let Foo = defineComponent({}, '{{@value}}');
let Bar = defineComponent({ Foo, foo }, '<Foo @value={{helper foo "Hello, world!"}}/>');
this.registerComponent('bar', { ComponentClass: Bar });
this.render('<Bar/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use a curried dynamic modifier'() {
let foo = defineSimpleModifier((element, [text]) => (element.innerHTML = text));
let Foo = defineComponent({}, '<div {{@value}}></div>');
let Bar = defineComponent({ Foo, foo }, '<Foo @value={{modifier foo "Hello, world!"}}/>');
this.registerComponent('bar', { ComponentClass: Bar });
this.render('<Bar/>');
this.assertHTML('<div>Hello, world!</div>');
this.assertStableRerender();
}
}
);
moduleFor(
'Strict Mode - built ins',
class extends RenderingTestCase {
'@test Can use Input'() {
let Foo = defineComponent({ Input }, '<Input/>');
this.registerComponent('foo', { ComponentClass: Foo });
this.render('<Foo/>');
this.assertComponentElement(this.firstChild, {
tagName: 'input',
attrs: {
type: 'text',
class: 'ember-text-field ember-view',
},
});
this.assertStableRerender();
}
'@test Can use Textarea'() {
let Foo = defineComponent({ Textarea }, '<Textarea/>');
this.registerComponent('foo', { ComponentClass: Foo });
this.render('<Foo/>');
this.assertComponentElement(this.firstChild, {
tagName: 'textarea',
attrs: {
class: 'ember-text-area ember-view',
},
});
this.assertStableRerender();
}
'@test Can use hash'() {
let Foo = defineComponent(
{ hash },
'{{#let (hash value="Hello, world!") as |hash|}}{{hash.value}}{{/let}}'
);
this.registerComponent('foo', { ComponentClass: Foo });
this.render('<Foo/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use array'() {
let Foo = defineComponent(
{ array },
'{{#each (array "Hello, world!") as |value|}}{{value}}{{/each}}'
);
this.registerComponent('foo', { ComponentClass: Foo });
this.render('<Foo/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use concat'() {
let Foo = defineComponent({ concat }, '{{(concat "Hello" ", " "world!")}}');
this.registerComponent('foo', { ComponentClass: Foo });
this.render('<Foo/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use get'() {
let Foo = defineComponent(
{ hash, get },
'{{#let (hash value="Hello, world!") as |hash|}}{{(get hash "value")}}{{/let}}'
);
this.registerComponent('foo', { ComponentClass: Foo });
this.render('<Foo/>');
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
'@test Can use on and fn'(assert) {
assert.expect(1);
let handleClick = (value) => {
assert.equal(value, 123);
};
let Foo = defineComponent(
{ on, fn, handleClick },
'<button {{on "click" (fn handleClick 123)}}>Click</button>'
);
this.registerComponent('foo', { ComponentClass: Foo });
this.render('<Foo/>');
this.click('button');
}
// Ember currently uses AST plugins to implement certain features that
// glimmer-vm does not natively provide, such as {{#each-in}}, {{outlet}}
// {{mount}} and some features in {{#in-element}}. These rewrites the AST
// and insert private keywords e.g. `{{#each (-each-in)}}`. These tests
// ensures we have _some_ basic coverage for those features in strict mode.
//
// Ultimately, our test coverage for strict mode is quite inadequate. This
// is particularly important as we expect more apps to start adopting the
// feature. Ideally we would run our entire/most of our test suite against
// both strict and resolution modes, and these things would be implicitly
// covered elsewhere, but until then, these coverage are essential.
'@test Can use each-in'() {
let obj = {
foo: 'FOO',
bar: 'BAR',
};
let Foo = defineComponent({ obj }, '{{#each-in obj as |k v|}}[{{k}}:{{v}}]{{/each-in}}');
this.registerComponent('foo', { ComponentClass: Foo });
this.render('<Foo/>');
this.assertHTML('[foo:FOO][bar:BAR]');
this.assertStableRerender();
}
'@test Can use in-element'() {
let getElement = (id) => document.getElementById(id);
let Foo = defineComponent(
{ getElement },
'{{#in-element (getElement "in-element-test")}}before{{/in-element}}after'
);
this.registerComponent('foo', { ComponentClass: Foo });
this.render('[<div id="in-element-test" />][<Foo/>]');
this.assertText('[before][after]');
this.assertStableRerender();
}
}
);
moduleFor(
'Strict Mode - LinkTo',
class extends ApplicationTestCase {
'@test Can use LinkTo'() {
let Foo = defineComponent({ LinkTo }, '<LinkTo @route="index">Index</LinkTo>');
this.addComponent('foo', { ComponentClass: Foo });
this.addTemplate('index', `<Foo/>`);
return this.visit('/').then(() => {
this.assertComponentElement(this.firstChild, {
tagName: 'a',
attrs: { href: '/', class: 'ember-view active' },
content: 'Index',
});
});
}
}
);