|
| 1 | +/// <reference path="../extend-expect.d.ts" /> |
| 2 | + |
| 3 | +import * as React from 'react'; |
| 4 | +import { View, Text } from 'react-native'; |
| 5 | +import { render, screen } from '../..'; |
| 6 | +import '../extend-expect'; |
| 7 | + |
| 8 | +test('toHaveTextContent() example test', () => { |
| 9 | + render( |
| 10 | + <View testID="view"> |
| 11 | + <Text>Hello</Text> <Text>World</Text> |
| 12 | + </View> |
| 13 | + ); |
| 14 | + |
| 15 | + const view = screen.getByTestId('view'); |
| 16 | + expect(view).toHaveTextContent('Hello World'); |
| 17 | + expect(view).not.toHaveTextContent('Hello there'); |
| 18 | +}); |
| 19 | + |
| 20 | +test('toHaveTextContent() handles positive test cases', () => { |
| 21 | + render(<Text testID="text">Hello World</Text>); |
| 22 | + |
| 23 | + const text = screen.getByTestId('text'); |
| 24 | + expect(text).toHaveTextContent('Hello World'); |
| 25 | + expect(text).toHaveTextContent('Hello', { exact: false }); |
| 26 | + expect(text).toHaveTextContent(/Hello World/); |
| 27 | +}); |
| 28 | + |
| 29 | +test('toHaveTextContent() does exact match by default', () => { |
| 30 | + render(<Text testID="text">Hello World</Text>); |
| 31 | + |
| 32 | + const text = screen.getByTestId('text'); |
| 33 | + expect(text).toHaveTextContent('Hello World'); |
| 34 | + expect(text).not.toHaveTextContent('Hello'); |
| 35 | + expect(text).not.toHaveTextContent('World'); |
| 36 | +}); |
| 37 | + |
| 38 | +test('toHaveTextContent() handles nested text', () => { |
| 39 | + render( |
| 40 | + <Text testID="text"> |
| 41 | + Hello <Text>React</Text> |
| 42 | + </Text> |
| 43 | + ); |
| 44 | + |
| 45 | + const text = screen.getByTestId('text'); |
| 46 | + expect(text).toHaveTextContent('Hello React'); |
| 47 | +}); |
| 48 | + |
| 49 | +test('toHaveTextContent() negative test cases', () => { |
| 50 | + render(<Text testID="text">Hello World</Text>); |
| 51 | + |
| 52 | + const text = screen.getByTestId('text'); |
| 53 | + expect(text).not.toHaveTextContent(/Hello React/); |
| 54 | + expect(() => expect(text).toHaveTextContent(/Hello React/)) |
| 55 | + .toThrowErrorMatchingInlineSnapshot(` |
| 56 | + "expect(element).toHaveTextContent() |
| 57 | +
|
| 58 | + Expected element to have text content: |
| 59 | + /Hello React/ |
| 60 | + Received: |
| 61 | + Hello World" |
| 62 | + `); |
| 63 | + |
| 64 | + expect(text).not.toHaveTextContent('Yellow', { exact: false }); |
| 65 | + expect(() => expect(text).toHaveTextContent('Yellow', { exact: false })) |
| 66 | + .toThrowErrorMatchingInlineSnapshot(` |
| 67 | + "expect(element).toHaveTextContent() |
| 68 | +
|
| 69 | + Expected element to have text content: |
| 70 | + Yellow |
| 71 | + Received: |
| 72 | + Hello World" |
| 73 | + `); |
| 74 | +}); |
| 75 | + |
| 76 | +test('toHaveTextContent() on null element', () => { |
| 77 | + expect(() => expect(null).toHaveTextContent('Hello World')) |
| 78 | + .toThrowErrorMatchingInlineSnapshot(` |
| 79 | + "expect(received).toHaveTextContent() |
| 80 | +
|
| 81 | + received value must be a host element. |
| 82 | + Received has value: null" |
| 83 | + `); |
| 84 | +}); |
0 commit comments