- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的项目中有以下 react 代码
import React from 'react';
import { Upload } from 'antd';
const { Dragger } = Upload;
...
<Dragger
accept={ACCEPTED_FORMATS}
beforeUpload={beforeUpload}
data-testid="upload-dragger"
maxCount={1}
onChange={({ file: { status } }) => {
if (status === 'done') onUploadComplete();
}}
progress={progress}
showUploadList={false}
>
{/* here i have a button, code ommited for clarity, if needed i'll post it */}
</Dragger>
我想测试一下回调函数
onUploadComplete()
当 file.status 为
'done'
时调用.
import React from 'react';
import { fireEvent, render, waitFor } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import { DraggerProps } from 'antd/lib/upload';
import userEvent from '@testing-library/user-event';
import UploadCompanyLogo from '../UploadCompanyLogo'; // This is the component where the dragger is placed
jest.mock('antd', () => {
const antd = jest.requireActual('antd');
const { Upload } = antd;
const { Dragger } = Upload;
const MockedDragger = (props: DraggerProps) => {
console.log('log test');
return (
<Dragger
{...props}
action="greetings"
customRequest={({ onSuccess }) => {
setTimeout(() => {
onSuccess('ok');
}, 0);
}}
/>
);
};
return { ...antd, Upload: { ...Upload, Dragger: MockedDragger } };
});
antd
),模拟上传,然后检查是否已调用回调。 it('completes the image upload', async () => {
const flushPromises = () => new Promise(setImmediate);
const { getByTestId } = render(elementRenderer({ onUploadComplete }));
const file = new File(['(⌐□_□)'], 'chucknorris.png', { type: 'image/png' });
const uploadDragger = await waitFor(() => getByTestId('upload-dragger'));
await act(async () => {
userEvent.upload(uploadDragger, file);
});
await flushPromises();
expect(onUploadComplete).toHaveBeenCalledTimes(1);
expect(onUploadComplete).toHaveBeenCalledWith();
});
const elementRenderer = ({
onUploadComplete = () => {}
}) => (
<ApplicationProvider>
<UploadCompanyLogo
onUploadComplete={onUploadComplete}
/>
</ApplicationProvider>
);
import React, { PropsWithChildren } from 'react';
import { ConfigProvider as AntdProvider } from 'antd';
import { RendererProvider as FelaProvider } from 'react-fela';
import { createRenderer } from 'fela';
import { I18nextProvider } from 'react-i18next';
import antdExternalContainer, {
EXTERNAL_CONTAINER_ID,
} from 'src/util/antdExternalContainer';
import { antdLocale } from 'src/util/locales';
import rendererConfig from 'src/fela/felaConfig';
import i18n from 'src/i18n';
const ApplicationProvider = (props: PropsWithChildren<{}>) => {
const { children } = props;
return (
<AntdProvider locale={antdLocale} getPopupContainer={antdExternalContainer}>
<FelaProvider renderer={createRenderer(rendererConfig)}>
<I18nextProvider i18n={i18n}>
<div className="antd-local">
<div id={EXTERNAL_CONTAINER_ID} />
{children}
</div>
</I18nextProvider>
</FelaProvider>
</AntdProvider>
);
};
export default ApplicationProvider;
这目前不起作用,但已在
@diedu 的帮助下得到改进.
console.log()
我已放入当前未显示的 MockedDragger。如果我放一个
console.log()
在组件和 mockedDragger 中,它都会打印组件日志。
最佳答案
您应该更改的第一件事是您在模拟中所做的返回。您正在返回一个名为 MockedDragger
的新组件,而不是 Dragger
.
return { ...antd, Upload: { ...Upload, Dragger: MockedDragger } };
接下来是事件触发。根据
this issue您应该使用 RTL 用户事件库并将调用包装在
act
中
import userEvent from "@testing-library/user-event";
...
await act(async () => {
userEvent.upload(uploadDragger, file);
});
...
最后,由于运行了一些异步代码
you need to flush the pending promises在检查通话之前
const flushPromises = () => new Promise(setImmediate);
...
await flushPromises();
expect(onUploadComplete).toHaveBeenCalled()
这是完整版
import { render, waitFor } from "@testing-library/react";
import App from "./App";
import userEvent from "@testing-library/user-event";
import { act } from "react-dom/test-utils";
import { DraggerProps } from "antd/lib/upload";
jest.mock('antd', () => {
const antd = jest.requireActual('antd');
const { Upload } = antd;
const { Dragger } = Upload;
const MockedDragger = (props: DraggerProps) => {
return <Dragger {...props} customRequest={({ onSuccess }) => {
setTimeout(() => {
onSuccess('ok');
}, 0)
}} />;
};
return { ...antd, Upload: { ...Upload, Dragger: MockedDragger } };
});
it("completes the image upload", async () => {
const onUploadComplete = jest.fn();
const flushPromises = () => new Promise(setImmediate);
const { getByTestId } = render(
<App onUploadComplete={onUploadComplete} />
);
const file = new File(["(⌐□_□)"], "chucknorris.png", { type: "image/png" });
const uploadDragger = await waitFor(() => getByTestId("upload-dragger"));
await act(async () => {
userEvent.upload(uploadDragger, file);
});
await flushPromises();
expect(onUploadComplete).toHaveBeenCalled();
});
不幸的是,我
couldn't set up一个代码框来显示它正在工作,但是如果您遇到任何问题,请告诉我,我可以将代码推送到存储库。
关于javascript - 如何正确地 mock `antd` Upload Dragger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67895146/
你好,我想知道是否可以使用 mockdata 和 dragger 创建一个单例 我知道没有拖动器 2 的标准代码 public class Singleton { private Single
我的项目中有以下 react 代码 import React from 'react'; import { Upload } from 'antd'; const { Dragger } = Uplo
我正在将 d3.js 集成到我的 Angular 应用程序中。 这是我的 ngOnInit ngOnInit() { this.dragger = d3.drag() .on("drag", ()
我们使用了一个名为 jesse 的 jquery 库效果很好 问题是我们有一个 li 属性,在它里面我们使用了 onclick 事件..但不知何故它停止工作,我们无法调用 onclick 事件..这是
我正在尝试构建一个使用 goog.fx.Dragger 使目标可拖动的小型 om 组件。我需要 goog.fx.Dragger :as dragger 像这样: (ns om-draggable-me
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关于您编写的代码问题的问题必须在问题本身中描述具体问题 — 并且包括有效代码 以重现它。参见 SS
我是一名优秀的程序员,十分优秀!