cloudscape-design / components

React components for Cloudscape Design System
https://cloudscape.design/
Apache License 2.0
2.33k stars 152 forks source link

[Bug]: Warning when openning and then closing dialog in tests #2485

Closed JElgar closed 3 weeks ago

JElgar commented 1 month ago

Browser

No response

Package version

3.0.1335

React version

18.3.1

Description

When using test utils dom, opening and then closing a dialog has the following warning Warning: An update to Transition inside a test was not wrapped in act(...). This may be related to https://github.com/cloudscape-design/components/issues/2471?

Source code

No response

Reproduction

import { Select } from '@amzn/awsui-components-react';
import wrapper from '@amzn/awsui-components-react/polaris/test-utils/dom';
import { render } from '@testing-library/react';
import { test } from 'vitest';

function TestThing() {
    return (
        <div>
            <Select
                options={[
                    {
                        description: 'abc',
                        value: '123',
                    },
                ]}
                selectedOption={null}
            />
        </div>
    );
}

test('thing', async () => {
    const { container } = render(<TestThing />);
    const select = wrapper(container).findSelect()!;
    select.openDropdown();
    select.closeDropdown();
});

Code of Conduct

MathiasSM commented 1 month ago

They seem to be correctly wrapped for both current version and your version (keep in mind SelectWrapper extends that class)

Could it be because of the async test callback? (I don't think it should affect it but no idea)

For maintainers: I wonder if this test has the same issue?

Disclaimer: I'm not a mantainer for this package

gethinwebster commented 1 month ago

I've taken a bit more of a look into this and I can indeed recreate it, although in vitest only: it doesn't occur in our own tests, but they use jest.

I have a feeling it has something to do with our use of motion/transitions in the component, but usual things like disabling motion or fake timers don't seem to help. I'll continue to look into this a bit further.

gethinwebster commented 3 weeks ago

We are continuing to investigate fixing this internally, but as a workaround, you can get rid of the warnings by wrapping open/close dropdown actions in an async act call:

test('thing', async () => {
    const { container } = render(<TestThing />);
    const select = wrapper(container).findSelect()!;
    await act(async () => select.openDropdown()};
    await act(async () => select.closeDropdown()};
});
JElgar commented 3 weeks ago

Thanks @gethinwebster. That work around works! Head us that Ive also observed this issue with dialogs. Would it make sense to keep this issue open until a fix is released or is that not how you do things around here?