Skip to content

Warning An update to ForwardRef inside a test was not wrapped in act(...) #1668

Closed as not planned
@obaricisse

Description

@obaricisse

Describe the bug

How do I get rid of this warning an update to ForwardRef inside a test was not wrapped in act?
I am already using act in my test but keep getting this warning even though test passes. see repro step.

Expected behavior

Steps to Reproduce

Here is an example component and the output.

import React, { PureComponent } from "react";
import { Button, Text, View } from "react-native";
import { connect } from "react-redux";
import { render, fireEvent, act } from "@testing-library/react-native";
import configureMockStore from "redux-mock-store";
import { Provider } from "react-redux";
import "core-js"; 

interface Props {
  count: number;
  incrementCount: () => void;
}

interface State {
  internalCount: number;
}

export class MyPureComponent extends PureComponent<Props, State> {
  constructor(props: Props) {
    super(props);
    this.state = {
      internalCount: props.count,
    };
  }

  componentDidUpdate(prevProps: Props) {
    if (prevProps.count !== this.props.count) {
      this.setState({ internalCount: this.props.count });
    }
  }

  render() {
    return (
      <View>
        <Text testID="internal-count">{this.state.internalCount}</Text>
        <Button
          disabled={this.props.count >= 1}
          title="Increment"
          testID="increment-button"
          onPress={this.props.incrementCount}
        />
      </View>
    );
  }
}

const mapStateToProps = (state: { count: number }) => ({
  count: state.count,
});

const mapDispatchToProps = (dispatch: any) => ({
  incrementCount: () => dispatch({ type: "INCREMENT" }),
});

export const ConnectedMyPureComponent = connect(
  mapStateToProps,
  mapDispatchToProps
)(MyPureComponent);

const mockStore = configureMockStore([]);

describe("MyPureComponent", () => {
  let store: any;

  beforeEach(() => {
    // Initial state for the store
    store = mockStore({
      count: 0, // Initial Redux state
    });

    store.dispatch = jest.fn(); // Mock dispatch
  });

  it("should update the internal state when the Redux count changes", () => {
    const { getByTestId, rerender } = render(
      <Provider store={store}>
        <ConnectedMyPureComponent />
      </Provider>
    );

    // Initial state should be 0
    expect(getByTestId("internal-count").props.children).toBe(0);

    // Update Redux state and rerender the component
    act(() => {
      store = mockStore({ count: 5 });
      rerender(
        <Provider store={store}>
          <ConnectedMyPureComponent />
        </Provider>
      );
    });

    // Now the internal state should be updated to 5
    expect(getByTestId("internal-count").props.children).toBe(5);
  });
});

Screenshots

image

Versions

12.7.2

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions