Commit d0509bf3 authored by Mark Florian's avatar Mark Florian

Merge branch 'clarify-querying-dom-elements' into 'master'

Clarify testing docs around querying DOM elements

See merge request gitlab-org/gitlab!33190
parents 9766ca6f a23b5acb
...@@ -204,11 +204,11 @@ Following you'll find some general common practices you will find as part of our ...@@ -204,11 +204,11 @@ Following you'll find some general common practices you will find as part of our
### How to query DOM elements ### How to query DOM elements
When it comes to querying DOM elements in your tests, it is best to uniquely target the element, without adding additional attributes specifically for testing purposes. Sometimes this cannot be done feasibly. In these cases, adding test attributes to simplify the selectors might be the best option. When it comes to querying DOM elements in your tests, it is best to uniquely and semantically target the element. Sometimes this cannot be done feasibly. In these cases, adding test attributes to simplify the selectors might be the best option.
Preferentially, in component testing with `@vue/test-utils`, you should query for child components using the component itself. This helps enforce that specific behavior can be covered by that component's individual unit tests. Otherwise, try to use: Preferentially, in component testing with `@vue/test-utils`, you should query for child components using the component itself. This helps enforce that specific behavior can be covered by that component's individual unit tests. Otherwise, try to use:
- A behavioral attribute like `name` (also verifies that `name` was setup properly) - A semantic attribute like `name` (also verifies that `name` was setup properly)
- A `data-testid` attribute ([recommended by maintainers of `@vue/test-utils`](https://github.com/vuejs/vue-test-utils/issues/1498#issuecomment-610133465)) - A `data-testid` attribute ([recommended by maintainers of `@vue/test-utils`](https://github.com/vuejs/vue-test-utils/issues/1498#issuecomment-610133465))
- a Vue `ref` (if using `@vue/test-utils`) - a Vue `ref` (if using `@vue/test-utils`)
...@@ -216,11 +216,17 @@ Examples: ...@@ -216,11 +216,17 @@ Examples:
```javascript ```javascript
it('exists', () => { it('exists', () => {
// Good
wrapper.find(FooComponent); wrapper.find(FooComponent);
wrapper.find('input[name=foo]'); wrapper.find('input[name=foo]');
wrapper.find('[data-testid="foo"]'); wrapper.find('[data-testid="foo"]');
wrapper.find({ ref: 'foo'}); wrapper.find({ ref: 'foo'});
// Bad
wrapper.find('.js-foo'); wrapper.find('.js-foo');
wrapper.find('.btn-primary');
wrapper.find('.qa-foo-component');
wrapper.find('[data-qa-selector="foo"]');
}); });
``` ```
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment