From 872163bf0234e272c9e9af3809d92d804e81e7b9 Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Tue, 22 Aug 2023 16:41:23 +0200 Subject: [PATCH] refactor: expose legacy extend-expect (#189) --- legacy-extend-expect.d.ts | 30 ++++++++++++++++++++++++++ legacy-extend-expect.js | 1 + package.json | 4 +++- src/__tests__/legacy-extend-expect.tsx | 17 +++++++++++++++ src/legacy-extend-expect.ts | 24 +++++++++++++++++++++ tsconfig.json | 2 +- 6 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 legacy-extend-expect.d.ts create mode 100644 legacy-extend-expect.js create mode 100644 src/__tests__/legacy-extend-expect.tsx create mode 100644 src/legacy-extend-expect.ts diff --git a/legacy-extend-expect.d.ts b/legacy-extend-expect.d.ts new file mode 100644 index 0000000..5519490 --- /dev/null +++ b/legacy-extend-expect.d.ts @@ -0,0 +1,30 @@ +import type { AccessibilityState, ImageStyle, StyleProp, TextStyle, ViewStyle } from 'react-native'; +import type { ReactTestInstance } from 'react-test-renderer'; +import type { AccessibilityValueMatcher } from './src/to-have-accessibility-value'; + +export interface JestNativeMatchers { + legacy_toBeDisabled(): R; + legacy_toBeEmptyElement(): R; + legacy_toBeEnabled(): R; + legacy_toBeOnTheScreen(): R; + legacy_toBeVisible(): R; + legacy_toContainElement(element: ReactTestInstance | null): R; + legacy_toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R; + legacy_toHaveProp(attr: string, value?: unknown): R; + legacy_toHaveStyle(style: StyleProp): R; + legacy_toHaveAccessibilityState(state: AccessibilityState): R; + legacy_toHaveAccessibilityValue(value: AccessibilityValueMatcher): R; +} + +// Implicit Jest global `expect`. +declare global { + namespace jest { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + interface Matchers extends JestNativeMatchers {} + } +} + +// Explicit `@jest/globals` `expect` matchers. +declare module '@jest/expect' { + interface Matchers> extends JestNativeMatchers {} +} diff --git a/legacy-extend-expect.js b/legacy-extend-expect.js new file mode 100644 index 0000000..d41102f --- /dev/null +++ b/legacy-extend-expect.js @@ -0,0 +1 @@ +require('./dist/legacy-extend-expect'); diff --git a/package.json b/package.json index 5c01c51..93294d6 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,9 @@ "files": [ "dist", "extend-expect.js", - "extend-expect.d.ts" + "extend-expect.d.ts", + "legacy-extend-expect.js", + "legacy-extend-expect.d.ts" ], "keywords": [ "testing", diff --git a/src/__tests__/legacy-extend-expect.tsx b/src/__tests__/legacy-extend-expect.tsx new file mode 100644 index 0000000..38fa3b2 --- /dev/null +++ b/src/__tests__/legacy-extend-expect.tsx @@ -0,0 +1,17 @@ +import React from 'react'; +import { View, Text } from 'react-native'; +import { render, screen } from '@testing-library/react-native'; +import '../legacy-extend-expect'; + +test('legacy expect.extend() works correctly', () => { + render( + + Hello + , + ); + expect(screen.getByTestId('view')).legacy_toBeOnTheScreen(); + expect(screen.getByTestId('view')).legacy_toHaveTextContent('Hello'); + expect(screen.getByTestId('view')).legacy_toBeVisible(); + expect(screen.getByTestId('view')).legacy_toBeEnabled(); + expect(screen.getByTestId('view')).not.legacy_toBeDisabled(); +}); diff --git a/src/legacy-extend-expect.ts b/src/legacy-extend-expect.ts new file mode 100644 index 0000000..aa565cd --- /dev/null +++ b/src/legacy-extend-expect.ts @@ -0,0 +1,24 @@ +import { toBeDisabled, toBeEnabled } from './to-be-disabled'; +import { toBeEmptyElement } from './to-be-empty-element'; +import { toBeOnTheScreen } from './to-be-on-the-screen'; +import { toBeVisible } from './to-be-visible'; +import { toContainElement } from './to-contain-element'; +import { toHaveAccessibilityState } from './to-have-accessibility-state'; +import { toHaveAccessibilityValue } from './to-have-accessibility-value'; +import { toHaveProp } from './to-have-prop'; +import { toHaveStyle } from './to-have-style'; +import { toHaveTextContent } from './to-have-text-content'; + +expect.extend({ + legacy_toBeDisabled: toBeDisabled, + legacy_toBeEnabled: toBeEnabled, + legacy_toBeEmptyElement: toBeEmptyElement, + legacy_toBeOnTheScreen: toBeOnTheScreen, + legacy_toBeVisible: toBeVisible, + legacy_toContainElement: toContainElement, + legacy_toHaveAccessibilityState: toHaveAccessibilityState, + legacy_toHaveAccessibilityValue: toHaveAccessibilityValue, + legacy_toHaveProp: toHaveProp, + legacy_toHaveStyle: toHaveStyle, + legacy_toHaveTextContent: toHaveTextContent, +}); diff --git a/tsconfig.json b/tsconfig.json index 345818f..78157bd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,7 @@ "noEmit": false, "outDir": "dist" }, - "files": ["./extend-expect.d.ts"], + "files": ["./extend-expect.d.ts", "./legacy-extend-expect.d.ts"], "exclude": ["node_modules", "android", "ios"], "include": ["src/**/*", "setup-test.ts"] }