Skip to content
This repository has been archived by the owner on May 26, 2019. It is now read-only.

Testing component blocks #158

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions source/testing/testing-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,41 @@ test('trigger external action when button is clicked', function(assert) {
});
```

### Testing component blocks and defaults

Often you might define a component which works in block form:

```app/templates/components/my-component.hbs
{{#if template}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We intend to add hasBlock? helper for this, I am unsure that we should suggest this (though checking template will likely always work).

{{yield}}
{{else}}
{{defaultContent}}
{{/if}}
```

Now this shows `{{defaultContent}}` when using `{{my-component}}` otherwise it shows the content within `{{#my-component}}custom content{{/my-component}}`.

To test this functionality, we can do the following:

```tests/unit/components/my-component-test.js
test('component with optional block', function(assert) {
assert.expect(1);

var component = this.subject();

Ember.run(function() {
component.set('template', function() {
return 'Something else';
});
component.set('defaultContent', 'foo');
});

assert.trimEq(this.$().text(), 'Something else');
});
```

Notice that `template` is actually a function that returns a string.

<!---
### Components Using Other Components

Expand Down