如何使用React测试库模拟ResizeObserver在单元测试中工作

时间:2020-10-27 15:48:44

标签: reactjs unit-testing jestjs react-testing-library window-resize

如果有人可以提供帮助,我有一个使用ResizeObserver更改组件宽度的自定义钩子。我的问题是,当我去运行单元测试时,它将破坏我的所有测试,并且查看快照时,它不会呈现dom中的所有元素。在我实现ResizeObserver之前,它一直在工作。有谁知道我是否有一种用jest.mock将ResizeObserver设置为不确定的方法。或其他建议。

import * as React from 'react';
import ResizeObserver from 'resize-observer-polyfill';

const useResizeObserver = (ref: { current: any }) => {
    const [dimensions, setDimensions] = React.useState<DOMRectReadOnly>();
    React.useEffect(() => {
        const observeTarget = ref.current;
        const resizeObserver = new ResizeObserver((entries) => {
            entries.forEach((entry) => {
                setDimensions(entry.contentRect);
            });
        });
        resizeObserver.observe(observeTarget);
        return () => {
            resizeObserver.unobserve(observeTarget);
        };
    }, [ref]);
    return dimensions;
};

export default useResizeObserver;



import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';

import mockFetchProfileActivity from '../../../services/mocks/fetch-profile-activity';
import BarChart from './BarChart';

const component = <BarChart userActivity={mockFetchProfileActivity} />;

describe('Render barElement Chart component', () => {
    const observers: any[] = [];
    let resizeHandler: (observers: any[]) => void;
    (window as any).ResizeObserver = (e: any) => {
        resizeHandler = e;

        return {
            observe(element: any) {
                observers.push(element);
            },
            unobserve(element: any) {
                const i = observers.indexOf(element);
                if (i !== -1) {
                    observers.splice(i, 1);
                }
            }
        };
    };

    it('Matches the snapshot', () => {
        // resizeHandler(observers);
        const container = render(component);
        expect(container).toMatchSnapshot();
    });

    it('when clicking on a chart barElement drilldown "challenges" are shown', async () => {
        // arrange
        const componentRender = render(component);
        waitFor(() => resizeHandler(observers));

        // act
        const barElement = componentRender.container.querySelector('svg rect');

        if (barElement) userEvent.click(barElement);

        // assert
        expect(screen.getByText('Challenge 1')).toBeInTheDocument();
    });
});

3 个答案:

答案 0 :(得分:3)

我选择将 polyfill 添加为开发依赖项,并将以下行添加到 setupTests.js/ts:

global.ResizeObserver = require('resize-observer-polyfill')

答案 1 :(得分:2)

我在使用 Create React App 设置时遇到了类似的问题。

如果是这种情况,您可以在根目录中创建一个名为 setupTest.js 的文件并添加以下代码:

  import '@testing-library/jest-dom/extend-expect';
  import 'jest-extended';
    
  jest.mock('./hooks/useResizeObserver', () => () => ({
    __esModule: true,
    default: jest.fn().mockImplementation(() => ({
        observe: jest.fn(),
        unobserve: jest.fn(),
        disconnect: jest.fn(),
    })),
  }));

您可以找到更多信息来配置 Create React App here 和 ResizeObserver API here 的测试环境

答案 2 :(得分:0)

模拟ResizeObserver:

class ResizeObserver {
    observe() {
        // do nothing
    }
    unobserve() {
        // do nothing
    }
}

window.ResizeObserver = ResizeObserver;
export default ResizeObserver;
sample.test.js*

import ResizeObserver from './__mocks__/ResizeObserver';
import module from 'sample;

describe('module', ()=> {
     it('returns an instance of ResizeObserver', () => {
           // do something that uses the resize observer
           // NOTE: The actual observe handler would not be called in jsdom anyways as no resize would be triggered.
           // e.g.
           expect(module.somethingThatReturnAReference to the resize observer).toBeInstanceOf(ResizeObserver);
        });
});

source