|
| 1 | +import * as React from 'react'; |
| 2 | +import { TextInput, View } from 'react-native'; |
| 3 | +import { render, screen } from '../..'; |
| 4 | +import '../extend-expect'; |
| 5 | + |
| 6 | +test('example test', () => { |
| 7 | + render(<TextInput testID="text-input" value="test" />); |
| 8 | + |
| 9 | + const textInput = screen.getByTestId('text-input'); |
| 10 | + expect(textInput).toHaveDisplayValue('test'); |
| 11 | +}); |
| 12 | + |
| 13 | +test('toHaveDisplayValue() on matching display value', () => { |
| 14 | + render(<TextInput testID="text-input" value="test" />); |
| 15 | + |
| 16 | + const textInput = screen.getByTestId('text-input'); |
| 17 | + expect(textInput).toHaveDisplayValue('test'); |
| 18 | + |
| 19 | + expect(() => expect(textInput).not.toHaveDisplayValue('test')) |
| 20 | + .toThrowErrorMatchingInlineSnapshot(` |
| 21 | + "expect(element).not.toHaveDisplayValue() |
| 22 | +
|
| 23 | + Expected element not to have display value: |
| 24 | + test |
| 25 | + Received: |
| 26 | + test" |
| 27 | + `); |
| 28 | +}); |
| 29 | + |
| 30 | +test('toHaveDisplayValue() on non-matching display value', () => { |
| 31 | + render(<TextInput testID="text-input" value="test" />); |
| 32 | + |
| 33 | + const textInput = screen.getByTestId('text-input'); |
| 34 | + expect(textInput).not.toHaveDisplayValue('non-test'); |
| 35 | + |
| 36 | + expect(() => expect(textInput).toHaveDisplayValue('non-test')) |
| 37 | + .toThrowErrorMatchingInlineSnapshot(` |
| 38 | + "expect(element).toHaveDisplayValue() |
| 39 | +
|
| 40 | + Expected element to have display value: |
| 41 | + non-test |
| 42 | + Received: |
| 43 | + test" |
| 44 | + `); |
| 45 | +}); |
| 46 | + |
| 47 | +test("toHaveDisplayValue() on non-'TextInput' elements", () => { |
| 48 | + render(<View testID="view" />); |
| 49 | + |
| 50 | + const view = screen.getByTestId('view'); |
| 51 | + expect(() => |
| 52 | + expect(view).toHaveDisplayValue('test') |
| 53 | + ).toThrowErrorMatchingInlineSnapshot( |
| 54 | + `"toHaveDisplayValue() works only with host "TextInput" elements. Passed element has type "View"."` |
| 55 | + ); |
| 56 | +}); |
| 57 | + |
| 58 | +test('toHaveDisplayValue() performing partial match', () => { |
| 59 | + render(<TextInput testID="text-input" value="Hello World" />); |
| 60 | + |
| 61 | + const textInput = screen.getByTestId('text-input'); |
| 62 | + expect(textInput).toHaveDisplayValue('Hello World'); |
| 63 | + |
| 64 | + expect(textInput).not.toHaveDisplayValue('hello world'); |
| 65 | + expect(textInput).not.toHaveDisplayValue('Hello'); |
| 66 | + expect(textInput).not.toHaveDisplayValue('World'); |
| 67 | + |
| 68 | + expect(textInput).toHaveDisplayValue('Hello World', { exact: false }); |
| 69 | + expect(textInput).toHaveDisplayValue('hello', { exact: false }); |
| 70 | + expect(textInput).toHaveDisplayValue('world', { exact: false }); |
| 71 | +}); |
| 72 | + |
| 73 | +test('toHaveDisplayValue() uses defaultValue', () => { |
| 74 | + render(<TextInput testID="text-input" defaultValue="default" />); |
| 75 | + |
| 76 | + const textInput = screen.getByTestId('text-input'); |
| 77 | + expect(textInput).toHaveDisplayValue('default'); |
| 78 | +}); |
| 79 | + |
| 80 | +test('toHaveDisplayValue() prioritizes value over defaultValue', () => { |
| 81 | + render( |
| 82 | + <TextInput testID="text-input" value="value" defaultValue="default" /> |
| 83 | + ); |
| 84 | + |
| 85 | + const textInput = screen.getByTestId('text-input'); |
| 86 | + expect(textInput).toHaveDisplayValue('value'); |
| 87 | +}); |
0 commit comments