Javascript 如何通过在构造函数中创建新事件对组件进行单元测试?
我有一个组件,它正在创建一个新事件,在组件更新时发送Javascript 如何通过在构造函数中创建新事件对组件进行单元测试?,javascript,reactjs,enzyme,Javascript,Reactjs,Enzyme,我有一个组件,它正在创建一个新事件,在组件更新时发送 export class FullScreenButton extends Component { /** * @constructor * Instanciates a new Event called resize and binds the current context to the handleClick method. */ constructor() { super(); this.h
export class FullScreenButton extends Component {
/**
* @constructor
* Instanciates a new Event called resize and binds the current context to the handleClick method.
*/
constructor() {
super();
this.handleClick = this.handleClick.bind(this);
this.event = new Event('resize')
}
/**
* If the component was updated then dispatches the resize event.
* @param {Object} prevProps - Previous state of the props.
*/
componentDidUpdate(prevProps) {
if (prevProps !== this.props) {
window.dispatchEvent(this.event);
}
}
...
}
然而,在单元测试中,当使用酶安装组件时,它向我显示了以下错误:
1) Testing FullScreenButton Component
Should alter the state and css on click:
ReferenceError: Event is not defined
我错过了什么?
提前谢谢你的帮助
编辑:我的测试如下所示:
import React from 'react';
import { expect } from 'chai';
import { mount } from 'enzyme';
import { FullScreenButton } from '../../../../../src/screens/flowConstructor/components/toolsMenu/FullScreenButton';
describe.only('Testing FullScreenButton Component', () => {
it('Should alter the state and css on click', (done) => {
const wrapper = mount(<FullScreenButton />);
wrapper.setProps({
toggleFullScreen: (fullscreen) => {
expect(fullscreen).to.equal(false);
}
});
expect(wrapper.find('button').hasClass('gof-icon-full-screen')).to.equal(true);
wrapper.find('button').simulate('click');
expect(wrapper.find('button').hasClass('gof-icon-full-windowed')).to.equal(true);
done();
});
});
从“React”导入React;
从“chai”导入{expect};
从“酶”导入{mount};
从“../../../../../src/screens/flowConstructor/components/toolsMenu/FullScreenButton”导入{FullScreenButton};
仅说明('测试FullScreenButton组件',()=>{
它('应在单击时更改状态和css',(完成)=>{
const wrapper=mount();
包装器.setProps({
toggleFullScreen:(全屏)=>{
expect(全屏).to.equal(假);
}
});
expect(wrapper.find('button').hasClass('gof-icon-full-screen')).to.equal(true);
wrapper.find('button').simulate('click');
expect(wrapper.find('button').hasClass('gof-icon-full-windowed')).to.equal(true);
完成();
});
});
更新:找到了一个有效的解决方案。
以下是更新的测试:
describe.only('Testing FullScreenButton Component', () => {
before(() => {
global.Event = class {
constructor(event) {
console.log(event);
}
};
global.window.dispatchEvent = (event) => {
console.log(event)
};
});
it('Should alter the state and css on click', (done) => {
const wrapper = mount(<FullScreenButton />);
wrapper.setProps({
toggleFullScreen: () => {},
fullScreen: false,
});
expect(wrapper.find('span').hasClass('gof-icon-full-screen')).to.equal(true);
wrapper.setProps({
fullScreen: true,
});
expect()
expect(wrapper.find('span').hasClass('gof-icon-windowed')).to.equal(true);
done();
});
});
description.only('测试全屏按钮组件',()=>{
之前(()=>{
global.Event=class{
构造函数(事件){
console.log(事件);
}
};
global.window.dispatchEvent=(事件)=>{
console.log(事件)
};
});
它('应在单击时更改状态和css',(完成)=>{
const wrapper=mount();
包装器.setProps({
切换全屏:()=>{},
全屏:假,
});
expect(wrapper.find('span').hasClass('gof-icon-full-screen')).to.equal(true);
包装器.setProps({
全屏:对,
});
expect()
expect(wrapper.find('span').hasClass('gof-icon-windowed')).to.equal(true);
完成();
});
});
您也可以在这里发布测试代码吗?看起来您没有将事件对象传递给simulate()
函数?我用测试代码编辑了我的问题,我不这么认为,我是按照文档中显示的那样做的