|
| 1 | +import { RenderingTestCase, moduleFor, strip } from 'internal-test-helpers'; |
| 2 | + |
| 3 | +import { precompileJSON } from '@glimmer/compiler'; |
| 4 | +import { getTemplateLocals } from '@glimmer/syntax'; |
| 5 | +import { createTemplateFactory } from '@ember/template-factory'; |
| 6 | + |
| 7 | +import { Helper } from '../../index'; |
| 8 | +import { setComponentTemplate } from '@glimmer/manager'; |
| 9 | +import { templateOnlyComponent } from '@glimmer/runtime'; |
| 10 | + |
| 11 | +// eslint-disable-next-line ember-internal/require-yuidoc-access |
| 12 | +/** |
| 13 | + * The template-compiler does not support strict mode at this time. |
| 14 | + * precompile from ember-template-compiler returns a string and |
| 15 | + * not a template-factory, so it doesn't help with strict-mode testing. |
| 16 | + * |
| 17 | + * We also can't import from `@ember/template-compiler` because it |
| 18 | + * doesn't exist to this kind of test, otherwise we'd be able to use |
| 19 | + * precompileTemplate, which would be perfect :D |
| 20 | + * |
| 21 | + * Copied(ish) from https://github.com/NullVoxPopuli/ember-repl/blob/main/addon/hbs.ts#L51 |
| 22 | + */ |
| 23 | +function precompileTemplate(source, { moduleName, scope = {} }) { |
| 24 | + let locals = getTemplateLocals(source); |
| 25 | + |
| 26 | + let options = { |
| 27 | + strictMode: true, |
| 28 | + moduleName, |
| 29 | + locals, |
| 30 | + isProduction: false, |
| 31 | + meta: { moduleName }, |
| 32 | + }; |
| 33 | + |
| 34 | + // Copied from @glimmer/compiler/lib/compiler#precompile |
| 35 | + let [block, usedLocals] = precompileJSON(source, options); |
| 36 | + let usedScope = usedLocals.map((key) => scope[key]); |
| 37 | + |
| 38 | + let blockJSON = JSON.stringify(block); |
| 39 | + let templateJSONObject = { |
| 40 | + id: moduleName, |
| 41 | + block: blockJSON, |
| 42 | + moduleName: moduleName ?? '(unknown template module)', |
| 43 | + scope: () => usedScope, |
| 44 | + isStrictMode: true, |
| 45 | + }; |
| 46 | + |
| 47 | + let factory = createTemplateFactory(templateJSONObject); |
| 48 | + |
| 49 | + return factory; |
| 50 | +} |
| 51 | + |
| 52 | +moduleFor( |
| 53 | + 'Custom Helper test', |
| 54 | + class extends RenderingTestCase { |
| 55 | + ['@test works with strict-mode']() { |
| 56 | + class Custom extends Helper { |
| 57 | + compute([value]) { |
| 58 | + return `${value}-custom`; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + let template = strip` |
| 63 | + {{ (Custom 'my-test') }} |
| 64 | + `; |
| 65 | + |
| 66 | + let templateFactory = precompileTemplate(template, { |
| 67 | + moduleName: 'strict-mode', |
| 68 | + scope: { Custom }, |
| 69 | + }); |
| 70 | + |
| 71 | + let TestComponent = setComponentTemplate(templateFactory, templateOnlyComponent()); |
| 72 | + |
| 73 | + this.render(`<this.TestComponent />`, { TestComponent }); |
| 74 | + this.assertText('my-test-custom'); |
| 75 | + this.assertStableRerender(); |
| 76 | + } |
| 77 | + } |
| 78 | +); |
0 commit comments