-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
fn-test.js
228 lines (176 loc) · 6.49 KB
/
fn-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
import { set } from '@ember/-internals/metal';
import { HAS_NATIVE_PROXY } from '@ember/-internals/utils';
import { DEBUG } from '@glimmer/env';
import { RenderingTestCase, moduleFor, runTask } from 'internal-test-helpers';
import { Component } from '../../utils/helpers';
moduleFor(
'Helpers test: {{fn}}',
class extends RenderingTestCase {
beforeEach() {
this.registerHelper('invoke', function([fn]) {
return fn();
});
let testContext = this;
this.registerComponent('stash', {
ComponentClass: Component.extend({
init() {
this._super(...arguments);
testContext.stashedFn = this.stashedFn;
},
}),
});
}
'@test updates when arguments change'() {
this.render(`{{invoke (fn this.myFunc this.arg1 this.arg2)}}`, {
myFunc(arg1, arg2) {
return `arg1: ${arg1}, arg2: ${arg2}`;
},
arg1: 'foo',
arg2: 'bar',
});
this.assertText('arg1: foo, arg2: bar');
this.assertStableRerender();
runTask(() => set(this.context, 'arg1', 'qux'));
this.assertText('arg1: qux, arg2: bar');
runTask(() => set(this.context, 'arg2', 'derp'));
this.assertText('arg1: qux, arg2: derp');
runTask(() => {
set(this.context, 'arg1', 'foo');
set(this.context, 'arg2', 'bar');
});
this.assertText('arg1: foo, arg2: bar');
}
'@test updates when the function changes'() {
let func1 = (arg1, arg2) => `arg1: ${arg1}, arg2: ${arg2}`;
let func2 = (arg1, arg2) => `arg2: ${arg2}, arg1: ${arg1}`;
this.render(`{{invoke (fn this.myFunc this.arg1 this.arg2)}}`, {
myFunc: func1,
arg1: 'foo',
arg2: 'bar',
});
this.assertText('arg1: foo, arg2: bar');
this.assertStableRerender();
runTask(() => set(this.context, 'myFunc', func2));
this.assertText('arg2: bar, arg1: foo');
runTask(() => set(this.context, 'myFunc', func1));
this.assertText('arg1: foo, arg2: bar');
}
'@test a stashed fn result update arguments when invoked'(assert) {
this.render(`{{stash stashedFn=(fn this.myFunc this.arg1 this.arg2)}}`, {
myFunc(arg1, arg2) {
return `arg1: ${arg1}, arg2: ${arg2}`;
},
arg1: 'foo',
arg2: 'bar',
});
assert.equal(this.stashedFn(), 'arg1: foo, arg2: bar');
runTask(() => set(this.context, 'arg1', 'qux'));
assert.equal(this.stashedFn(), 'arg1: qux, arg2: bar');
runTask(() => set(this.context, 'arg2', 'derp'));
assert.equal(this.stashedFn(), 'arg1: qux, arg2: derp');
runTask(() => {
set(this.context, 'arg1', 'foo');
set(this.context, 'arg2', 'bar');
});
assert.equal(this.stashedFn(), 'arg1: foo, arg2: bar');
}
'@test a stashed fn result invokes the correct function when the bound function changes'(
assert
) {
let func1 = (arg1, arg2) => `arg1: ${arg1}, arg2: ${arg2}`;
let func2 = (arg1, arg2) => `arg2: ${arg2}, arg1: ${arg1}`;
this.render(`{{stash stashedFn=(fn this.myFunc this.arg1 this.arg2)}}`, {
myFunc: func1,
arg1: 'foo',
arg2: 'bar',
});
assert.equal(this.stashedFn(), 'arg1: foo, arg2: bar');
runTask(() => set(this.context, 'myFunc', func2));
assert.equal(this.stashedFn(), 'arg2: bar, arg1: foo');
runTask(() => set(this.context, 'myFunc', func1));
assert.equal(this.stashedFn(), 'arg1: foo, arg2: bar');
}
'@test asserts if the first argument is not a function'() {
expectAssertion(() => {
this.render(`{{invoke (fn this.myFunc this.arg1 this.arg2)}}`, {
myFunc: null,
arg1: 'foo',
arg2: 'bar',
});
}, /You must pass a function as the `fn` helpers first argument, you passed null/);
}
'@test asserts if the provided function accesses `this` without being bound prior to passing to fn'(
assert
) {
if (!HAS_NATIVE_PROXY) {
assert.expect(0);
return;
}
this.render(`{{stash stashedFn=(fn this.myFunc this.arg1)}}`, {
myFunc(arg1) {
return `arg1: ${arg1}, arg2: ${this.arg2}`;
},
arg1: 'foo',
arg2: 'bar',
});
expectAssertion(() => {
this.stashedFn();
}, /You accessed `this.arg2` from a function passed to the `fn` helper, but the function itself was not bound to a valid `this` context. Consider updating to usage of `@action`./);
}
'@test there is no `this` context within the callback'(assert) {
if (DEBUG && HAS_NATIVE_PROXY) {
assert.expect(0);
return;
}
this.render(`{{stash stashedFn=(fn this.myFunc this.arg1)}}`, {
myFunc() {
assert.strictEqual(this, null, 'this is bound to null in production builds');
},
});
this.stashedFn();
}
'@test can use `this` if bound prior to passing to fn'(assert) {
this.render(`{{stash stashedFn=(fn (action this.myFunc) this.arg1)}}`, {
myFunc(arg1) {
return `arg1: ${arg1}, arg2: ${this.arg2}`;
},
arg1: 'foo',
arg2: 'bar',
});
assert.equal(this.stashedFn(), 'arg1: foo, arg2: bar');
}
'@test partially applies each layer when nested [GH#17959]'() {
this.render(`{{invoke (fn (fn (fn this.myFunc this.arg1) this.arg2) this.arg3)}}`, {
myFunc(arg1, arg2, arg3) {
return `arg1: ${arg1}, arg2: ${arg2}, arg3: ${arg3}`;
},
arg1: 'foo',
arg2: 'bar',
arg3: 'qux',
});
this.assertText('arg1: foo, arg2: bar, arg3: qux');
this.assertStableRerender();
runTask(() => set(this.context, 'arg1', 'qux'));
this.assertText('arg1: qux, arg2: bar, arg3: qux');
runTask(() => set(this.context, 'arg2', 'derp'));
this.assertText('arg1: qux, arg2: derp, arg3: qux');
runTask(() => set(this.context, 'arg3', 'huzzah'));
this.assertText('arg1: qux, arg2: derp, arg3: huzzah');
runTask(() => {
set(this.context, 'arg1', 'foo');
set(this.context, 'arg2', 'bar');
set(this.context, 'arg3', 'qux');
});
this.assertText('arg1: foo, arg2: bar, arg3: qux');
}
'@test can be used on the result of `mut`'() {
this.render(`{{this.arg1}}{{stash stashedFn=(fn (mut this.arg1) this.arg2)}}`, {
arg1: 'foo',
arg2: 'bar',
});
this.assertText('foo');
runTask(() => this.stashedFn());
this.assertText('bar');
}
}
);