-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathif-inline-test.ts
197 lines (152 loc) · 5.59 KB
/
if-inline-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
import {
defineComponent,
jitSuite,
RenderTest,
strip,
test,
tracked,
} from '@glimmer-workspace/integration-tests';
class IfInlineTest extends RenderTest {
static suiteName = 'Helpers test: inline {{if}}';
@test
'inline if helper updates when tracked property changes'() {
class Person {
@tracked isActive = false;
toggle() {
this.isActive = !this.isActive;
}
}
const person = new Person();
this.render(strip`<div>{{if this.person.isActive "Active" "Inactive"}}</div>`, { person });
this.assertHTML('<div>Inactive</div>', 'Initial render shows inactive state');
this.assertStableRerender();
person.toggle();
this.rerender();
this.assertHTML('<div>Active</div>', 'Updates when tracked property changes to true');
person.toggle();
this.rerender();
this.assertHTML('<div>Inactive</div>', 'Updates when tracked property changes to false');
}
@test
'inline if helper with only truthy value updates when tracked property changes'() {
class Person {
@tracked isActive = false;
toggle() {
this.isActive = !this.isActive;
}
}
const person = new Person();
this.render(strip`<div>{{if this.person.isActive "Active"}}</div>`, { person });
this.assertHTML('<div></div>', 'Initial render shows empty when false');
this.assertStableRerender();
person.toggle();
this.rerender();
this.assertHTML('<div>Active</div>', 'Updates when tracked property changes to true');
person.toggle();
this.rerender();
this.assertHTML('<div></div>', 'Updates when tracked property changes to false');
}
@test
'inline if helper updates when component argument changes'() {
const TestComponent = defineComponent({}, '{{if @isActive "Active" "Inactive"}}');
this.render('<TestComponent @isActive={{this.isActive}} />', {
isActive: false,
TestComponent,
});
this.assertHTML('Inactive', 'Initial render shows inactive state');
this.assertStableRerender();
this.rerender({ isActive: true, TestComponent });
this.assertHTML('Active', 'Updates when argument changes to true');
this.rerender({ isActive: false, TestComponent });
this.assertHTML('Inactive', 'Updates when argument changes to false');
}
@test
'inline if helper with components updates when tracked property changes'() {
class Person {
@tracked isActive = false;
toggle() {
this.isActive = !this.isActive;
}
}
const person = new Person();
const Ok = defineComponent({}, '<div>OK Component</div>');
const Ko = defineComponent({}, '<div>KO Component</div>');
// Create a component with Ok and Ko in scope
const TestContainer = defineComponent({ Ok, Ko }, '<div>{{if @isActive Ok Ko}}</div>');
this.render('<TestContainer @isActive={{this.person.isActive}} />', { person, TestContainer });
this.assertHTML('<div><div>KO Component</div></div>', 'Initial render shows KO component');
this.assertStableRerender();
person.toggle();
this.rerender();
this.assertHTML(
'<div><div>OK Component</div></div>',
'Updates to OK component when tracked property changes to true'
);
person.toggle();
this.rerender();
this.assertHTML(
'<div><div>KO Component</div></div>',
'Updates to KO component when tracked property changes to false'
);
}
@test
'inline if helper with components updates when component argument changes'() {
const Ok = defineComponent({}, '<div>OK Component</div>');
const Ko = defineComponent({}, '<div>KO Component</div>');
const TestComponent = defineComponent({ Ok, Ko }, '{{if @isOk Ok Ko}}');
this.render('<TestComponent @isOk={{this.isOk}} />', { isOk: false, TestComponent });
this.assertHTML('<div>KO Component</div>', 'Initial render shows KO component');
this.assertStableRerender();
this.rerender({ isOk: true, TestComponent });
this.assertHTML(
'<div>OK Component</div>',
'Updates to OK component when argument changes to true'
);
this.rerender({ isOk: false, TestComponent });
this.assertHTML(
'<div>KO Component</div>',
'Updates to KO component when argument changes to false'
);
}
@test
'comparison with block form if helper using components'() {
class Person {
@tracked isActive = false;
toggle() {
this.isActive = !this.isActive;
}
}
const person = new Person();
const Ok = defineComponent({}, '<div>OK Component</div>');
const Ko = defineComponent({}, '<div>KO Component</div>');
// Create a component with Ok and Ko in scope for both inline and block forms
const TestContainer = defineComponent(
{ Ok, Ko },
`
<div>
Inline: {{if @isActive Ok Ko}}
Block: {{#if @isActive}}<Ok />{{else}}<Ko />{{/if}}
</div>
`
);
this.render('<TestContainer @isActive={{this.person.isActive}} />', { person, TestContainer });
this.assertHTML(
'<div>Inline: <div>KO Component</div> Block: <div>KO Component</div></div>',
'Initial render both show KO component'
);
this.assertStableRerender();
person.toggle();
this.rerender();
this.assertHTML(
'<div>Inline: <div>OK Component</div> Block: <div>OK Component</div></div>',
'Both update to OK component when tracked property changes to true'
);
person.toggle();
this.rerender();
this.assertHTML(
'<div>Inline: <div>KO Component</div> Block: <div>KO Component</div></div>',
'Both update to KO component when tracked property changes to false'
);
}
}
jitSuite(IfInlineTest);