-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
each-in-test.js
181 lines (143 loc) · 4.37 KB
/
each-in-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
import { set } from 'ember-metal/property_set';
import { strip } from '../../utils/abstract-test-case';
import { applyMixins } from '../../utils/abstract-test-case';
import { moduleFor, RenderingTest } from '../../utils/test-case';
import {
BasicConditionalsTest,
SyntaxCondtionalTestHelpers,
TruthyGenerator,
FalsyGenerator,
// ObjectTestCases
} from '../../utils/shared-conditional-tests';
class EachInTest extends BasicConditionalsTest {
get truthyValue() { return { 'Not Empty': 1 }; }
get falsyValue() { return {}; }
}
applyMixins(EachInTest,
SyntaxCondtionalTestHelpers,
new TruthyGenerator([
// TODO: figure out what the rest of the cases are
{ foo: 1 }
]),
new FalsyGenerator([
// TODO: figure out what the rest of the cases are
{},
Object.create({ 'Not Empty': 1 }),
undefined,
null
])
// TODO(mmun): Add support for object proxies and
// include the ObjectTestCases mixin.
);
moduleFor('@htmlbars Syntax test: {{#each-in}}', class extends EachInTest {
templateFor({ cond, truthy, falsy }) {
return `{{#each-in ${cond}}}${truthy}{{else}}${falsy}{{/each-in}}`;
}
[`@test it repeats the given block for each item in the hash`]() {
this.render(strip`
<ul>
{{#each-in categories as |category count|}}
<li>{{category}}: {{count}}</li>
{{/each-in}}
</ul>
`, {
categories: {
'Smartphones': 8203,
'JavaScript Frameworks': Infinity
}
});
this.assertHTML(strip`
<ul>
<li>Smartphones: 8203</li>
<li>JavaScript Frameworks: Infinity</li>
</ul>
`);
this.assertStableRerender();
this.runTask(() => {
set(this.context, 'categories.Smartphones', 100);
set(this.context, 'categories.Tweets', 443115);
// {{#each-in}} does not currently observe internal mutations to the hash
// so we manually trigger a rerender. This behavior may change in the future.
this.rerender();
});
this.assertHTML(strip`
<ul>
<li>Smartphones: 100</li>
<li>JavaScript Frameworks: Infinity</li>
<li>Tweets: 443115</li>
</ul>
`);
this.runTask(() => set(this.context, 'categories', {
'Smartphones': 8203,
'JavaScript Frameworks': Infinity
}));
this.assertHTML(strip`
<ul>
<li>Smartphones: 8203</li>
<li>JavaScript Frameworks: Infinity</li>
</ul>
`);
}
[`@test it only iterates over an object's own properties`]() {
let protoCategories = {
'Smartphones': 8203,
'JavaScript Frameworks': Infinity
};
let categories = Object.create(protoCategories);
categories['Televisions'] = 183;
categories['Alarm Clocks'] = 999;
this.render(strip`
<ul>
{{#each-in categories as |category count|}}
<li>{{category}}: {{count}}</li>
{{/each-in}}
</ul>
`, { categories });
this.assertHTML(strip`
<ul>
<li>Televisions: 183</li>
<li>Alarm Clocks: 999</li>
</ul>
`);
this.assertStableRerender();
this.runTask(() => {
set(protoCategories, 'Robots', 666);
set(categories, 'Tweets', 443115);
// {{#each-in}} does not currently observe internal mutations to the hash
// so we manually trigger a rerender. This behavior may change in the future.
this.rerender();
});
this.assertHTML(strip`
<ul>
<li>Televisions: 183</li>
<li>Alarm Clocks: 999</li>
<li>Tweets: 443115</li>
</ul>
`);
categories = Object.create(protoCategories);
categories['Televisions'] = 183;
categories['Alarm Clocks'] = 999;
this.runTask(() => set(this.context, 'categories', categories));
this.assertHTML(strip`
<ul>
<li>Televisions: 183</li>
<li>Alarm Clocks: 999</li>
</ul>
`);
}
});
moduleFor('@htmlbars Syntax test: {{#each-in}} undefined path', class extends RenderingTest {
['@test keying off of `undefined` does not render'](assert) {
this.render(strip`
{{#each-in foo.bar.baz as |thing|}}
{{thing}}
{{/each-in}}`, { foo: {} });
this.assertText('');
this.runTask(() => this.rerender());
this.assertText('');
this.runTask(() => set(this.context, 'foo', { bar: { baz: { 'Here!': 1 } } }));
this.assertText('Here!');
this.runTask(() => set(this.context, 'foo', {}));
this.assertText('');
}
});