Search code examples
javascriptreactjsreact-testing-libraryuser-event

Why clear method not exist on @testing-library/user-event


I am working on a CMS project and I just came across an issue. _userEvent.default.clear is not a function.

import user from '@testing-library/user-event'

test('can edit label', async () => {
    createArticleMock()
    await renderRootAndInitStore(rightNowTeasers, globalInitialState)

    const index = 1
    const input = screen.getByDisplayValue(labels[index])
    const newLabel = 'VÄRLDEN'
    user.clear(input)
    user.type(input, newLabel)

    expect(await screen.findByDisplayValue(newLabel))
    user.click(screen.getByTestId('settings-form-save'))
    await screen.findByText(newLabel)
})

When I visit @testing-library/user-event, I see those lines

// Definitions by: Wu Haotian <https://github.com/whtsky>
export interface IUserOptions {
    allAtOnce?: boolean;
    delay?: number;
}

export interface ITabUserOptions {
    shift?: boolean;
    focusTrap?: Document | Element;
}

export type TargetElement = Element | Window;

declare const userEvent: {
    click: (element: TargetElement) => void;
    dblClick: (element: TargetElement) => void;
    selectOptions: (element: TargetElement, values: string | string[]) => void;
    type: (
        element: TargetElement,
        text: string,
        userOpts?: IUserOptions
    ) => Promise<void>;
    tab: (userOpts?: ITabUserOptions) => void;
};

export default userEvent;

As the file shows, there is no clear() method on userEvent. But the documentation points out to clear() method.

Doc => Doc

This is first time I use this library. Anyone knows what the issue is?


Solution

  • You Have to update the @testing-library/user-event to the latest version. Then that error will be resolved. You Can find the latest version over here: https://www.npmjs.com/package/@testing-library/user-event