Skip to content

Commit

Permalink
support invoking components
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jan 9, 2022
1 parent 9c6e884 commit baee787
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
22 changes: 20 additions & 2 deletions addon-test-support/@ember/test-helpers/setup-rendering-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,33 @@ export interface RenderOptions {
Renders the provided template and appends it to the DOM.
@public
@param {CompiledTemplate} template the template to render
@param {TemplateFactory | unknown} renderable the template or component to render
@param {RenderOptions} options options hash containing engine owner ({ owner: engineOwner })
@returns {Promise<void>} resolves when settled
*/
export function render(
template: TemplateFactory,
renderable: TemplateFactory | unknown,
options?: RenderOptions
): Promise<void> {
let context = getContext();
let template: TemplateFactory;

if (
typeof renderable === 'function' &&
renderable.name === 'factory' &&
'__meta' in renderable
) {
template = renderable as TemplateFactory;
} else {
// NOTE: this only works with [email protected]+
if (!context) {
throw new Error(
'cannot render strict mode templates outside of a test context'
);
}
context.setProperties({ __renderable__: renderable });
template = hbs`<this.__renderable__ />`;
}

if (!template) {
throw new Error('you must pass a template to `render()`');
Expand Down
14 changes: 13 additions & 1 deletion tests/integration/setup-rendering-context-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Ember from 'ember';
import { module, test } from 'qunit';
import Component from '@ember/component';
import Component, { setComponentTemplate } from '@ember/component';
import templateOnly from '@ember/component/template-only';
import { helper } from '@ember/component/helper';
import { registerWaiter } from '@ember/test';
import {
Expand Down Expand Up @@ -157,4 +158,15 @@ module('setupRenderingContext "real world"', function (hooks) {
});
}
});

module('<template> transpilation support', function () {
if (hasEmberVersion(3, 25)) {
test('can render components', async function (assert) {
let myComponent = setComponentTemplate(hbs`4`, templateOnly());
await render(myComponent);

assert.equal(this.element.textContent, '4');
});
}
});
});

0 comments on commit baee787

Please sign in to comment.