首页 > 解决方案 > 用于 Jest 测试的模拟文档

问题描述

我在反应组件中有这样的功能:

    handleOnScroll = () => {
        const {navigationSections, setNavigationSectionActive} = this.props;

        const reversedSections = this.getReversedNavigationSections();

        const OFFSET_TOP = 32;
        const st = window.pageYOffset || document.documentElement.scrollTop; 

        if (st > lastScrollTop) {
            for (let i = 0; i < navigationSections.length; i += 1) {
                if(document.getElementById(navigationSections[i].id).getBoundingClientRect().top <= OFFSET_TOP) {
                    setNavigationSectionActive(navigationSections[i].id);
                }
            }

        } else if (st < lastScrollTop) {
            for (let y = 0; y < reversedSections.length; y += 1) {
                if(document.getElementById(navigationSections[y].id).getBoundingClientRect().top <= OFFSET_TOP) {
                    setNavigationSectionActive(navigationSections[y].id);
                }
            }
        }

        lastScrollTop = st <= 0 ? 0 : st;

    }

和一些像这样的测试:

    it('should handle handleOnScroll', () => {
        instance.handleOnScroll();
        expect(instance.getReversedNavigationSections()).toEqual(props.navigationSections.reverse());
    });

    props.navigationSections.forEach(navSection => {
        it('should call setNavigationSectionActive', () => {
            instance.handleOnScroll();
            expect(props.setNavigationSectionActive).toHaveBeenCalledWith(navSection.id);
        });
    });

如您所见,第一个测试通过但第二个测试('应该调用 setNavigationSectionActive')失败:

在此处输入图像描述

我认为原因是因为文档没有被嘲笑,因此 if 失败。但是,在执行此操作时的实际实现中:

document.getElementById(navigationSections[i].id).getBoundingClientRect().top 

具有这些 ID 的 DIV 位于另一部分(不在用于相关测试的包装器组件中)。

我应该模拟文档以模仿 if 语句通过的实际结构还是我完全错了?

到目前为止我的尝试

    it('should handle custom handleOnScroll', () => {
        document.body.innerHTML = '<div><div id="id">my div</div><div id="id-1">my div</div></div>';

        const div = document.getElementById('id');
        div.getBoundingClientRect = () => ({ top: 100 });  // <= mock getBoundingClientRect
        instance.handleOnScroll();
        props.navigationSections.forEach(() => {
            if (global.document.getElementById('id').getBoundingClientRect().top <= global.OFFSET_TOP) {
                expect(props.setNavigationSectionActive).toHaveBeenCalledWith('id');
            }
        });
    });

标签: javascriptunit-testingjestjs

解决方案


默认的测试环境Jestjsdom提供类似浏览器的环境。

如果您的测试需要特定内容,document那么您可以使用类似document.body.innerHTML.

jsdom实现了很多浏览器功能,但不是全部。在这种情况下getBoundingClientRect,它总是返回0,所以如果你想让它返回其他东西,你必须模拟它。

这是一个简单的工作示例,可帮助您入门:

const OFFSET_TOP = 5;

const func = () => 
  document.getElementById('theid').getBoundingClientRect().top <= OFFSET_TOP ?
    'less' :
    'more';

test('func', () => {
  document.body.innerHTML = '<div id="theid">my div</div>';
  expect(func()).toBe('less');  // Success!

  const div = document.getElementById('theid');
  div.getBoundingClientRect = () => ({ top: 10 });  // <= mock getBoundingClientRect
  expect(func()).toBe('more');  // Success!
});

推荐阅读