Функция имитации кнопки вызова при нажатии с Jest и Enzyme - PullRequest
0 голосов
/ 21 апреля 2020

Это функция, которую я хочу проверить:

    const downloadTemperaturesGraph = React.useCallback(() => {
            if (chartRef.current) {
                domtoimage
                    .toBlob(chartRef.current, {
                        bgcolor: 'white',
                        filter: (node: Node) => node !== closeRef.current,
                    })
                    .then((blob) => {
                        fileDownload(blob, `${downloadedTrailerName}.png`);
                    });
            }
        }, [chartRef, downloadedTrailerName]);


     const actions = React.useMemo(() => {
            const closeButton = (
                <div>
                    <Button type="button" onClick={downloadTemperaturesGraph}>
                        DOWNLOAD
                    </Button>
                   ....
                </div>
            );

            return [closeButton];
        }, [downloadTemperaturesGraph]);

     ......

     <WidgetDialog
        headerActions={actions}
         ...
     </WidgetDialog>

Это мой тестовый пример:

 it.only('calls downloadTemperaturesGraph on header action download click', () => {
        // Arrange
        const wrapper = shallow(<TemperaturesGraphDialogComponent {...props} />);

        const downloadTemperaturesGraphMock = jest.fn();

        const headerActions = shallow(
            React.createElement('div', {}, wrapper.find(WidgetDialog).prop('headerActions')!)
        );

        // Act
        headerActions.find(Button).simulate('click');

        // Assert
        expect(downloadTemperaturesGraphMock).toHaveBeenCalled();
    });

Я получаю эту ошибку:

  ● TemperaturesGraphDialog › calls downloadTemperaturesGraph on header action download click

    expect(jest.fn()).toBeCalled()

    Expected number of calls: >= 1
    Received number of calls:    0

      85 | 
      86 |         // Assert
    > 87 |         expect(downloadTemperaturesGraphMock).toHaveBeenCalled();
         |                                               ^
      88 |     });
      89 |     it('calls onClose on dialog close', () => {
      90 |         // Arrange

Как правильно смоделировать функцию, подобную моей, чтобы вызываться при нажатии кнопки?

...