-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
toHaveDisplayValue
matcher (#1463)
* feat: to have display value * add tests for toHaveDisplayValue() * update type of toHaveDisplayValue matcher * rename test file * format * chore: add more tests * refactor: extract common TextInput utils * chore: fix codecov --------- Co-authored-by: Jan Jaworski <[email protected]>
- Loading branch information
1 parent
a7f250d
commit 344e9b3
Showing
13 changed files
with
207 additions
and
24 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,22 @@ | ||
import * as React from 'react'; | ||
import { View } from 'react-native'; | ||
import { render, screen } from '../..'; | ||
import { getTextInputValue, isTextInputEditable } from '../text-input'; | ||
|
||
test('getTextInputValue() throws error when invoked on non-text input', () => { | ||
render(<View testID="view" />); | ||
|
||
const view = screen.getByTestId('view'); | ||
expect(() => getTextInputValue(view)).toThrowErrorMatchingInlineSnapshot( | ||
`"Element is not a "TextInput", but it has type "View"."` | ||
); | ||
}); | ||
|
||
test('isTextInputEditable() throws error when invoked on non-text input', () => { | ||
render(<View testID="view" />); | ||
|
||
const view = screen.getByTestId('view'); | ||
expect(() => isTextInputEditable(view)).toThrowErrorMatchingInlineSnapshot( | ||
`"Element is not a "TextInput", but it has type "View"."` | ||
); | ||
}); |
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,22 @@ | ||
import { ReactTestInstance } from 'react-test-renderer'; | ||
import { isHostTextInput } from './host-component-names'; | ||
|
||
export function isTextInputEditable(element: ReactTestInstance) { | ||
if (!isHostTextInput(element)) { | ||
throw new Error( | ||
`Element is not a "TextInput", but it has type "${element.type}".` | ||
); | ||
} | ||
|
||
return element.props.editable !== false; | ||
} | ||
|
||
export function getTextInputValue(element: ReactTestInstance) { | ||
if (!isHostTextInput(element)) { | ||
throw new Error( | ||
`Element is not a "TextInput", but it has type "${element.type}".` | ||
); | ||
} | ||
|
||
return element.props.value ?? element.props.defaultValue; | ||
} |
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,87 @@ | ||
import * as React from 'react'; | ||
import { TextInput, View } from 'react-native'; | ||
import { render, screen } from '../..'; | ||
import '../extend-expect'; | ||
|
||
test('example test', () => { | ||
render(<TextInput testID="text-input" value="test" />); | ||
|
||
const textInput = screen.getByTestId('text-input'); | ||
expect(textInput).toHaveDisplayValue('test'); | ||
}); | ||
|
||
test('toHaveDisplayValue() on matching display value', () => { | ||
render(<TextInput testID="text-input" value="test" />); | ||
|
||
const textInput = screen.getByTestId('text-input'); | ||
expect(textInput).toHaveDisplayValue('test'); | ||
|
||
expect(() => expect(textInput).not.toHaveDisplayValue('test')) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toHaveDisplayValue() | ||
Expected element not to have display value: | ||
test | ||
Received: | ||
test" | ||
`); | ||
}); | ||
|
||
test('toHaveDisplayValue() on non-matching display value', () => { | ||
render(<TextInput testID="text-input" value="test" />); | ||
|
||
const textInput = screen.getByTestId('text-input'); | ||
expect(textInput).not.toHaveDisplayValue('non-test'); | ||
|
||
expect(() => expect(textInput).toHaveDisplayValue('non-test')) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toHaveDisplayValue() | ||
Expected element to have display value: | ||
non-test | ||
Received: | ||
test" | ||
`); | ||
}); | ||
|
||
test("toHaveDisplayValue() on non-'TextInput' elements", () => { | ||
render(<View testID="view" />); | ||
|
||
const view = screen.getByTestId('view'); | ||
expect(() => | ||
expect(view).toHaveDisplayValue('test') | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"toHaveDisplayValue() works only with host "TextInput" elements. Passed element has type "View"."` | ||
); | ||
}); | ||
|
||
test('toHaveDisplayValue() performing partial match', () => { | ||
render(<TextInput testID="text-input" value="Hello World" />); | ||
|
||
const textInput = screen.getByTestId('text-input'); | ||
expect(textInput).toHaveDisplayValue('Hello World'); | ||
|
||
expect(textInput).not.toHaveDisplayValue('hello world'); | ||
expect(textInput).not.toHaveDisplayValue('Hello'); | ||
expect(textInput).not.toHaveDisplayValue('World'); | ||
|
||
expect(textInput).toHaveDisplayValue('Hello World', { exact: false }); | ||
expect(textInput).toHaveDisplayValue('hello', { exact: false }); | ||
expect(textInput).toHaveDisplayValue('world', { exact: false }); | ||
}); | ||
|
||
test('toHaveDisplayValue() uses defaultValue', () => { | ||
render(<TextInput testID="text-input" defaultValue="default" />); | ||
|
||
const textInput = screen.getByTestId('text-input'); | ||
expect(textInput).toHaveDisplayValue('default'); | ||
}); | ||
|
||
test('toHaveDisplayValue() prioritizes value over defaultValue', () => { | ||
render( | ||
<TextInput testID="text-input" value="value" defaultValue="default" /> | ||
); | ||
|
||
const textInput = screen.getByTestId('text-input'); | ||
expect(textInput).toHaveDisplayValue('value'); | ||
}); |
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,49 @@ | ||
import type { ReactTestInstance } from 'react-test-renderer'; | ||
import { matcherHint } from 'jest-matcher-utils'; | ||
import { isHostTextInput } from '../helpers/host-component-names'; | ||
import { ErrorWithStack } from '../helpers/errors'; | ||
import { getTextInputValue } from '../helpers/text-input'; | ||
import { TextMatch, TextMatchOptions, matches } from '../matches'; | ||
import { checkHostElement, formatMessage } from './utils'; | ||
|
||
export function toHaveDisplayValue( | ||
this: jest.MatcherContext, | ||
element: ReactTestInstance, | ||
expectedValue: TextMatch, | ||
options?: TextMatchOptions | ||
) { | ||
checkHostElement(element, toHaveDisplayValue, this); | ||
|
||
if (!isHostTextInput(element)) { | ||
throw new ErrorWithStack( | ||
`toHaveDisplayValue() works only with host "TextInput" elements. Passed element has type "${element.type}".`, | ||
toHaveDisplayValue | ||
); | ||
} | ||
|
||
const receivedValue = getTextInputValue(element); | ||
|
||
return { | ||
pass: matches( | ||
expectedValue, | ||
receivedValue, | ||
options?.normalizer, | ||
options?.exact | ||
), | ||
message: () => { | ||
return [ | ||
formatMessage( | ||
matcherHint( | ||
`${this.isNot ? '.not' : ''}.toHaveDisplayValue`, | ||
'element', | ||
'' | ||
), | ||
`Expected element ${this.isNot ? 'not to' : 'to'} have display value`, | ||
expectedValue, | ||
'Received', | ||
receivedValue | ||
), | ||
].join('\n'); | ||
}, | ||
}; | ||
} |
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
export * from './content-size'; | ||
export * from './dispatch-event'; | ||
export * from './host-components'; | ||
export * from './text-range'; | ||
export * from './wait'; | ||
export * from './warn-about-real-timers'; |