Skip to content

Commit ea73b2a

Browse files
authored
Merge branch 'master' into pr/aria-expanded
2 parents 3e09253 + 21c4db3 commit ea73b2a

23 files changed

+300
-92
lines changed

.all-contributorsrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,8 @@
10191019
"contributions": [
10201020
"doc",
10211021
"code",
1022-
"test"
1022+
"test",
1023+
"review"
10231024
]
10241025
},
10251026
{

.github/workflows/validate.yml

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@ name: validate
22
on:
33
push:
44
branches:
5-
[
6-
'+([0-9])?(.{+([0-9]),x}).x',
7-
'master',
8-
'next',
9-
'next-major',
10-
'beta',
11-
'alpha',
12-
'!all-contributors/**',
13-
]
14-
pull_request:
15-
branches-ignore: ['all-contributors/**']
5+
- '+([0-9])?(.{+([0-9]),x}).x'
6+
- 'master'
7+
- 'next'
8+
- 'next-major'
9+
- 'beta'
10+
- 'alpha'
11+
- '!all-contributors/**'
12+
pull_request: {}
1613
jobs:
1714
main:
15+
# ignore all-contributors PRs
16+
if: ${{ !contains(github.head_ref, 'all-contributors') }}
1817
strategy:
1918
matrix:
2019
node: [10.14.2, 12, 14, 15]
2120
runs-on: ubuntu-latest
2221
steps:
22+
- name: 🛑 Cancel Previous Runs
23+
uses: styfle/[email protected]
24+
with:
25+
access_token: ${{ secrets.GITHUB_TOKEN }}
26+
2327
- name: ⬇️ Checkout repo
2428
uses: actions/checkout@v2
2529

@@ -47,6 +51,11 @@ jobs:
4751
contains('refs/heads/master,refs/heads/beta,refs/heads/next,refs/heads/alpha',
4852
github.ref) && github.event_name == 'push' }}
4953
steps:
54+
- name: 🛑 Cancel Previous Runs
55+
uses: styfle/[email protected]
56+
with:
57+
access_token: ${{ secrets.GITHUB_TOKEN }}
58+
5059
- name: ⬇️ Checkout repo
5160
uses: actions/checkout@v2
5261

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ Thanks goes to these people ([emoji key][emojis]):
262262
<td align="center"><a href="https://github.com/benmonro"><img src="https://avatars3.githubusercontent.com/u/399236?v=4" width="100px;" alt=""/><br /><sub><b>Ben Monro</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=benmonro" title="Code">💻</a> <a href="#ideas-benmonro" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=benmonro" title="Tests">⚠️</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=benmonro" title="Documentation">📖</a></td>
263263
<td align="center"><a href="https://github.com/smeijer"><img src="https://avatars1.githubusercontent.com/u/1196524?v=4" width="100px;" alt=""/><br /><sub><b>Stephan Meijer</b></sub></a><br /><a href="#ideas-smeijer" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=smeijer" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=smeijer" title="Tests">⚠️</a></td>
264264
<td align="center"><a href="https://joaoforja.com/"><img src="https://avatars2.githubusercontent.com/u/7002157?v=4" width="100px;" alt=""/><br /><sub><b>João Forja</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=Jnforja" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=Jnforja" title="Tests">⚠️</a></td>
265-
<td align="center"><a href="https://nickmccurdy.com/"><img src="https://avatars0.githubusercontent.com/u/927220?v=4" width="100px;" alt=""/><br /><sub><b>Nick McCurdy</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=nickmccurdy" title="Documentation">📖</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=nickmccurdy" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=nickmccurdy" title="Tests">⚠️</a></td>
265+
<td align="center"><a href="https://nickmccurdy.com/"><img src="https://avatars0.githubusercontent.com/u/927220?v=4" width="100px;" alt=""/><br /><sub><b>Nick McCurdy</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=nickmccurdy" title="Documentation">📖</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=nickmccurdy" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=nickmccurdy" title="Tests">⚠️</a> <a href="https://github.com/testing-library/dom-testing-library/pulls?q=is%3Apr+reviewed-by%3Anickmccurdy" title="Reviewed Pull Requests">👀</a></td>
266266
<td align="center"><a href="http://calebmer.com"><img src="https://avatars1.githubusercontent.com/u/8282507?v=4" width="100px;" alt=""/><br /><sub><b>Caleb Meredith</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=calebmer" title="Code">💻</a></td>
267267
<td align="center"><a href="https://github.com/marcosvega91"><img src="https://avatars2.githubusercontent.com/u/5365582?v=4" width="100px;" alt=""/><br /><sub><b>Marco Moretti</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=marcosvega91" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=marcosvega91" title="Tests">⚠️</a> <a href="https://github.com/testing-library/dom-testing-library/pulls?q=is%3Apr+reviewed-by%3Amarcosvega91" title="Reviewed Pull Requests">👀</a></td>
268268
</tr>
@@ -304,7 +304,6 @@ Thanks goes to these people ([emoji key][emojis]):
304304

