forked from emberjs/ember.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bugfix release] Allow class-based helpers in strict-mode
- Loading branch information
1 parent
19fcd09
commit 6bf0153
Showing
2 changed files
with
79 additions
and
1 deletion.
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
78 changes: 78 additions & 0 deletions
78
packages/@ember/-internals/glimmer/tests/integration/custom-helper-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,78 @@ | ||
import { RenderingTestCase, moduleFor, strip } from 'internal-test-helpers'; | ||
|
||
import { precompileJSON } from '@glimmer/compiler'; | ||
import { getTemplateLocals } from '@glimmer/syntax'; | ||
import { createTemplateFactory } from '@ember/template-factory'; | ||
|
||
import { Helper } from '../../index'; | ||
import { setComponentTemplate } from '@glimmer/manager'; | ||
import { templateOnlyComponent } from '@glimmer/runtime'; | ||
|
||
// eslint-disable-next-line ember-internal/require-yuidoc-access | ||
/** | ||
* The template-compiler does not support strict mode at this time. | ||
* precompile from ember-template-compiler returns a string and | ||
* not a template-factory, so it doesn't help with strict-mode testing. | ||
* | ||
* We also can't import from `@ember/template-compiler` because it | ||
* doesn't exist to this kind of test, otherwise we'd be able to use | ||
* precompileTemplate, which would be perfect :D | ||
* | ||
* Copied(ish) from https://github.com/NullVoxPopuli/ember-repl/blob/main/addon/hbs.ts#L51 | ||
*/ | ||
function precompileTemplate(source, { moduleName, scope = {} }) { | ||
let locals = getTemplateLocals(source); | ||
|
||
let options = { | ||
strictMode: true, | ||
moduleName, | ||
locals, | ||
isProduction: false, | ||
meta: { moduleName }, | ||
}; | ||
|
||
// Copied from @glimmer/compiler/lib/compiler#precompile | ||
let [block, usedLocals] = precompileJSON(source, options); | ||
let usedScope = usedLocals.map((key) => scope[key]); | ||
|
||
let blockJSON = JSON.stringify(block); | ||
let templateJSONObject = { | ||
id: moduleName, | ||
block: blockJSON, | ||
moduleName: moduleName ?? '(unknown template module)', | ||
scope: () => usedScope, | ||
isStrictMode: true, | ||
}; | ||
|
||
let factory = createTemplateFactory(templateJSONObject); | ||
|
||
return factory; | ||
} | ||
|
||
moduleFor( | ||
'Custom Helper test', | ||
class extends RenderingTestCase { | ||
['@test works with strict-mode']() { | ||
class Custom extends Helper { | ||
compute([value]) { | ||
return `${value}-custom`; | ||
} | ||
} | ||
|
||
let template = strip` | ||
{{ (Custom 'my-test') }} | ||
`; | ||
|
||
let templateFactory = precompileTemplate(template, { | ||
moduleName: 'strict-mode', | ||
scope: { Custom }, | ||
}); | ||
|
||
let TestComponent = setComponentTemplate(templateFactory, templateOnlyComponent()); | ||
|
||
this.render(`<this.TestComponent />`, { TestComponent }); | ||
this.assertText('my-test-custom'); | ||
this.assertStableRerender(); | ||
} | ||
} | ||
); |