Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix(getBy*): throw an error if more than one element is found (#229)
Closes #202 BREAKING CHANGE: All `getBy` and `findBy` query variants now will throw an error if more than one element is returned. If this is expected, then use `getAllBy` (or `findAllBy`) instead.
- Loading branch information
Kent C. Dodds
authored and
Kent C. Dodds
committed
Apr 25, 2019
1 parent
253d677
commit 7f8c100
Showing
16 changed files
with
567 additions
and
407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import cases from 'jest-in-case' | ||
import {render} from './helpers/test-utils' | ||
|
||
cases( | ||
'getBy* queries throw an error when there are multiple elements returned', | ||
({name, query, html}) => { | ||
const utils = render(html) | ||
expect(() => utils[name](query)).toThrow(/multiple elements/i) | ||
}, | ||
{ | ||
getByLabelText: { | ||
query: /his/, | ||
html: `<div aria-label="his"></div><div aria-label="history"></div>`, | ||
}, | ||
getByPlaceholderText: { | ||
query: /his/, | ||
html: `<input placeholder="his" /><input placeholder="history" />`, | ||
}, | ||
getByText: { | ||
query: /his/, | ||
html: `<div>his</div><div>history</div>`, | ||
}, | ||
getByAltText: { | ||
query: /his/, | ||
html: `<img alt="his" src="his.png" /><img alt="history" src="history.png" />`, | ||
}, | ||
getByTitle: { | ||
query: /his/, | ||
html: `<div title="his"></div><div title="history"></div>`, | ||
}, | ||
getByDisplayValue: { | ||
query: /his/, | ||
html: `<input value="his" /><select><option value="history">history</option></select>`, | ||
}, | ||
getByRole: { | ||
query: /his/, | ||
html: `<div role="his"></div><div role="history"></div>`, | ||
}, | ||
getByTestId: { | ||
query: /his/, | ||
html: `<div data-testid="his"></div><div data-testid="history"></div>`, | ||
}, | ||
}, | ||
) | ||
|
||
cases( | ||
'queryBy* queries throw an error when there are multiple elements returned', | ||
({name, query, html}) => { | ||
const utils = render(html) | ||
expect(() => utils[name](query)).toThrow(/multiple elements/i) | ||
}, | ||
{ | ||
queryByLabelText: { | ||
query: /his/, | ||
html: `<div aria-label="his"></div><div aria-label="history"></div>`, | ||
}, | ||
queryByPlaceholderText: { | ||
query: /his/, | ||
html: `<input placeholder="his" /><input placeholder="history" />`, | ||
}, | ||
queryByText: { | ||
query: /his/, | ||
html: `<div>his</div><div>history</div>`, | ||
}, | ||
queryByAltText: { | ||
query: /his/, | ||
html: `<img alt="his" src="his.png" /><img alt="history" src="history.png" />`, | ||
}, | ||
queryByTitle: { | ||
query: /his/, | ||
html: `<div title="his"></div><div title="history"></div>`, | ||
}, | ||
queryByDisplayValue: { | ||
query: /his/, | ||
html: `<input value="his" /><select><option value="history">history</option></select>`, | ||
}, | ||
queryByRole: { | ||
query: /his/, | ||
html: `<div role="his"></div><div role="history"></div>`, | ||
}, | ||
queryByTestId: { | ||
query: /his/, | ||
html: `<div data-testid="his"></div><div data-testid="history"></div>`, | ||
}, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {render} from './helpers/test-utils' | ||
import {queryByAttribute} from '..' | ||
|
||
// we used to use queryByAttribute internally, but we don't anymore. Some people | ||
// use it as an undocumented part of the API, so we'll keep it around. | ||
test('queryByAttribute', () => { | ||
const {container} = render( | ||
'<div data-foo="bar"></div><div data-foo="rubar"></div>', | ||
) | ||
expect(queryByAttribute('data-foo', container, 'bar')).not.toBeNull() | ||
expect(queryByAttribute('blah', container, 'sup')).toBeNull() | ||
expect(() => queryByAttribute('data-foo', container, /bar/)).toThrow( | ||
/multiple/, | ||
) | ||
}) |
Oops, something went wrong.