305305
<!-- markdownlint-enable -->
306306
<!-- prettier-ignore-end -->
307-
308307
<!-- ALL-CONTRIBUTORS-LIST:END -->
309308

310309
This project follows the [all-contributors][all-contributors] specification.

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"test:debug": "node --inspect-brk ./node_modules/.bin/jest --watch --runInBand",
3232
"test:update": "npm test -- --updateSnapshot --coverage",
3333
"validate": "kcd-scripts validate",
34-
"typecheck": "dtslint ./types/"
34+
"typecheck": "kcd-scripts typecheck --build types"
3535
},
3636
"files": [
3737
"dist",
@@ -48,15 +48,13 @@
4848
"pretty-format": "^26.6.2"
4949
},
5050
"devDependencies": {
51-
"@testing-library/jest-dom": "^5.11.5",
52-
"@types/estree": "0.0.45",
53-
"dtslint": "^4.0.5",
51+
"@testing-library/jest-dom": "^5.11.6",
5452
"jest-in-case": "^1.0.2",
5553
"jest-serializer-ansi": "^1.0.3",
5654
"jest-watch-select-projects": "^2.0.0",
5755
"jsdom": "^16.4.0",
58-
"kcd-scripts": "^6.7.0",
59-
"typescript": "^4.0.5"
56+
"kcd-scripts": "^7.5.1",
57+
"typescript": "^4.1.2"
6058
},
6159
"eslintConfig": {
6260
"extends": "./node_modules/kcd-scripts/eslint.js",

src/__tests__/.eslintrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"rules": {
3+
// this rule doesn't properly detect that our custom `render` does not
4+
// insert elements into the document.
5+
"jest-dom/prefer-in-document": "off"
6+
}
7+
}

src/__tests__/element-queries.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ test('matches case with RegExp matcher', () => {
144144
expect(queryByText(/Step 1 of 4/)).not.toBeTruthy()
145145
})
146146

