Skip to content

Commit

Permalink
fix: proper act usage (#1592)
Browse files Browse the repository at this point in the history
* fix: use proper act

# Conflicts:
#	src/render-act.ts

* fix: user event missing act on min press duration

* chore: add button test

* chore: spellcheck
  • Loading branch information
mdjastrzebski authored Apr 26, 2024
1 parent f7e3b1a commit ec032b0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/helpers/wrap-async.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
3 changes: 2 additions & 1 deletion src/render-act.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
});

Expand Down
4 changes: 2 additions & 2 deletions src/user-event/press/__tests__/press.real-timers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
14 changes: 12 additions & 2 deletions src/user-event/press/__tests__/press.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
TouchableHighlight,
TouchableOpacity,
View,
Button,
} from 'react-native';
import { createEventLogger, getEventsName } from '../../../test-utils';
import { render, screen } from '../../..';
Expand Down Expand Up @@ -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();

Expand All @@ -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(
Expand All @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion src/user-event/press/press.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
});
}
};

Expand Down

0 comments on commit ec032b0

Please sign in to comment.