Skip to content

Commit

Permalink
Add failing test using internal ember testing tools
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Dec 18, 2021
1 parent 99189eb commit a20c870
Showing 1 changed file with 78 additions and 0 deletions.
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';

/**
* 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] = precompileJSON(source, options);

let blockJSON = JSON.stringify(block);
let templateJSONObject = {
id: moduleName,
block: blockJSON,
moduleName: moduleName ?? '(unknown template module)',
scope,
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 precompiled = precompileTemplate(template, {
moduleName: 'strict-mode',
scope: () => ({ Custom }),
});

let strictMode = setComponentTemplate(precompiled, templateOnlyComponent());

this.render(`<this.strictMode />`, {
strictMode,
});
this.assertText('my-test-custom');
this.assertStableRerender();
}
}
);

0 comments on commit a20c870

Please sign in to comment.