Skip to content

Commit

Permalink
refactor: 0.76 stable tweaks (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski authored Oct 24, 2024
1 parent 0305877 commit a60ce28
Show file tree
Hide file tree
Showing 29 changed files with 204 additions and 102 deletions.
32 changes: 32 additions & 0 deletions examples/basic/components/AnimatedView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as React from 'react';
import { Animated, ViewStyle } from 'react-native';

type AnimatedViewProps = {
fadeInDuration?: number;
style?: ViewStyle;
children: React.ReactNode;
useNativeDriver?: boolean;
};

export function AnimatedView(props: AnimatedViewProps) {
const fadeAnim = React.useRef(new Animated.Value(0)).current; // Initial value for opacity: 0

React.useEffect(() => {
Animated.timing(fadeAnim, {
toValue: 1,
duration: props.fadeInDuration ?? 250,
useNativeDriver: props.useNativeDriver ?? true,
}).start();
}, [fadeAnim]);

return (
<Animated.View
style={{
...props.style,
opacity: fadeAnim,
}}
>
{props.children}
</Animated.View>
);
}
36 changes: 36 additions & 0 deletions examples/basic/components/__tests__/AnimatedView.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { act, render, screen } from '@testing-library/react-native';
import { AnimatedView } from '../AnimatedView';

describe('AnimatedView', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

it('should use native driver when useNativeDriver is true', async () => {
render(
<AnimatedView fadeInDuration={250} useNativeDriver={true}>
Test
</AnimatedView>,
);
expect(screen.root).toHaveStyle({ opacity: 0 });

await act(() => jest.advanceTimersByTime(250));
expect(screen.root).toHaveStyle({ opacity: 1 });
});

it('should not use native driver when useNativeDriver is false', async () => {
render(
<AnimatedView fadeInDuration={250} useNativeDriver={false}>
Test
</AnimatedView>,
);
expect(screen.root).toHaveStyle({ opacity: 0 });

await act(() => jest.advanceTimersByTime(250));
expect(screen.root).toHaveStyle({ opacity: 1 });
});
});
1 change: 1 addition & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { resetToDefaults } from './src/pure';
import './src/matchers/extend-expect';