147+
test('queryByText matches case with non-string matcher', () => {
148+
const {queryByText} = render(`<span>1</span>`)
149+
expect(queryByText(1)).toBeTruthy()
150+
})
151+
147152
test('can get form controls by label text', () => {
148153
const {getByLabelText} = render(`
149154
<div>
@@ -338,6 +343,11 @@ test('get can get form controls by placeholder', () => {
338343
expect(getByPlaceholderText('username').id).toBe('username-id')
339344
})
340345

346+
test('queryByPlaceholderText matches case with non-string matcher', () => {
347+
const {queryByPlaceholderText} = render(`<input placeholder="1" />`)
348+
expect(queryByPlaceholderText(1)).toBeTruthy()
349+
})
350+
341351
test('label with no form control', () => {
342352
const {getByLabelText, queryByLabelText} = render(`<label>All alone</label>`)
343353
expect(queryByLabelText(/alone/)).toBeNull()
@@ -535,6 +545,11 @@ test('getByLabelText with aria-label', () => {
535545
expect(queryByLabelText(/bat/)).toBeTruthy()
536546
})
537547

548+
test('queryByLabelText matches case with non-string matcher', () => {
549+
const {queryByLabelText} = render(`<input aria-label="1" />`)
550+
expect(queryByLabelText(1)).toBeTruthy()
551+
})
552+
538553
test('get element by its alt text', () => {
539554
const {getByAltText} = render(`
540555
<div>
@@ -545,6 +560,11 @@ test('get element by its alt text', () => {
545560
expect(getByAltText(/fin.*nem.*poster$/i).src).toContain('/finding-nemo.png')
546561
})
547562

563+
test('queryByAltText matches case with non-string matcher', () => {
564+
const {queryByAltText} = render(`<img alt="1" src="/finding-nemo.png" />`)
565+
expect(queryByAltText(1)).toBeTruthy()
566+
})
567+
548568
test('query/get element by its title', () => {
549569
const {getByTitle, queryByTitle} = render(`
550570
<div>
@@ -577,6 +597,11 @@ test('query/get title element of SVG', () => {
577597
expect(queryByTitle('Close').id).toEqual('svg-title')
578598
})
579599

600+
test('queryByTitle matches case with non-string matcher', () => {
601+
const {queryByTitle} = render(`<span title="1" />`)
602+
expect(queryByTitle(1)).toBeTruthy()
603+
})
604+
580605
test('query/get element by its value', () => {
581606
const {getByDisplayValue, queryByDisplayValue} = render(`
582607
<div>
@@ -632,6 +657,15 @@ test('query/get select by text with multiple options selected', () => {
632657
expect(queryByDisplayValue('Alaska').id).toEqual('state-select')
633658
})
634659

660+
test('queryByDisplayValue matches case with non-string matcher', () => {
661+
const {queryByDisplayValue} = render(`
662+
<select multiple id="state-select">
663+
<option selected value="one">1</option>
664+
</select>
665+
`)
666+
expect(queryByDisplayValue(1)).toBeTruthy()
667+
})
668+
635669
describe('query by test id', () => {
636670
afterEach(() => {
637671
// Restore the default test id attribute
@@ -651,6 +685,11 @@ describe('query by test id', () => {
651685
expect(queryByTestId('first-name')).not.toBeTruthy()
652686
})
653687

688+
test('queryByTestId matches case with non-string matcher', () => {
689+
const {queryByTestId} = render(`<span data-testid="1" />`)
690+
expect(queryByTestId(1)).toBeTruthy()
691+
})
692+
654693
test('can override test id attribute', () => {
655694
const {queryByTestId} = render(`<div data-my-test-id="theTestId"></div>`)
656695

@@ -732,6 +771,11 @@ test('queryAllByRole returns semantic html elements', () => {
732771
expect(queryAllByRole('listbox')).toHaveLength(1)
733772
})
734773

774+
test('queryByRole matches case with non-string matcher', () => {
775+
const {queryByRole} = render(`<span role="1" />`)
776+
expect(queryByRole(1)).toBeTruthy()
777+
})
778+
735779
test('getAll* matchers return an array', () => {
736780
const {
737781
getAllByAltText,

src/__tests__/events.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ test('assigns target properties', () => {
252252
node.addEventListener('change', spy)
253253
fireEvent.change(node, {target: {value}})
254254
expect(spy).toHaveBeenCalledTimes(1)
255-
expect(node.value).toBe(value)
255+
expect(node).toHaveValue(value)
256256
})
257257

258258
test('assigns selection-related target properties', () => {
@@ -263,7 +263,7 @@ test('assigns selection-related target properties', () => {
263263
const selectionEnd = 2
264264
node.addEventListener('change', spy)
265265
fireEvent.change(node, {target: {value, selectionStart, selectionEnd}})
266-
expect(node.value).toBe(value)
266+
expect(node).toHaveValue(value)
267267
expect(node.selectionStart).toBe(selectionStart)
268268
expect(node.selectionEnd).toBe(selectionEnd)
269269
})
@@ -299,12 +299,12 @@ test('assigns dataTransfer non-enumerable properties', () => {
299299
window.DataTransfer = function DataTransfer() {}
300300
const node = document.createElement('div')
301301
const spy = jest.fn()
302-
const item = {};
303-
const dataTransfer = new window.DataTransfer();
302+
const item = {}
303+
const dataTransfer = new window.DataTransfer()
304304

305305
Object.defineProperty(dataTransfer, 'items', {
306306
value: [item],
307-
enumerable: false
307+
enumerable: false,
308308
})
309309
node.addEventListener('drop', spy)
310310
fireEvent.drop(node, {dataTransfer})

src/__tests__/fake-timers.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import {waitFor, waitForElementToBeRemoved} from '..'
22
import {render} from './helpers/test-utils'
33

4-
beforeAll(() => {
5-
jest.useFakeTimers()
6-
})
7-
8-
afterAll(() => {
9-
jest.useRealTimers()
10-
})
11-
124
async function runWaitFor({time = 300} = {}, options) {
135
const response = 'data'
146
const doAsyncThing = () =>
@@ -48,6 +40,7 @@ test('fake timer timeout', async () => {
4840
})
4941

5042
test('times out after 1000ms by default', async () => {
43+
jest.useFakeTimers()
5144
const {container} = render(`<div></div>`)
5245
const start = performance.now()
5346
// there's a bug with this rule here...
@@ -66,6 +59,7 @@ test('times out after 1000ms by default', async () => {
6659
})
6760

6861
test('recursive timers do not cause issues', async () => {
62+
jest.useFakeTimers()
6963
let recurse = true
7064
function startTimer() {
7165
setTimeout(() => {

src/__tests__/role.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,8 @@ describe('configuration', () => {
543543
}
544544
})
545545
})
546+
547+
test('should find the input using type property instead of attribute', () => {
548+
const {getByRole} = render('<input type="124">')
549+
expect(getByRole('textbox')).not.toBeNull()
550+
})

src/__tests__/suggestions.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,16 @@ test('should suggest getByLabelText when no role available', () => {
232232
)
233233
})
234234

235+
it('should not suggest by label when using by label', async () => {
236+
renderIntoDocument(
237+
`<label><span>bar</span><input type="password" title="foo" /></label>`,
238+
)
239+
240+
// if a suggestion is made, this call will throw, thus failing the test.
241+
const password = await screen.findByLabelText(/bar/i)
242+
expect(password).toHaveAttribute('type', 'password')
243+
})
244+
235245
test(`should suggest getByLabel on non form elements`, () => {
236246
renderIntoDocument(`
237247
<div data-testid="foo" aria-labelledby="section-one-header">
@@ -551,14 +561,44 @@ test('should get the first label with aria-labelledby contains multiple ids', ()
551561
})
552562
})
553563

554-
test('should suggest hidden option if element is not in the accessibilty tree', () => {
555-
const {container} = renderIntoDocument(`
564+
test('should not suggest or warn about hidden element when suggested query is already used.', () => {
565+
console.warn.mockImplementation(() => {})
566+
567+
renderIntoDocument(`
556568
<input type="text" aria-hidden=true />
557569
`)
558570

559-
expect(
560-
getSuggestedQuery(container.querySelector('input'), 'get', 'role'),
561-
).toMatchObject({
571+
expect(() => screen.getByRole('textbox', {hidden: true})).not.toThrowError()
572+
expect(console.warn).not.toHaveBeenCalled()
573+
})
574+
test('should suggest and warn about if element is not in the accessibility tree', () => {
575+
console.warn.mockImplementation(() => {})
576+
577+
renderIntoDocument(`
578+
<input type="text" data-testid="foo" aria-hidden=true />
579+
`)
580+
581+
expect(() => screen.getByTestId('foo', {hidden: true})).toThrowError(
582+
/getByRole\('textbox', \{ hidden: true \}\)/,
583+
)
584+
expect(console.warn).toHaveBeenCalledWith(
585+
expect.stringContaining(`Element is inaccessible.`),
586+
)
587+
})
588+
589+
test('should suggest hidden option if element is not in the accessibility tree', () => {
590+
console.warn.mockImplementation(() => {})
591+
592+
const {container} = renderIntoDocument(`
593+
<input type="text" data-testid="foo" aria-hidden=true />
594+
`)
595+
596+
const suggestion = getSuggestedQuery(
597+
container.querySelector('input'),
598+
'get',
599+
'role',
600+
)
601+
expect(suggestion).toMatchObject({
562602
queryName: 'Role',
563603
queryMethod: 'getByRole',
564604
queryArgs: ['textbox', {hidden: true}],
@@ -567,6 +607,7 @@ test('should suggest hidden option if element is not in the accessibilty tree',
567607
If you are using the aria-hidden prop, make sure this is the right choice for your case.
568608
`,
569609
})
610+
suggestion.toString()
570611

571612
expect(console.warn.mock.calls).toMatchInlineSnapshot(`
572613
Array [

0 commit comments

Comments
 (0)