Skip to content

Commit 1c9fdaa

Browse files
committed
README: Simplify and modernize "Usage in Components" section
1 parent c19b042 commit 1c9fdaa

File tree

1 file changed

+14
-93
lines changed

1 file changed

+14
-93
lines changed

README.md

Lines changed: 14 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ automatically removed from `production` builds:
5656
<article>
5757
<h1 data-test-post-title data-test-resource-id={{post.id}}>{{post.title}}</h1>
5858
<p>{{post.body}}</p>
59-
<LikeButton data-test-like-button />
59+
<button data-test-like-button>Like</button>
6060
</article>
6161
```
6262

@@ -69,108 +69,29 @@ assert.dom('[data-test-post-title]').hasText('Ember is great!');
6969
await click('[data-test-like-button]');
7070
```
7171

72-
### Usage in Components
72+
### Usage with Components
7373

74-
You can also use `data-test-*` attributes on components in curly component
75-
invocation:
76-
77-
```handlebars
78-
{{comments-list data-test-comments-for=post.id}}
79-
```
80-
81-
These `data-test-*` attributes will be bound automatically and available
82-
as data attributes on the `<div>` wrapping the component template:
83-
84-
```html
85-
<div id="ember123" data-test-comments-for="42">
86-
<!-- comments -->
87-
</div>
88-
```
89-
90-
### Usage with tagless components
91-
92-
Since tagless components do not have a root element, `data-test-*` attributes
93-
passed to them cannot be bound to the DOM. If you try to pass a `data-test-*`
94-
attribute to a tagless component, or define one in its Javascript class,
95-
`ember-test-selectors` will throw an assertion error.
96-
97-
However, there are some cases where you might want to pass a `data-test-*`
98-
attribute to a tagless component, for example a tagless wrapper component:
99-
100-
```js
101-
// comment-wrapper.js
102-
export default class ComponentWrapper extends Component {
103-
tagName = '';
104-
}
105-
```
74+
You can use the same syntax also for component invocations:
10675

10776
```hbs
108-
{{!-- comment-wrapper.hbs --}}
109-
Comment:
110-
<CommentListItem @comment={{comment}} data-test-comment-id={{data-test-comment-id}} />
77+
<Spinner @color="blue" data-test-spinner>
11178
```
11279

113-
```handlebars
114-
{{!-- comment-list.hbs --}}
115-
{{#each comments as |comment|}}
116-
<CommentWrapper @comment={{comment}} @data-test-comment-id={{comment.id}} />
117-
{{/each}}
118-
```
80+
Inside the `Spinner` component template the `data-test-spinner` attribute will
81+
be applied to the element that has `...attributes` on it, or on the component
82+
wrapper `div` element if you don't use `tagName = ''`.
11983

120-
In this case, to prevent the assertion on the specific `comment-wrapper`
121-
component, you can specify `supportsDataTestProperties` on the class:
84+
If you still use the old curly invocation syntax for components you can pass
85+
`data-test-*` arguments to the components and they will automatically be bound
86+
on the wrapper element too:
12287

123-
```js
124-
// comment-wrapper.js
125-
export default class ComponentWrapper extends Component {
126-
tagName = '';
127-
supportsDataTestProperties = true;
128-
}
129-
```
130-
131-
`supportsDataTestProperties`, like `data-test-*` properties, will be stripped
132-
from the build.
133-
134-
#### With splattributes
135-
Splattributes of angle brackets invocations, introduced in Ember.js 3.4 or
136-
available through the
137-
[ember-angle-bracket-invocation-polyfill](https://github.com/rwjblue/ember-angle-bracket-invocation-polyfill)
138-
for earlier versions of Ember.js, work well with test selectors and tagless
139-
components.
140-
141-
For instance, we have a `SpecialButton` that wraps a `<button>` element. We can
142-
then pass arbitrary test selectors through splattributes, as follows:
143-
144-
```js
145-
// special-button.js
146-
export default class SpecialButton extends Component {
147-
tagName = '';
148-
}
149-
```
150-
151-
```hbs
152-
{{!-- special-button.hbs --}}
153-
<button ...attributes>
154-
{{yield}}
155-
</button>
156-
```
157-
158-
Then
159-
160-
```hbs
161-
<SpecialButton data-test-description="invite-user">
162-
Invite
163-
</SpecialButton>
88+
```handlebars
89+
{{spinner color="blue" data-test-spinner=true}}
16490
```
16591

166-
will yield
167-
92+
Please note that this only works for components based on `@ember/component`,
93+
but not `@glimmer/component`.
16894

169-
```html
170-
<button data-test-description="invite-user">
171-
Invite
172-
</button>
173-
```
17495

17596
### Usage in Ember addons
17697

0 commit comments

Comments
 (0)