|
| 1 | +import * as React from 'react'; |
| 2 | +import { Text, Pressable, View } from 'react-native'; |
| 3 | +import { render, within } from '../pure'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Our queries interact differently with composite and host elements, and some specific cases require us |
| 7 | + * to crawl up the tree to a Text composite element to be able to traverse it down again. Going up the tree |
| 8 | + * is a dangerous behaviour because we could take the risk of then traversing a sibling node to the original one. |
| 9 | + * This test suite is designed to be able to test as many different combinations, as a safety net. |
| 10 | + * Specific cases should still be tested within the relevant file (for instance an edge case with `within` should have |
| 11 | + * an explicit test in the within test suite) |
| 12 | + */ |
| 13 | +describe('nested text handling', () => { |
| 14 | + test('within same node', () => { |
| 15 | + const view = render(<Text testID="subject">Hello</Text>); |
| 16 | + expect(within(view.getByTestId('subject')).getByText('Hello')).toBeTruthy(); |
| 17 | + }); |
| 18 | + |
| 19 | + test('role with direct text children', () => { |
| 20 | + const view = render(<Text accessibilityRole="header">About</Text>); |
| 21 | + |
| 22 | + expect(view.getByRole('header', { name: 'About' })).toBeTruthy(); |
| 23 | + }); |
| 24 | + |
| 25 | + test('nested text with child with role', () => { |
| 26 | + const view = render( |
| 27 | + <Text> |
| 28 | + <Text testID="child" accessibilityRole="header"> |
| 29 | + About |
| 30 | + </Text> |
| 31 | + </Text> |
| 32 | + ); |
| 33 | + |
| 34 | + expect(view.getByRole('header', { name: 'About' }).props.testID).toBe( |
| 35 | + 'child' |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + test('pressable within View, with text child', () => { |
| 40 | + const view = render( |
| 41 | + <View> |
| 42 | + <Pressable testID="pressable" accessibilityRole="button"> |
| 43 | + <Text>Save</Text> |
| 44 | + </Pressable> |
| 45 | + </View> |
| 46 | + ); |
| 47 | + |
| 48 | + expect(view.getByRole('button', { name: 'Save' }).props.testID).toBe( |
| 49 | + 'pressable' |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + test('pressable within View, with text child within view', () => { |
| 54 | + const view = render( |
| 55 | + <View> |
| 56 | + <Pressable testID="pressable" accessibilityRole="button"> |
| 57 | + <View> |
| 58 | + <Text>Save</Text> |
| 59 | + </View> |
| 60 | + </Pressable> |
| 61 | + </View> |
| 62 | + ); |
| 63 | + |
| 64 | + expect(view.getByRole('button', { name: 'Save' }).props.testID).toBe( |
| 65 | + 'pressable' |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + test('Text within pressable', () => { |
| 70 | + const view = render( |
| 71 | + <Pressable testID="pressable" accessibilityRole="button"> |
| 72 | + <Text testID="text">Save</Text> |
| 73 | + </Pressable> |
| 74 | + ); |
| 75 | + |
| 76 | + expect(view.getByText('Save').props.testID).toBe('text'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('Text within view within pressable', () => { |
| 80 | + const view = render( |
| 81 | + <Pressable testID="pressable" accessibilityRole="button"> |
| 82 | + <View> |
| 83 | + <Text testID="text">Save</Text> |
| 84 | + </View> |
| 85 | + </Pressable> |
| 86 | + ); |
| 87 | + |
| 88 | + expect(view.getByText('Save').props.testID).toBe('text'); |
| 89 | + }); |
| 90 | +}); |
0 commit comments