Skip to content

False positive from no-unnecessary-act #686

Open
@Belco90

Description

@Belco90

Discussed in #685

Originally posted by douglasjunior October 28, 2022
In one test case we called a function within ACT that ESLint indicates is a testing-library function, but is not.

image

And dismissCallback need to be wrapped with act because it dispatches a state update to close the AlertModal.

  it('should not auto close modal with autoDismiss=false', async () => {
    const mockOnDismiss = jest.fn();

    let dismissCallback: undefined | (() => void);

    const tree = render(
      <AlertModal
        visible
        title="Title"
        description="Lorem ipsum is placeholder text commonly used in the graphic"
        primaryButtonName="Primary Action"
        secondaryButtonName="Secondary Action"
        autoDismiss={false}
        onPressSecondary={event => {
          dismissCallback = event.dismiss;
        }}
        onDismiss={mockOnDismiss}
      />,
    );

    const buttonInstance = tree.getByTestId('alert-modal-secondary-button');
    fireEvent.press(buttonInstance);

    expect(dismissCallback).toBeDefined();

    expect(mockOnDismiss).not.toBeCalled();

    act(() => {
      dismissCallback?.();
    });

    expect(mockOnDismiss).toHaveBeenCalled();

This has been confirmed as a false positive. Removing the optional chaining from dismissCallback call fixes the issue:

imagen

But wrapping it in an if statement gets the problem back:

imagen

This seems related to the fact that CallExpression is not a direct child of the function body, so we are not detecting this correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions