Skip to content

Commit

Permalink
Merge pull request #923 from mydea/fn/safe-component-tests
Browse files Browse the repository at this point in the history
Add documentation how to use safe components in tests
  • Loading branch information
ef4 authored Aug 21, 2021
2 parents 7715504 + a82c13e commit 4f3f7f8
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions REPLACING-COMPONENT-HELPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,49 @@ export default class extends Component {
This code will cause every modules under the `./feed-items/` directory to be eagerly included in your build.

To instead _lazily_ include them, refactor to use asynchronous `import()` instead of `importSync`. BUT CAUTION: using `import()` of your own app code is one of the few things that works _only_ under Embroider and not in classic builds, so don't do it until you have committed to Embroider.

## When using one-off components in tests

If you find yourself defining custom, one-off components to be used in your tests, you might have been using a syntax like this:

```js
import { setComponentTemplate } from '@ember/component';
import Component from '@glimmer/component';

test('my test', async function(assert) {
class TestComponent extends Component {}

setComponentTemplate(
hbs`Test content: {{@message}}`,
TestComponent
);

this.owner.register('component:test-component', TestComponent);

await render(hbs`
<MyComponent @display={{component 'test-component'}} />
`);
});
```

This will fail, as `test-component`cannot be statically found. Instead, you can directly reference the component class:

```js
import { setComponentTemplate } from '@ember/component';
import Component from '@glimmer/component';

test('my test', async function(assert) {
class TestComponent extends Component {}

setComponentTemplate(
hbs`Test content: {{@message}}`,
TestComponent
);

this.testComponent = TestComponent;

await render(hbs`
<MyComponent @display={{this.testComponent}} />
`);
});
```

0 comments on commit 4f3f7f8

Please sign in to comment.