beforeEach(() => {
resetToDefaults();
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
preset: './jest-preset',
setupFilesAfterEnv: ['./jest-setup.ts'],
testPathIgnorePatterns: ['timer-utils', 'examples/', 'experiments-app/', 'experiments-rtl/'],
testPathIgnorePatterns: ['build/', 'examples/', 'experiments-app/', 'timer-utils'],
testTimeout: 60000,
transformIgnorePatterns: ['/node_modules/(?!(@react-native|react-native)/).*/'],
snapshotSerializers: ['@relmify/jest-serializer-strip-ansi/always'],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"jest": "^29.7.0",
"prettier": "^2.8.8",
"react": "18.3.1",
"react-native": "0.76.0-rc.6",
"react-native": "0.76.0",
"react-test-renderer": "18.3.1",
"release-it": "^17.6.0",
"strip-ansi": "^6.0.1",
Expand Down
1 change: 0 additions & 1 deletion src/__tests__/fire-event.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
View,
} from 'react-native';
import { fireEvent, render, screen } from '..';
import '../matchers/extend-expect';

type OnPressComponentProps = {
onPress: () => void;
Expand Down
67 changes: 67 additions & 0 deletions src/__tests__/react-native-animated.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as React from 'react';
import { Animated, ViewStyle } from 'react-native';
import { act, render, screen } from '..';

type AnimatedViewProps = {
fadeInDuration?: number;
style?: ViewStyle;
children: React.ReactNode;
useNativeDriver?: boolean;
};

function AnimatedView(props: AnimatedViewProps) {
const fadeAnim = React.useRef(new Animated.Value(0)).current; // Initial value for opacity: 0

React.useEffect(() => {
Animated.timing(fadeAnim, {
toValue: 1,
duration: props.fadeInDuration ?? 250,
useNativeDriver: props.useNativeDriver ?? true,
}).start();
}, [fadeAnim, props.fadeInDuration, props.useNativeDriver]);

return (
<Animated.View
style={{
...props.style,
opacity: fadeAnim,
}}
>
{props.children}
</Animated.View>
);
}

describe('AnimatedView', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

it('should use native driver when useNativeDriver is true', async () => {
render(
<AnimatedView fadeInDuration={250} useNativeDriver={true}>
Test
</AnimatedView>,
);
expect(screen.root).toHaveStyle({ opacity: 0 });

await act(() => jest.advanceTimersByTime(250));
expect(screen.root).toHaveStyle({ opacity: 1 });
});

it('should not use native driver when useNativeDriver is false', async () => {
render(
<AnimatedView fadeInDuration={250} useNativeDriver={false}>
Test
</AnimatedView>,
);
expect(screen.root).toHaveStyle({ opacity: 0 });

await act(() => jest.advanceTimersByTime(250));
expect(screen.root).toHaveStyle({ opacity: 1 });
});
});
13 changes: 0 additions & 13 deletions src/matchers/__tests__/extend-expect.test.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/matchers/__tests__/to-be-busy.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { View } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toBeBusy() basic case', () => {
render(
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-be-checked.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import { type AccessibilityRole, Switch, View } from 'react-native';
import render from '../../render';
import { screen } from '../../screen';
import '../extend-expect';

function renderViewsWithRole(role: AccessibilityRole) {
render(
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-be-disabled.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
View,
} from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toBeDisabled()/toBeEnabled() supports basic case', () => {
render(
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-be-empty-element.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { View } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function DoNotRenderChildren({ children }: { children: React.ReactNode }) {
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-be-expanded.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { View } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toBeExpanded() basic case', () => {
render(
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-be-on-the-screen.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { View, Text } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toBeOnTheScreen() example test', () => {
render(
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-be-partially-checked.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import { type AccessibilityRole, View } from 'react-native';
import render from '../../render';
import { screen } from '../../screen';
import '../extend-expect';

function renderViewsWithRole(role: AccessibilityRole) {
return render(
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-be-selected.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { View } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toBeSelected() basic case', () => {
render(
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-be-visible.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { View, Modal } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toBeVisible() on empty view', () => {
render(<View testID="view" />);
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-contain-element.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { View } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toContainElement() supports basic case', () => {
render(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { View } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

describe('toHaveAccessibilityValue', () => {
it('supports "accessibilityValue.min"', () => {
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-have-accessible-name.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { View, Text, TextInput, Image } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toHaveAccessibleName() handles view with "accessibilityLabel" prop', () => {
render(<View testID="view" accessibilityLabel="Test label" />);
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-have-display-value.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { TextInput, View } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toHaveDisplayValue() example test', () => {
render(<TextInput testID="text-input" value="test" />);
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-have-prop.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { View, Text, TextInput } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toHaveProp() basic case', () => {
render(
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-have-style.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { StyleSheet, View, Pressable } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

const styles = StyleSheet.create({
container: { borderBottomColor: 'white' },
Expand Down
1 change: 0 additions & 1 deletion src/matchers/__tests__/to-have-text-content.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { View, Text } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('toHaveTextContent() example test', () => {
render(
Expand Down
1 change: 0 additions & 1 deletion src/user-event/__tests__/clear.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from 'react';
import { TextInput, TextInputProps, View } from 'react-native';
import { createEventLogger, getEventsNames } from '../../test-utils';
import { render, userEvent, screen } from '../..';
import '../../matchers/extend-expect';

beforeEach(() => {
jest.useRealTimers();
Expand Down
1 change: 0 additions & 1 deletion src/user-event/__tests__/paste.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from 'react';
import { TextInput, TextInputProps, View } from 'react-native';
import { createEventLogger, getEventsNames } from '../../test-utils';
import { render, userEvent, screen } from '../..';
import '../../matchers/extend-expect';

beforeEach(() => {
jest.useRealTimers();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { FlatList, ScrollViewProps, Text, View } from 'react-native';
import { render, screen } from '../../..';
import '../../../matchers/extend-expect';
import { EventEntry, createEventLogger } from '../../../test-utils';
import { userEvent } from '../..';

Expand Down
1 change: 0 additions & 1 deletion src/user-event/type/__tests__/type.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { TextInput, TextInputProps, View } from 'react-native';
import { createEventLogger, getEventsNames, lastEventPayload } from '../../../test-utils';
import { render, screen } from '../../..';
import { userEvent } from '../..';
import '../../../matchers/extend-expect';

beforeEach(() => {
jest.useRealTimers();
Expand Down
Loading

0 comments on commit a60ce28

Please sign in to comment.