Skip to content

Commit

Permalink
refactor: expose legacy extend-expect (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski authored Aug 22, 2023
1 parent d372016 commit 872163b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
30 changes: 30 additions & 0 deletions legacy-extend-expect.d.ts
Original file line number Diff line number Diff line change
@@ -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<R> {
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<ViewStyle | TextStyle | ImageStyle>): 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<R, T = {}> extends JestNativeMatchers<R> {}
}
}

// Explicit `@jest/globals` `expect` matchers.
declare module '@jest/expect' {
interface Matchers<R extends void | Promise<void>> extends JestNativeMatchers<R> {}
}
1 change: 1 addition & 0 deletions legacy-extend-expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./dist/legacy-extend-expect');
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/legacy-extend-expect.tsx
Original file line number Diff line number Diff line change
@@ -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(
<View testID="view">
<Text>Hello</Text>
</View>,
);
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();
});
24 changes: 24 additions & 0 deletions src/legacy-extend-expect.ts
Original file line number Diff line number Diff line change
@@ -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,
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

0 comments on commit 872163b

Please sign in to comment.