Skip to content

Commit

Permalink
add documentation how to use safe components in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Aug 18, 2021
1 parent 28460bf commit afe985f
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={{component this.testComponent}} />
`);
});
```

0 comments on commit afe985f

Please sign in to comment.