javascript Jest / Enzyme -如何在不同的视口进行测试?

im9ewurl  于 2023-06-28  发布在  Java
关注(0)|答案(3)|浏览(158)

我正尝试在某个视口宽度的组件上运行测试。我正在做以下事情,但这似乎并没有改变它:

test('Component should do something at a certain viewport width.', () => {
    global.innerWidth = 2000;
    const component = mount(<SomeComponent />);
    // ...
});

我还找到了一篇解释如何使用JSDom实现这一点的文章,但由于Jest现在随JSDom一起提供,我想知道是否有一个本地解决方案。
https://www.codementor.io/pkodmad/dom-testing-react-application-jest-k4ll4f8sd

vshtjzan

vshtjzan1#

背景资料:

  • jsdom不实现window.resizeBy()window.resizeTo()
  • jsdom将窗口innerWidth和innerHeight定义为1024 x 768
  • 通过手动设置window.innerWidth和window.innerHeight并触发resize事件,可以使用jsdom模拟窗口调整大小

下面是一个例子:
comp.js

import * as React from 'react';

export default class Comp extends React.Component {
  constructor(...args) {
    super(...args);
    this.state = { width: 0, height: 0 }
  }
  updateDimensions = () => {
    this.setState({ width: window.innerWidth, height: window.innerHeight });
  }
  componentDidMount() {
    this.updateDimensions();
    window.addEventListener("resize", this.updateDimensions);
  }
  componentWillUnmount() {
    window.removeEventListener("resize", this.updateDimensions);
  }
  render() {
    return <div>{this.state.width} x {this.state.height}</div>;
  }
}

comp.test.js

import * as React from 'react';
import { shallow } from 'enzyme';

import Comp from './comp';

const resizeWindow = (x, y) => {
  window.innerWidth = x;
  window.innerHeight = y;
  window.dispatchEvent(new Event('resize'));
}

describe('Comp', () => {
  it('should display the window size', () => {
    const component = shallow(<Comp />);

    expect(component.html()).toEqual('<div>1024 x 768</div>');

    resizeWindow(500, 300);
    expect(component.html()).toEqual('<div>500 x 300</div>');

    resizeWindow(2880, 1800);
    expect(component.html()).toEqual('<div>2880 x 1800</div>');
  });
});

注意事项:

ffdz8vbo

ffdz8vbo2#

如果你使用TypeScript,它会抱怨window.innerWidth/innerHeight是只读的。您可以通过重新声明属性来解决此问题:

Object.defineProperty(window, 'innerWidth', {writable: true, configurable: true, value: 105})

或者使用Object.assign方法:

window = Object.assign(window, { innerWidth: 105 });

这两个都不是很好的解决方案,但它们都有效。

0pizxfdo

0pizxfdo3#

下面的测试适合我。源代码不再标记为未覆盖。

it('resize event listener changes the state', () => {
  const wrapper = shallow(<Component />);
  const instance = wrapper.instance();

  instance.setState({
    mobileMode: true
  });

  global.innerWidth = 800;
  window.dispatchEvent(new Event('resize'));
  expect(instance.state.mobileMode).toBeFalsy();

  global.innerWidth = 600;
  window.dispatchEvent(new Event('resize'));
  expect(instance.state.mobileMode).toBeTruthy();
});

调整组件中监听器的大小:

// ...
  resizeListener = () => {
      if (window.innerWidth < 768) {
        this.setState({
          mobileMode: true
        });
      } else {
        this.setState({
          mobileMode: false
        });
      }
    };
    window.addEventListener('resize', resizeListener);
// ...

相关问题