mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
fix: proper act usage (#1592)
* fix: use proper act # Conflicts: # src/render-act.ts * fix: user event missing act on min press duration * chore: add button test * chore: spellcheck
This commit is contained in:
committed by
GitHub
parent
f7e3b1a907
commit
ec032b0fc2
@@ -1,7 +1,6 @@
|
||||
/* istanbul ignore file */
|
||||
|
||||
import { act } from 'react-test-renderer';
|
||||
import { getIsReactActEnvironment, setReactActEnvironment } from '../act';
|
||||
import act, { getIsReactActEnvironment, setReactActEnvironment } from '../act';
|
||||
import { flushMicroTasksLegacy } from '../flush-micro-tasks';
|
||||
import { checkReactVersionAtLeast } from '../react-versions';
|
||||
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
import TestRenderer from 'react-test-renderer';
|
||||
import type { ReactTestRenderer, TestRendererOptions } from 'react-test-renderer';
|
||||
import act from './act';
|
||||
|
||||
export function renderWithAct(
|
||||
component: React.ReactElement,
|
||||
@@ -8,7 +9,7 @@ export function renderWithAct(
|
||||
let renderer: ReactTestRenderer;
|
||||
|
||||
// This will be called synchronously.
|
||||
void TestRenderer.act(() => {
|
||||
void act(() => {
|
||||
renderer = TestRenderer.create(component, options);
|
||||
});
|
||||
|
||||
|
||||
@@ -203,7 +203,7 @@ describe('userEvent.press with real timers', () => {
|
||||
expect(getEventsName(events)).toEqual(['pressIn', 'press', 'pressOut']);
|
||||
});
|
||||
|
||||
test('doesnt trigger on disabled Text', async () => {
|
||||
test('does not trigger on disabled Text', async () => {
|
||||
const { events, logEvent } = createEventLogger();
|
||||
|
||||
render(
|
||||
@@ -222,7 +222,7 @@ describe('userEvent.press with real timers', () => {
|
||||
expect(events).toEqual([]);
|
||||
});
|
||||
|
||||
test('doesnt trigger on Text with disabled pointer events', async () => {
|
||||
test('does not trigger on Text with disabled pointer events', async () => {
|
||||
const { events, logEvent } = createEventLogger();
|
||||
|
||||
render(
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
TouchableHighlight,
|
||||
TouchableOpacity,
|
||||
View,
|
||||
Button,
|
||||
} from 'react-native';
|
||||
import { createEventLogger, getEventsName } from '../../../test-utils';
|
||||
import { render, screen } from '../../..';
|
||||
@@ -201,6 +202,15 @@ describe('userEvent.press with fake timers', () => {
|
||||
expect(getEventsName(events)).toEqual(['pressIn', 'press', 'pressOut']);
|
||||
});
|
||||
|
||||
test('press works on Button', async () => {
|
||||
const { events, logEvent } = createEventLogger();
|
||||
|
||||
render(<Button title="press me" onPress={logEvent('press')} />);
|
||||
|
||||
await userEvent.press(screen.getByText('press me'));
|
||||
expect(getEventsName(events)).toEqual(['press']);
|
||||
});
|
||||
|
||||
test('longPress works Text', async () => {
|
||||
const { events, logEvent } = createEventLogger();
|
||||
|
||||
@@ -219,7 +229,7 @@ describe('userEvent.press with fake timers', () => {
|
||||
expect(getEventsName(events)).toEqual(['pressIn', 'longPress', 'pressOut']);
|
||||
});
|
||||
|
||||
test('doesnt trigger on disabled Text', async () => {
|
||||
test('does not trigger on disabled Text', async () => {
|
||||
const { events, logEvent } = createEventLogger();
|
||||
|
||||
render(
|
||||
@@ -238,7 +248,7 @@ describe('userEvent.press with fake timers', () => {
|
||||
expect(events).toEqual([]);
|
||||
});
|
||||
|
||||
test('doesnt trigger on Text with disabled pointer events', async () => {
|
||||
test('does not trigger on Text with disabled pointer events', async () => {
|
||||
const { events, logEvent } = createEventLogger();
|
||||
|
||||
render(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ReactTestInstance } from 'react-test-renderer';
|
||||
import act from '../../act';
|
||||
import { getHostParent } from '../../helpers/component-tree';
|
||||
import { isTextInputEditable } from '../../helpers/text-input';
|
||||
import { isPointerEventEnabled } from '../../helpers/pointer-events';
|
||||
@@ -82,7 +83,9 @@ const emitPressablePressEvents = async (
|
||||
// before emitting the `pressOut` event. We need to wait here, so that
|
||||
// `press()` function does not return before that.
|
||||
if (DEFAULT_MIN_PRESS_DURATION - options.duration > 0) {
|
||||
await wait(config, DEFAULT_MIN_PRESS_DURATION - options.duration);
|
||||
await act(async () => {
|
||||
await wait(config, DEFAULT_MIN_PRESS_DURATION - options.duration);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user