Skip to content

Commit 549c63c

Browse files
authored
docs: ✏️ byTestId DOM selector (#726)
* docs: ✏️ add byTestId DOM selector * test: 💍 add missing byTestId tests
1 parent 7b7039e commit 549c63c

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

docs/docs/queries.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ spectator.query(byText('By text'));
4444
spectator.query(byText('By text', {selector: '#some .selector'}));
4545
spectator.query(byTextContent('By text content', {selector: '#some .selector'}));
4646
spectator.query(byRole('checkbox', { checked: true }));
47+
spectator.query(byTestId('someTestId'))
4748
```
4849

4950
The difference between `byText` and `byTextContent` is that the former doesn't match text inside a nested elements.
@@ -64,6 +65,14 @@ expect(byPlaceholder('Please enter your email address')).toHaveValue('my-value')
6465
// ... See the Custom Matchers section for a full list of possible matchers
6566
```
6667

68+
Configure `@testing-library/dom` to use `byTestId` with a different `testIdAttribute`
69+
70+
```ts
71+
import { configure } from '@testing-library/dom';
72+
73+
configure({ testIdAttribute: 'data-test' });
74+
```
75+
6776
### Parent Selector
6877
Spectator allows you to query for nested elements within a parent element. This is useful when you have multiple instances of the same component on the page and you want to query for children within a specific one. The parent selector is a string selector that is used to find the parent element. The parent selector is passed as the second parameter to the query methods. For example:
6978
```ts

projects/spectator/jest/test/dom-selectors/dom-selectors.component.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
byTitle,
99
byValue,
1010
byTextContent,
11+
byTestId,
1112
} from '@ngneat/spectator/jest';
13+
import { configure, getConfig } from '@testing-library/dom';
1214

1315
import { DomSelectorsComponent, DomSelectorsNestedComponent } from '../../../test/dom-selectors/dom-selectors.component';
1416

@@ -170,4 +172,23 @@ describe('DomSelectorsComponent', () => {
170172
});
171173
});
172174
});
175+
176+
describe('byTestId', () => {
177+
const { testIdAttribute } = getConfig();
178+
beforeEach(() => configure({ testIdAttribute }));
179+
afterEach(() => configure({ testIdAttribute }));
180+
181+
it('should allow querying with byTestId (default: data-testid)', () => {
182+
const element = spectator.query(byTestId('by-testid-default'));
183+
expect(element).toHaveAttribute('data-testid', 'by-testid-default');
184+
});
185+
186+
it('should allow querying with byTestId and custom testIdAttribute', () => {
187+
// configure byTestId to use the custom attribute
188+
configure({ testIdAttribute: 'data-testid-custom' });
189+
190+
const element = spectator.query(byTestId('by-testid-custom'));
191+
expect(element).toHaveAttribute('data-testid-custom', 'by-testid-custom');
192+
});
193+
});
173194
});

projects/spectator/test/dom-selectors/dom-selectors.component.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
byAltText,
33
byLabel,
44
byPlaceholder,
5+
byTestId,
56
byText,
67
byTextContent,
78
byTitle,
@@ -10,6 +11,7 @@ import {
1011
createComponentFactory,
1112
Spectator,
1213
} from '@ngneat/spectator';
14+
import { configure, getConfig } from '@testing-library/dom';
1315

1416
import { DomSelectorsComponent, DomSelectorsNestedComponent } from './dom-selectors.component';
1517

@@ -216,4 +218,23 @@ describe('DomSelectorsComponent', () => {
216218
expect(element).toHaveExactText('Sugar');
217219
});
218220
});
221+
222+
describe('byTestId', () => {
223+
const { testIdAttribute } = getConfig();
224+
beforeEach(() => configure({ testIdAttribute }));
225+
afterEach(() => configure({ testIdAttribute }));
226+
227+
it('should allow querying with byTestId (default: data-testid)', () => {
228+
const element = spectator.query(byTestId('by-testid-default'));
229+
expect(element).toHaveAttribute('data-testid', 'by-testid-default');
230+
});
231+
232+
it('should allow querying with byTestId and custom testIdAttribute', () => {
233+
// configure byTestId to use the custom attribute
234+
configure({ testIdAttribute: 'data-testid-custom' });
235+
236+
const element = spectator.query(byTestId('by-testid-custom'));
237+
expect(element).toHaveAttribute('data-testid-custom', 'by-testid-custom');
238+
});
239+
});
219240
});

projects/spectator/test/dom-selectors/dom-selectors.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export class DomSelectorsNestedComponent {}
5252
<app-dom-selectors-nested-components id="first"></app-dom-selectors-nested-components>
5353
<app-dom-selectors-nested-components id="last"></app-dom-selectors-nested-components>
5454
</div>
55+
56+
<div id="testid">
57+
<div data-testid="by-testid-default"></div>
58+
<div data-testid-custom="by-testid-custom"></div>
59+
</div>
5560
`,
5661
standalone: false,
5762
imports: [DomSelectorsNestedComponent],

projects/spectator/vitest/test/dom-selectors/dom-selectors.component.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
byTitle,
99
byValue,
1010
byTextContent,
11+
byTestId,
1112
} from '@ngneat/spectator/vitest';
13+
import { configure, getConfig } from '@testing-library/dom';
1214

1315
import { DomSelectorsComponent, DomSelectorsNestedComponent } from '../../../test/dom-selectors/dom-selectors.component';
1416

@@ -170,4 +172,23 @@ describe('DomSelectorsComponent', () => {
170172
});
171173
});
172174
});
175+
176+
describe('byTestId', () => {
177+
const { testIdAttribute } = getConfig();
178+
beforeEach(() => configure({ testIdAttribute }));
179+
afterEach(() => configure({ testIdAttribute }));
180+
181+
it('should allow querying with byTestId (default: data-testid)', () => {
182+
const element = spectator.query(byTestId('by-testid-default'));
183+
expect(element).toHaveAttribute('data-testid', 'by-testid-default');
184+
});
185+
186+
it('should allow querying with byTestId and custom testIdAttribute', () => {
187+
// configure byTestId to use the custom attribute
188+
configure({ testIdAttribute: 'data-testid-custom' });
189+
190+
const element = spectator.query(byTestId('by-testid-custom'));
191+
expect(element).toHaveAttribute('data-testid-custom', 'by-testid-custom');
192+
});
193+
});
173194
});

0 commit comments

Comments
 (0)