-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE EMBER_GLIMMER_FN_HELPER] Initial implementation of fn helper.
As proposed in https://emberjs.github.io/rfcs/0470-fn-helper.html.
- Loading branch information
Showing
4 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Arguments, VM } from '@glimmer/runtime'; | ||
import { InternalHelperReference } from '../utils/references'; | ||
import { ICapturedArguments } from '@glimmer/runtime/dist/types/lib/vm/arguments'; | ||
import { assert } from '@ember/debug'; | ||
|
||
function fnHelper({ positional }: ICapturedArguments) { | ||
assert(`You must pass a function as the \`fn\` helpers first argument, you passed ${positional.at(0).value()}`, typeof positional.at(0).value() === 'function'); | ||
|
||
return () => { | ||
let [fn, ...args] = positional.value(); | ||
|
||
return fn; | ||
}; | ||
} | ||
|
||
export default function(_vm: VM, args: Arguments) { | ||
return new InternalHelperReference(fnHelper, args.capture()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/@ember/-internals/glimmer/tests/integration/helpers/fn-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { EMBER_GLIMMER_FN_HELPER } from '@ember/canary-features'; | ||
import { RenderingTestCase, moduleFor, runTask } from 'internal-test-helpers'; | ||
|
||
import { set } from '@ember/-internals/metal'; | ||
|
||
if (EMBER_GLIMMER_FN_HELPER) { | ||
moduleFor( | ||
'Helpers test: {{fn}}', | ||
class extends RenderingTestCase { | ||
beforeEach() { | ||
this.registerHelper('invoke', function([fn]) { | ||
return fn(); | ||
}); | ||
} | ||
|
||
'@test invokes the provided function with arguments'() { | ||
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 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/); | ||
} | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters