|
| 1 | +import React from 'react'; |
| 2 | +import { View } from 'react-native'; |
| 3 | +import { render, screen } from '../..'; |
| 4 | +import '../extend-expect'; |
| 5 | + |
| 6 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 7 | +function DoNotRenderChildren({ children }: { children: React.ReactNode }) { |
| 8 | + // Intentionally do not render children. |
| 9 | + return null; |
| 10 | +} |
| 11 | + |
| 12 | +test('toBeEmptyElement()', () => { |
| 13 | + render( |
| 14 | + <View testID="not-empty"> |
| 15 | + <View testID="empty" /> |
| 16 | + </View> |
| 17 | + ); |
| 18 | + |
| 19 | + const empty = screen.getByTestId('empty'); |
| 20 | + expect(empty).toBeEmptyElement(); |
| 21 | + expect(() => expect(empty).not.toBeEmptyElement()) |
| 22 | + .toThrowErrorMatchingInlineSnapshot(` |
| 23 | + "expect(element).not.toBeEmptyElement() |
| 24 | +
|
| 25 | + Received: |
| 26 | + (no elements)" |
| 27 | + `); |
| 28 | + |
| 29 | + const notEmpty = screen.getByTestId('not-empty'); |
| 30 | + expect(notEmpty).not.toBeEmptyElement(); |
| 31 | + expect(() => expect(notEmpty).toBeEmptyElement()) |
| 32 | + .toThrowErrorMatchingInlineSnapshot(` |
| 33 | + "expect(element).toBeEmptyElement() |
| 34 | +
|
| 35 | + Received: |
| 36 | + <View |
| 37 | + testID="empty" |
| 38 | + />" |
| 39 | + `); |
| 40 | +}); |
| 41 | + |
| 42 | +test('toBeEmptyElement() ignores composite-only children', () => { |
| 43 | + render( |
| 44 | + <View testID="view"> |
| 45 | + <DoNotRenderChildren> |
| 46 | + <View testID="not-rendered" /> |
| 47 | + </DoNotRenderChildren> |
| 48 | + </View> |
| 49 | + ); |
| 50 | + |
| 51 | + const view = screen.getByTestId('view'); |
| 52 | + expect(view).toBeEmptyElement(); |
| 53 | + expect(() => expect(view).not.toBeEmptyElement()) |
| 54 | + .toThrowErrorMatchingInlineSnapshot(` |
| 55 | + "expect(element).not.toBeEmptyElement() |
| 56 | +
|
| 57 | + Received: |
| 58 | + (no elements)" |
| 59 | + `); |
| 60 | +}); |
| 61 | + |
| 62 | +test('toBeEmptyElement() on null element', () => { |
| 63 | + expect(() => { |
| 64 | + expect(null).toBeEmptyElement(); |
| 65 | + }).toThrowErrorMatchingInlineSnapshot(` |
| 66 | + "expect(received).toBeEmptyElement() |
| 67 | +
|
| 68 | + received value must be a host element. |
| 69 | + Received has value: null" |
| 70 | + `); |
| 71 | +}); |
0 commit comments