From fe9e6f1c45da4fc7130e24acf3084d80918c833c Mon Sep 17 00:00:00 2001 From: siepra Date: Mon, 11 Sep 2023 16:31:04 +0200 Subject: [PATCH 01/12] feat: add toContainElement matcher --- .../__tests__/to-contain-element.test.tsx | 38 +++++++++++++++++ src/matchers/extend-expect.d.ts | 2 + src/matchers/extend-expect.ts | 2 + src/matchers/index.tsx | 1 + src/matchers/to-contain-element.tsx | 41 +++++++++++++++++++ src/matchers/utils.tsx | 27 ++++++++++++ 6 files changed, 111 insertions(+) create mode 100644 src/matchers/__tests__/to-contain-element.test.tsx create mode 100644 src/matchers/to-contain-element.tsx diff --git a/src/matchers/__tests__/to-contain-element.test.tsx b/src/matchers/__tests__/to-contain-element.test.tsx new file mode 100644 index 000000000..f446740ed --- /dev/null +++ b/src/matchers/__tests__/to-contain-element.test.tsx @@ -0,0 +1,38 @@ +import * as React from 'react'; +import { View } from 'react-native'; +import { render, screen } from '../..'; +import '../extend-expect'; + +test('toContainElement() on parent view', () => { + render( + + + + ); + + const parent = screen.getByTestId('parent'); + const child = screen.getByTestId('child'); + + expect(parent).toContainElement(child); + + expect(() => expect(parent).not.toContainElement(child)) + .toThrowErrorMatchingInlineSnapshot(` + "expect(element).not.toContainElement(element) + + + } + testID="parent" + /> + + contains: + + + " + `); +}); diff --git a/src/matchers/extend-expect.d.ts b/src/matchers/extend-expect.d.ts index 54e02213c..875a4fb50 100644 --- a/src/matchers/extend-expect.d.ts +++ b/src/matchers/extend-expect.d.ts @@ -1,4 +1,5 @@ import type { StyleProp } from 'react-native'; +import { ReactTestInstance } from 'react-test-renderer'; import type { TextMatch, TextMatchOptions } from '../matches'; import type { Style } from './to-have-style'; @@ -12,6 +13,7 @@ export interface JestNativeMatchers { toBePartiallyChecked(): R; toBeSelected(): R; toBeVisible(): R; + toContainElement(element: ReactTestInstance | null): R; toHaveDisplayValue(expectedValue: TextMatch, options?: TextMatchOptions): R; toHaveProp(name: string, expectedValue?: unknown): R; toHaveStyle(style: StyleProp