简体   繁体   English

当没有使用测试库 react 和 jest 传递给它的道具时,将调用按钮的测试 onClick

[英]Test onClick of a button is called when there is no props passed down to it using testing library react and jest

I have a component for an upload button which calls the ref of an input when clicked:我有一个上传按钮的组件,它在单击时调用输入的引用:

import { useRef, FC } from 'react';
import Button from '@mui/lab/LoadingButton';

const UploadButton: FC<UploadButtonProps> = ({
  onFileChange,
  loading,
  text = 'Import File',
  accept = '.csv',
  disabled = false,
}) => {
  const inputRef = useRef<HTMLInputElement>(null);
  const onChange = ({ target }: { target: HTMLInputElement }) => {
    const { files } = target;
    const file = files?.[0];
    file && onFileChange?.(file);
    target.value = '';
  };
  return (
    <>
      <Button
        loading={loading}
        color="primary"
        variant="contained"
        disabled={disabled}
        onClick={() => {
          inputRef.current?.click();
        }}
      >
        {text}
      </Button>
      <input
        aria-label="hidden-upload-input"
        ref={inputRef}
        style={{
          display: 'none',
        }}
        type="file"
        accept={accept}
        required
        onChange={onChange}
      />
    </>
  );
};

export default UploadButton;

interface UploadButtonProps {
  onFileChange?: (file: File) => void;
  loading?: boolean;
  text?: string;
  accept?: string;
  disabled?: boolean;
}

I want to test that when button is clicked its onClick is called or inputRef.current.click is called but I don't know how.我想测试当单击按钮时它的 onClick 被调用或 inputRef.current.click 被调用但我不知道如何。 I assume that I need to spyOn the onClick prop somehow.我假设我需要以某种方式监视 onClick 道具。

I tried something like this:我试过这样的事情:

it('should call button onClick if being clicked',() => {
const upload = render(<UploadButton text="upload text" />);

const buttonClick = jest.spyOn(upload, 'onClick');

const button = screen.getByText('upload text');

fireEvent.click(button);

expect(button.onclick).toBeCalled();
});

as a workaround I ended up adding a console log in the onClick function and spyOn it作为一种解决方法,我最终在 onClick function 中添加了一个控制台日志并监视它

import { useRef, FC } from 'react';
import Button from '@mui/lab/LoadingButton';

const UploadButton: FC<UploadButtonProps> = ({
  onFileChange,
  loading,
  text = 'Import File',
  accept = '.csv',
  disabled = false,
}) => {
  const inputRef = useRef<HTMLInputElement>(null);
  const onChange = ({ target }: { target: HTMLInputElement }) => {
    const { files } = target;
    const file = files?.[0];
    file && onFileChange?.(file);
    target.value = '';
  };

  return (
    <>
      <Button
        loading={loading}
        color="primary"
        variant="contained"
        disabled={disabled}
        onClick={() => {
          inputRef.current?.click();
          console.log('upload');
        }}
      >
        {text}
      </Button>
      <input
        aria-label="hidden-upload-input"
        ref={inputRef}
        style={{
          display: 'none',
        }}
        type="file"
        accept={accept}
        required
        onChange={onChange}
      />
    </>
  );
};

export default UploadButton;

interface UploadButtonProps {
  onFileChange?: (file: File) => void;
  loading?: boolean;
  text?: string;
  accept?: string;
  disabled?: boolean;
}

and test it like this并像这样测试

  it('should call button onClick if being clicked', async () => {
    render(<UploadButton text="upload text" />);

    const spy = jest.spyOn(console, 'log');

    const button = screen.getByText('upload text');

    fireEvent.click(button);

    expect(spy).toBeCalled();
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM