-
Notifications
You must be signed in to change notification settings - Fork 380
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
- Loading branch information
1 parent
28f960f
commit fd747bf
Showing
8 changed files
with
274 additions
and
48 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
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,106 @@ | ||
import {render} from './helpers/test-utils' | ||
|
||
describe('.toHaveValue', () => { | ||
test('handles value of text input', () => { | ||
const {queryByTestId} = render(` | ||
<input type="text" value="foo" data-testid="value" /> | ||
<input type="text" value="" data-testid="empty" /> | ||
<input type="text" data-testid="without" /> | ||
`) | ||
|
||
expect(queryByTestId('value')).toHaveValue('foo') | ||
expect(queryByTestId('value')).toHaveValue() | ||
expect(queryByTestId('value')).not.toHaveValue('bar') | ||
expect(queryByTestId('value')).not.toHaveValue('') | ||
|
||
expect(queryByTestId('empty')).toHaveValue('') | ||
expect(queryByTestId('empty')).not.toHaveValue() | ||
expect(queryByTestId('empty')).not.toHaveValue('foo') | ||
|
||
expect(queryByTestId('without')).toHaveValue('') | ||
expect(queryByTestId('without')).not.toHaveValue() | ||
expect(queryByTestId('without')).not.toHaveValue('foo') | ||
queryByTestId('without').value = 'bar' | ||
expect(queryByTestId('without')).toHaveValue('bar') | ||
}) | ||
|
||
test('handles value of number input', () => { | ||
const {queryByTestId} = render(` | ||
<input type="number" value="5" data-testid="number" /> | ||
<input type="number" value="" data-testid="empty" /> | ||
<input type="number" data-testid="without" /> | ||
`) | ||
|
||
expect(queryByTestId('number')).toHaveValue(5) | ||
expect(queryByTestId('number')).toHaveValue() | ||
expect(queryByTestId('number')).not.toHaveValue(4) | ||
expect(queryByTestId('number')).not.toHaveValue('5') | ||
|
||
expect(queryByTestId('empty')).toHaveValue(null) | ||
expect(queryByTestId('empty')).not.toHaveValue() | ||
expect(queryByTestId('empty')).not.toHaveValue('5') | ||
|
||
expect(queryByTestId('without')).toHaveValue(null) | ||
expect(queryByTestId('without')).not.toHaveValue() | ||
expect(queryByTestId('without')).not.toHaveValue('10') | ||
queryByTestId('without').value = 10 | ||
expect(queryByTestId('without')).toHaveValue(10) | ||
}) | ||
|
||
test('handles value of select element', () => { | ||
const {queryByTestId} = render(` | ||
<select data-testid="single"> | ||
<option value="first">First Value</option> | ||
<option value="second" selected>Second Value</option> | ||
<option value="third">Third Value</option> | ||
</select> | ||
<select data-testid="multiple" multiple> | ||
<option value="first">First Value</option> | ||
<option value="second" selected>Second Value</option> | ||
<option value="third" selected>Third Value</option> | ||
</select> | ||
<select data-testid="not-selected" > | ||
<option value="" disabled selected>- Select some value - </option> | ||
<option value="first">First Value</option> | ||
<option value="second">Second Value</option> | ||
<option value="third">Third Value</option> | ||
</select> | ||
`) | ||
|
||
expect(queryByTestId('single')).toHaveValue('second') | ||
expect(queryByTestId('single')).toHaveValue() | ||
|
||
expect(queryByTestId('multiple')).toHaveValue(['second', 'third']) | ||
expect(queryByTestId('multiple')).toHaveValue() | ||
|
||
expect(queryByTestId('not-selected')).not.toHaveValue() | ||
expect(queryByTestId('not-selected')).toHaveValue('') | ||
|
||
queryByTestId('single').children[0].setAttribute('selected', true) | ||
expect(queryByTestId('single')).toHaveValue('first') | ||
}) | ||
|
||
test('handles value of textarea element', () => { | ||
const {queryByTestId} = render(` | ||
<textarea data-testid="textarea">text value</textarea> | ||
`) | ||
expect(queryByTestId('textarea')).toHaveValue('text value') | ||
}) | ||
|
||
test('throws when passed checkbox or radio', () => { | ||
const {queryByTestId} = render(` | ||
<input data-testid="checkbox" type="checkbox" name="checkbox" value="val" checked /> | ||
<input data-testid="radio" type="radio" name="radio" value="val" checked /> | ||
`) | ||
|
||
expect(() => { | ||
expect(queryByTestId('checkbox')).toHaveValue('') | ||
}).toThrow() | ||
|
||
expect(() => { | ||
expect(queryByTestId('radio')).toHaveValue('') | ||
}).toThrow() | ||
}) | ||
}) |
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
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,44 @@ | ||
import {matcherHint} from 'jest-matcher-utils' | ||
import isEqualWith from 'lodash/isEqualWith' | ||
import { | ||
checkHtmlElement, | ||
compareArraysAsSet, | ||
getMessage, | ||
getSingleElementValue, | ||
} from './utils' | ||
|
||
export function toHaveValue(htmlElement, expectedValue) { | ||
checkHtmlElement(htmlElement, toHaveValue, this) | ||
|
||
if ( | ||
htmlElement.tagName.toLowerCase() === 'input' && | ||
['checkbox', 'radio'].includes(htmlElement.type) | ||
) { | ||
throw new Error( | ||
'input with type=checkbox or type=radio cannot be used with .toHaveValue(). Use .toHaveFormValues() instead', | ||
) | ||
} | ||
|
||
const receivedValue = getSingleElementValue(htmlElement) | ||
const expectsValue = expectedValue !== undefined | ||
return { | ||
pass: expectsValue | ||
? isEqualWith(receivedValue, expectedValue, compareArraysAsSet) | ||
: Boolean(receivedValue), | ||
message: () => { | ||
const to = this.isNot ? 'not to' : 'to' | ||
const matcher = matcherHint( | ||
`${this.isNot ? '.not' : ''}.toHaveValue`, | ||
'element', | ||
expectedValue, | ||
) | ||
return getMessage( | ||
matcher, | ||
`Expected the element ${to} have value`, | ||
expectsValue ? expectedValue : '(any)', | ||
'Received', | ||
receivedValue, | ||
) | ||
}, | ||
} | ||
} |
Oops, something went wrong.