formik useformikcontext测试与测试库

发布于 2025-01-21 04:56:29 字数 2244 浏览 1 评论 0原文

我正在尝试为我的输入组件编写一些单元测试,其中大多数已连接到formik,并且在某些情况下,当用户按Special键即(“ Enter”,“”,“,”,“”时,在我的自动完成组件之类的情况下,表单会得到验证。 “等),并生成标签。我希望我的单元测试检查此功能。

我正在使用助手功能在形式包装器中渲染组件,并使用test-test-library使用一些初始值。 我想以某种方式可以访问formik值,而我发现这样做的唯一方法是使用helper函数getformprops我模拟useformikcontext使用renderhook renderhook < /code>来自@testing-library/react-hooks

export const getFormProps = () => {
    return renderHook(() => useFormikContext<typeof formInitialValues>());
};

我不确定是否有更好的方法来执行此操作,或者是否缺少文档中的某些内容,到目前为止,我尚未管理使它工作。 它总是给我以下错误,

Warning: Formik context is undefined, please verify you are calling useFormikContext() as child of a <Formik> component.

我还尝试了在这里但是,每当我检查表单上的更新时,我总是会获取原始初始数据。

如果有人遇到类似问题,请分享您如何解决这些问题。 预先感谢您

it.only('add a tag', async () => {
    const tree = mountWithForm(<EmailsInput name="emails" label="Label" />);

    const { result } = getFormProps();
    const input = getEditable(tree);
    if (!input) return expect(input).toBe(null);

    expect(result.current.values.emails.length).toBe(0);

    await act(() => {
        fireEvent.change(input, { target: { textContent: newEmail } });
        fireEvent.keyPress(input, { key: 'Enter', code: 'Enter', charCode: 13 });
    });

    await waitFor(() => {
        expect(document.getSelection).toHaveBeenCalledTimes(1);
        expect(document.createRange).toHaveBeenCalledTimes(1);
        expect(input.textContent).toBeEmpty();
        // -----> expect(result.current.values.emails.length).toBe(1)
    });
});

使用以下包:

    "formik": "^2.2.9",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "jest": "^27.5.1",
    "jest-extended": "^2.0.0",
    "react-test-renderer": "^18.0.0",
    "@testing-library/react": "^13.0.1",
    "@testing-library/react-hooks": "^8.0.0",
    "@testing-library/jest-dom": "^5.16.4",

I am trying to write some unit tests for my input components, most of them are connected to Formik and there are cases like my Autocomplete component where the form gets validated when the user presses special keys i.e ( "Enter", ",", " ", etc ), and a Tag is generated. I want my unit test to check this functionality.

I am using a helper function to render the component inside a Formik wrapper with some initial values using testing-library.
I want to somehow get access to Formik values and the only way I have found to do so is with the helper function getFormProps in which I mock useFormikContext using renderHook from @testing-library/react-hooks,

export const getFormProps = () => {
    return renderHook(() => useFormikContext<typeof formInitialValues>());
};

I am not sure if there is a better way to do this or if I am missing something in the documentation, so far I haven't managed to get it to work.
It always gives me the following error

Warning: Formik context is undefined, please verify you are calling useFormikContext() as child of a <Formik> component.

I've also tried the suggested solution from here but whenever I checked for updates on my form I was always getting the original initial data.

If anyone has experienced similar issues please share how you resolved them.
Thank you in advance

it.only('add a tag', async () => {
    const tree = mountWithForm(<EmailsInput name="emails" label="Label" />);

    const { result } = getFormProps();
    const input = getEditable(tree);
    if (!input) return expect(input).toBe(null);

    expect(result.current.values.emails.length).toBe(0);

    await act(() => {
        fireEvent.change(input, { target: { textContent: newEmail } });
        fireEvent.keyPress(input, { key: 'Enter', code: 'Enter', charCode: 13 });
    });

    await waitFor(() => {
        expect(document.getSelection).toHaveBeenCalledTimes(1);
        expect(document.createRange).toHaveBeenCalledTimes(1);
        expect(input.textContent).toBeEmpty();
        // -----> expect(result.current.values.emails.length).toBe(1)
    });
});

I am using the following packages :

    "formik": "^2.2.9",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "jest": "^27.5.1",
    "jest-extended": "^2.0.0",
    "react-test-renderer": "^18.0.0",
    "@testing-library/react": "^13.0.1",
    "@testing-library/react-hooks": "^8.0.0",
    "@testing-library/jest-dom": "^5.16.4",

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文