-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
each-test.js
298 lines (204 loc) · 8.61 KB
/
each-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
import { get } from 'ember-metal/property_get';
import { set } from 'ember-metal/property_set';
import { applyMixins } from '../../utils/abstract-test-case';
import { moduleFor, RenderingTest } from '../../utils/test-case';
import { A as emberA } from 'ember-runtime/system/native_array';
import {
BasicConditionalsTest,
SyntaxCondtionalTestHelpers,
TruthyGenerator,
FalsyGenerator,
ArrayTestCases
} from '../../utils/shared-conditional-tests';
class EachTest extends BasicConditionalsTest {
get truthyValue() { return ['non-empty']; }
get falsyValue() { return []; }
}
applyMixins(EachTest,
SyntaxCondtionalTestHelpers,
new TruthyGenerator([
// TODO: figure out what the rest of the cases are
['hello']
]),
new FalsyGenerator([
// TODO: figure out what the rest of the cases are
[],
undefined
]),
ArrayTestCases
);
moduleFor('Syntax test: {{#each}}', class extends EachTest {
templateFor({ cond, truthy, falsy }) {
return `{{#each ${cond}}}${truthy}{{else}}${falsy}{{/each}}`;
}
});
moduleFor('Syntax test: {{#each as}}', class extends EachTest {
templateFor({ cond, truthy, falsy }) {
return `{{#each ${cond} as |test|}}${truthy}{{else}}${falsy}{{/each}}`;
}
['@test it repeats the given block for each item in the array']() {
this.render(`{{#each list as |item|}}{{item.text}}{{else}}Empty{{/each}}`, {
list: emberA([{ text: 'hello' }])
});
this.assertText('hello');
this.runTask(() => this.rerender());
this.assertText('hello');
this.runTask(() => set(this.context.get('list').objectAt(0), 'text', 'Hello'));
this.assertText('Hello');
this.runTask(() => {
let list = this.context.get('list');
list.pushObject({ text: ' ' });
list.pushObject({ text: 'world' });
});
this.assertText('Hello world');
this.runTask(() => this.context.get('list').clear());
this.assertText('Empty');
this.runTask(() => set(this.context, 'list', [{ text: 'hello' }]));
this.assertText('hello');
}
[`@test it maintains DOM stability when condition changes between objects with the same keys`]() {
this.render(`{{#each list key="text" as |item|}}{{item.text}}{{/each}}`, {
list: emberA([{ text: 'Hello' }, { text: ' ' }, { text: 'world' }])
});
this.assertText('Hello world');
this.takeSnapshot();
this.runTask(() => {
let list = this.context.get('list');
list.popObject();
list.popObject();
list.pushObject({ text: ' ' });
list.pushObject({ text: 'world' });
});
this.assertText('Hello world');
this.assertInvariants();
this.runTask(() => set(this.context, 'list', [{ text: 'Hello' }, { text: ' ' }, { text: 'world' }]));
this.assertText('Hello world');
this.assertInvariants();
}
['@test can access the item and the original scope']() {
this.render(`{{#each people key="name" as |person|}}[{{title}}: {{person.name}}]{{/each}}`, {
title: 'Señor Engineer',
people: emberA([{ name: 'Tom Dale' }, { name: 'Yehuda Katz' }, { name: 'Godfrey Chan' }])
});
this.assertText('[Señor Engineer: Tom Dale][Señor Engineer: Yehuda Katz][Señor Engineer: Godfrey Chan]');
this.runTask(() => this.rerender());
this.assertText('[Señor Engineer: Tom Dale][Señor Engineer: Yehuda Katz][Señor Engineer: Godfrey Chan]');
this.runTask(() => {
let people = get(this.context, 'people');
set(people.objectAt(1), 'name', 'Stefan Penner');
people.removeAt(0);
people.pushObject({ name: 'Tom Dale' });
people.insertAt(1, { name: 'Chad Hietala' });
set(this.context, 'title', 'Principal Engineer');
});
this.assertText('[Principal Engineer: Stefan Penner][Principal Engineer: Chad Hietala][Principal Engineer: Godfrey Chan][Principal Engineer: Tom Dale]');
this.runTask(() => {
set(this.context, 'people', [{ name: 'Tom Dale' }, { name: 'Yehuda Katz' }, { name: 'Godfrey Chan' }]);
set(this.context, 'title', 'Señor Engineer');
});
this.assertText('[Señor Engineer: Tom Dale][Señor Engineer: Yehuda Katz][Señor Engineer: Godfrey Chan]');
}
['@test the scoped variable is not available outside the {{#each}} block.']() {
this.render(`{{name}}-{{#each other as |name|}}{{name}}{{/each}}-{{name}}`, {
name: 'Stef',
other: emberA(['Yehuda'])
});
this.assertText('Stef-Yehuda-Stef');
this.runTask(() => this.rerender());
this.assertText('Stef-Yehuda-Stef');
this.runTask(() => get(this.context, 'other').pushObjects([' ', 'Katz']));
this.assertText('Stef-Yehuda Katz-Stef');
this.runTask(() => set(this.context, 'name', 'Tom'));
this.assertText('Tom-Yehuda Katz-Tom');
this.runTask(() => {
set(this.context, 'name', 'Stef');
set(this.context, 'other', ['Yehuda']);
});
this.assertText('Stef-Yehuda-Stef');
}
['@test inverse template is displayed with context']() {
this.render(`{{#each falsyThing as |thing|}}Has Thing{{else}}No Thing {{otherThing}}{{/each}}`, {
falsyThing: [],
otherThing: 'bar'
});
this.assertText('No Thing bar');
this.runTask(() => this.rerender());
this.assertText('No Thing bar');
this.runTask(() => set(this.context, 'otherThing', 'biz'));
this.assertText('No Thing biz');
this.runTask(() => set(this.context, 'falsyThing', ['non-empty']));
this.assertText('Has Thing');
this.runTask(() => set(this.context, 'otherThing', 'baz'));
this.assertText('Has Thing');
this.runTask(() => {
set(this.context, 'otherThing', 'bar');
set(this.context, 'falsyThing', null);
});
this.assertText('No Thing bar');
}
});
moduleFor('Syntax test: Multiple {{#each as}} helpers', class extends RenderingTest {
['@test re-using the same variable with different {{#each}} blocks does not override each other']() {
this.render(`Admin: {{#each admins key="name" as |person|}}[{{person.name}}]{{/each}} User: {{#each users key="name" as |person|}}[{{person.name}}]{{/each}}`, {
admins: emberA([{ name: 'Tom Dale' }]),
users: emberA([{ name: 'Yehuda Katz' }])
});
this.assertText('Admin: [Tom Dale] User: [Yehuda Katz]');
this.runTask(() => this.rerender());
this.assertText('Admin: [Tom Dale] User: [Yehuda Katz]');
this.runTask(() => {
get(this.context, 'admins').pushObject({ name: 'Godfrey Chan' });
set(get(this.context, 'users').objectAt(0), 'name', 'Stefan Penner');
});
this.assertText('Admin: [Tom Dale][Godfrey Chan] User: [Stefan Penner]');
this.runTask(() => {
set(this.context, 'admins', [{ name: 'Tom Dale' }]);
set(this.context, 'users', [{ name: 'Yehuda Katz' }]);
});
this.assertText('Admin: [Tom Dale] User: [Yehuda Katz]');
}
['@test the scoped variable is not available outside the {{#each}} block']() {
this.render(`{{ring}}-{{#each first as |ring|}}{{ring}}-{{#each fifth as |ring|}}{{ring}}-{{#each ninth as |ring|}}{{ring}}-{{/each}}{{ring}}-{{/each}}{{ring}}-{{/each}}{{ring}}`, {
ring: 'Greed',
first: emberA(['Limbo']),
fifth: emberA(['Wrath']),
ninth: emberA(['Treachery'])
});
this.assertText('Greed-Limbo-Wrath-Treachery-Wrath-Limbo-Greed');
this.runTask(() => this.rerender());
this.assertText('Greed-Limbo-Wrath-Treachery-Wrath-Limbo-Greed');
this.runTask(() => {
set(this.context, 'ring', 'O');
get(this.context, 'fifth').insertAt(0, 'D');
});
this.assertText('O-Limbo-D-Treachery-D-Wrath-Treachery-Wrath-Limbo-O');
this.runTask(() => {
get(this.context, 'first').pushObject('I');
get(this.context, 'ninth').replace(0, 1, 'K');
});
this.assertText('O-Limbo-D-K-D-Wrath-K-Wrath-Limbo-I-D-K-D-Wrath-K-Wrath-I-O');
this.runTask(() => {
set(this.context, 'ring', 'Greed');
set(this.context, 'first', ['Limbo']);
set(this.context, 'fifth', ['Wrath']);
set(this.context, 'ninth', ['Treachery']);
});
this.assertText('Greed-Limbo-Wrath-Treachery-Wrath-Limbo-Greed');
}
['@test it should support {{#each name as |foo|}}, then {{#each foo as |bar|}}']() {
this.render(`{{#each name key="@index" as |foo|}}{{#each foo as |bar|}}{{bar}}{{/each}}{{/each}}`, {
name: emberA([emberA(['caterpillar'])])
});
this.assertText('caterpillar');
this.runTask(() => this.rerender());
this.assertText('caterpillar');
this.runTask(() => {
let name = get(this.context, 'name');
name.objectAt(0).replace(0, 1, 'lady');
name.pushObject(['bird']);
});
this.assertText('ladybird');
this.runTask(() => set(this.context, 'name', [['caterpillar']]));
this.assertText('caterpillar');
}
});