Skip to content

Commit df49ca9

Browse files
committed
test: 💍 add missing byTestId tests
1 parent 568e9ef commit df49ca9

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

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)