|
| 1 | +import * as React from 'react'; |
| 2 | +import { View } from 'react-native'; |
| 3 | +import { render, screen } from '../..'; |
| 4 | +import '../extend-expect'; |
| 5 | + |
| 6 | +test('toContainElement() supports basic case', () => { |
| 7 | + render( |
| 8 | + <View testID="parent"> |
| 9 | + <View testID="child" /> |
| 10 | + </View> |
| 11 | + ); |
| 12 | + |
| 13 | + const parent = screen.getByTestId('parent'); |
| 14 | + const child = screen.getByTestId('child'); |
| 15 | + |
| 16 | + expect(parent).toContainElement(child); |
| 17 | + |
| 18 | + expect(() => expect(parent).not.toContainElement(child)) |
| 19 | + .toThrowErrorMatchingInlineSnapshot(` |
| 20 | + "expect(container).not.toContainElement(element) |
| 21 | +
|
| 22 | + <View |
| 23 | + testID="parent" |
| 24 | + /> |
| 25 | +
|
| 26 | + contains: |
| 27 | +
|
| 28 | + <View |
| 29 | + testID="child" |
| 30 | + /> |
| 31 | + " |
| 32 | + `); |
| 33 | +}); |
| 34 | + |
| 35 | +test('toContainElement() supports negative case', () => { |
| 36 | + render( |
| 37 | + <> |
| 38 | + <View testID="view1" /> |
| 39 | + <View testID="view2" /> |
| 40 | + </> |
| 41 | + ); |
| 42 | + |
| 43 | + const view1 = screen.getByTestId('view1'); |
| 44 | + const view2 = screen.getByTestId('view2'); |
| 45 | + |
| 46 | + expect(view1).not.toContainElement(view2); |
| 47 | + expect(view2).not.toContainElement(view1); |
| 48 | + |
| 49 | + expect(() => expect(view1).toContainElement(view2)) |
| 50 | + .toThrowErrorMatchingInlineSnapshot(` |
| 51 | + "expect(container).toContainElement(element) |
| 52 | +
|
| 53 | + <View |
| 54 | + testID="view1" |
| 55 | + /> |
| 56 | +
|
| 57 | + does not contain: |
| 58 | +
|
| 59 | + <View |
| 60 | + testID="view2" |
| 61 | + /> |
| 62 | + " |
| 63 | + `); |
| 64 | +}); |
| 65 | + |
| 66 | +test('toContainElement() handles null container', () => { |
| 67 | + render(<View testID="view" />); |
| 68 | + |
| 69 | + const view = screen.getByTestId('view'); |
| 70 | + |
| 71 | + expect(() => expect(null).toContainElement(view)) |
| 72 | + .toThrowErrorMatchingInlineSnapshot(` |
| 73 | + "expect(received).toContainElement() |
| 74 | +
|
| 75 | + received value must be a host element. |
| 76 | + Received has value: null" |
| 77 | + `); |
| 78 | +}); |
| 79 | + |
| 80 | +test('toContainElement() handles null element', () => { |
| 81 | + render(<View testID="view" />); |
| 82 | + |
| 83 | + const view = screen.getByTestId('view'); |
| 84 | + |
| 85 | + expect(view).not.toContainElement(null); |
| 86 | + |
| 87 | + expect(() => expect(view).toContainElement(null)) |
| 88 | + .toThrowErrorMatchingInlineSnapshot(` |
| 89 | + "expect(container).toContainElement(element) |
| 90 | +
|
| 91 | + <View |
| 92 | + testID="view" |
| 93 | + /> |
| 94 | +
|
| 95 | + does not contain: |
| 96 | +
|
| 97 | + null |
| 98 | + " |
| 99 | + `); |
| 100 | +}); |
| 101 | + |
| 102 | +test('toContainElement() handles non-element container', () => { |
| 103 | + render(<View testID="view" />); |
| 104 | + |
| 105 | + const view = screen.getByTestId('view'); |
| 106 | + |
| 107 | + expect(() => expect({ name: 'non-element' }).not.toContainElement(view)) |
| 108 | + .toThrowErrorMatchingInlineSnapshot(` |
| 109 | + "expect(received).not.toContainElement() |
| 110 | +
|
| 111 | + received value must be a host element. |
| 112 | + Received has type: object |
| 113 | + Received has value: {"name": "non-element"}" |
| 114 | + `); |
| 115 | + |
| 116 | + expect(() => expect(true).not.toContainElement(view)) |
| 117 | + .toThrowErrorMatchingInlineSnapshot(` |
| 118 | + "expect(received).not.toContainElement() |
| 119 | +
|
| 120 | + received value must be a host element. |
| 121 | + Received has type: boolean |
| 122 | + Received has value: true" |
| 123 | + `); |
| 124 | +}); |
| 125 | + |
| 126 | +test('toContainElement() handles non-element element', () => { |
| 127 | + render(<View testID="view" />); |
| 128 | + |
| 129 | + const view = screen.getByTestId('view'); |
| 130 | + |
| 131 | + expect(() => |
| 132 | + // @ts-expect-error |
| 133 | + expect(view).not.toContainElement({ name: 'non-element' }) |
| 134 | + ).toThrowErrorMatchingInlineSnapshot(` |
| 135 | + "expect(received).not.toContainElement() |
| 136 | +
|
| 137 | + received value must be a host element. |
| 138 | + Received has type: object |
| 139 | + Received has value: {"name": "non-element"}" |
| 140 | + `); |
| 141 | +}); |
0 commit comments