Skip to content

Commit 170b5ed

Browse files
jeanchunggnapse
authored andcommitted
fix: recognize changed value of select (#126)
1 parent c975afb commit 170b5ed

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@
267267
"contributions": [
268268
"code"
269269
]
270+
},
271+
{
272+
"login": "jeanchung",
273+
"name": "Jean Chung",
274+
"avatar_url": "https://avatars0.githubusercontent.com/u/10778036?v=4",
275+
"profile": "https://github.com/jeanchung",
276+
"contributions": [
277+
"code",
278+
"test"
279+
]
270280
}
271281
]
272282
}

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
[![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
2222
[![MIT License][license-badge]][license]
2323

24-
[![All Contributors](https://img.shields.io/badge/all_contributors-26-orange.svg?style=flat-square)](#contributors)
24+
[![All Contributors](https://img.shields.io/badge/all_contributors-27-orange.svg?style=flat-square)](#contributors-)
2525
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
2626

2727
[![Watch on GitHub][github-watch-badge]][github-watch]
@@ -115,8 +115,8 @@ expect.extend({toBeInTheDocument, toHaveClass})
115115
`@testing-library/jest-dom` can work with any library or framework that returns
116116
DOM elements from queries. The custom matcher examples below demonstrate using
117117
`document.querySelector` and
118-
[DOM Testing Library](https://github.com/testing-library/dom-testing-library) for
119-
querying DOM elements.
118+
[DOM Testing Library](https://github.com/testing-library/dom-testing-library)
119+
for querying DOM elements.
120120

121121
### `toBeDisabled`
122122

@@ -989,8 +989,8 @@ here!
989989
> [The more your tests resemble the way your software is used, the more
990990
> confidence they can give you.][guiding-principle]
991991
992-
This library follows the same guiding principles as its mother library
993-
[DOM Testing Library][]. Go [check them out][guiding-principle] for more details.
992+
This library follows the same guiding principles as its mother library [DOM
993+
Testing Library][]. Go [check them out][guiding-principle] for more details.
994994
995995
Additionally, with respect to custom DOM matchers, this library aims to maintain
996996
a minimal but useful set of them, while avoiding bloating itself with merely
@@ -1004,7 +1004,8 @@ more verbose, less clear in its intent, and/or harder to read.
10041004
Thanks goes to these people ([emoji key][emojis]):
10051005
10061006
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
1007-
<!-- prettier-ignore -->
1007+
<!-- prettier-ignore-start -->
1008+
<!-- markdownlint-disable -->
10081009
<table>
10091010
<tr>
10101011
<td align="center"><a href="https://kentcdodds.com"><img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;" alt="Kent C. Dodds"/><br /><sub><b>Kent C. Dodds</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=kentcdodds" title="Code">💻</a> <a href="https://github.com/testing-library/jest-dom/commits?author=kentcdodds" title="Documentation">📖</a> <a href="#infra-kentcdodds" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/testing-library/jest-dom/commits?author=kentcdodds" title="Tests">⚠️</a></td>
@@ -1039,9 +1040,13 @@ Thanks goes to these people ([emoji key][emojis]):
10391040
<td align="center"><a href="https://blog.revathskumar.com"><img src="https://avatars3.githubusercontent.com/u/463904?v=4" width="100px;" alt="Revath S Kumar"/><br /><sub><b>Revath S Kumar</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=revathskumar" title="Code">💻</a></td>
10401041
<td align="center"><a href="https://raccoon.studio"><img src="https://avatars0.githubusercontent.com/u/4989733?v=4" width="100px;" alt="hiwelo."/><br /><sub><b>hiwelo.</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=hiwelo" title="Code">💻</a> <a href="#ideas-hiwelo" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/testing-library/jest-dom/commits?author=hiwelo" title="Tests">⚠️</a></td>
10411042
<td align="center"><a href="https://github.com/lukaszfiszer"><img src="https://avatars3.githubusercontent.com/u/1201711?v=4" width="100px;" alt="Łukasz Fiszer"/><br /><sub><b>Łukasz Fiszer</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=lukaszfiszer" title="Code">💻</a></td>
1043+
<td align="center"><a href="https://github.com/jeanchung"><img src="https://avatars0.githubusercontent.com/u/10778036?v=4" width="100px;" alt="Jean Chung"/><br /><sub><b>Jean Chung</b></sub></a><br /><a href="https://github.com/testing-library/jest-dom/commits?author=jeanchung" title="Code">💻</a> <a href="https://github.com/testing-library/jest-dom/commits?author=jeanchung" title="Tests">⚠️</a></td>
10421044
</tr>
10431045
</table>
10441046
1047+
<!-- markdownlint-enable -->
1048+
<!-- prettier-ignore-end -->
1049+
10451050
<!-- ALL-CONTRIBUTORS-LIST:END -->
10461051
10471052
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/to-have-form-values.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ describe('.toHaveFormValues', () => {
229229
expect(form).toHaveFormValues(expectedValues)
230230
}).not.toThrowError()
231231
})
232+
233+
it('matches change in selected value of select', () => {
234+
const oldValue = ''
235+
const newValue = 'design'
236+
237+
const {container} = render(`
238+
<form>
239+
${renderSelectSingle('category', 'Category', categories, oldValue)}
240+
</form>
241+
`)
242+
243+
const form = container.querySelector('form')
244+
const select = container.querySelector('select')
245+
expect(form).toHaveFormValues({category: oldValue})
246+
247+
select.value = newValue
248+
expect(form).toHaveFormValues({category: newValue})
249+
})
232250
})
233251

234252
describe('failed assertions', () => {
@@ -331,3 +349,5 @@ function renderSelectMultiple(name, label, options, value = []) {
331349
function renderList(items, mapper) {
332350
return items.map(mapper).join('')
333351
}
352+
353+
/* eslint max-lines-per-function:0 */

src/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ function getTag(element) {
138138
return element.tagName && element.tagName.toLowerCase()
139139
}
140140

141-
function getSelectValue({multiple, selectedOptions}) {
141+
function getSelectValue({multiple, options}) {
142+
const selectedOptions = [...options].filter(option => option.selected)
143+
142144
if (multiple) {
143145
return [...selectedOptions].map(opt => opt.value)
144146
}

0 commit comments

Comments
 (0)