admin管理员组文章数量:1279241
Thats my ponent
class Foo extends React.Component {
constructor(props) {
super(props);
this.state = {
...
};
this.input = React.createRef();
}
ponentDidMount() {
const id = 'bar';
let element = document.getElementById(id);
element.addEventListener('transitionend', () => {
this.setState({ ... });
}, false);
}
...
When I set up my test like so
import React from 'react';
import { mount } from 'enzyme';
import 'jsdom-global/register';
import Foo from './';
it('renders the ponent correctly', () => {
const ponent = mount(
<Foo />
);
ponent
.unmount();
});
I get
console.error node_modules/react-dom/cjs/react-dom.development.js:16647 The above error occurred in the ponent: in Foo (created by WrapperComponent) in WrapperComponent
Consider adding an error boundary to your tree to customize error handling behavior. ● renders the ponent correctly TypeError: Cannot read property 'addEventListener' of null
I tried
ReactDOM.render(<Foo />, document.body);
or adding this
const map = {};
Window.addEventListener = jest.genMockFn().mockImpl((event, cb) => {
map[event] = cb;
});
as well as this
const map = {};
document.addEventListener = jest.fn((event, cb) => {
map[event] = cb;
})
before mounting <Foo />
in the test. But it all es back with the same error. Why is that?
Thats my ponent
class Foo extends React.Component {
constructor(props) {
super(props);
this.state = {
...
};
this.input = React.createRef();
}
ponentDidMount() {
const id = 'bar';
let element = document.getElementById(id);
element.addEventListener('transitionend', () => {
this.setState({ ... });
}, false);
}
...
When I set up my test like so
import React from 'react';
import { mount } from 'enzyme';
import 'jsdom-global/register';
import Foo from './';
it('renders the ponent correctly', () => {
const ponent = mount(
<Foo />
);
ponent
.unmount();
});
I get
console.error node_modules/react-dom/cjs/react-dom.development.js:16647 The above error occurred in the ponent: in Foo (created by WrapperComponent) in WrapperComponent
Consider adding an error boundary to your tree to customize error handling behavior. ● renders the ponent correctly TypeError: Cannot read property 'addEventListener' of null
I tried
ReactDOM.render(<Foo />, document.body);
or adding this
const map = {};
Window.addEventListener = jest.genMockFn().mockImpl((event, cb) => {
map[event] = cb;
});
as well as this
const map = {};
document.addEventListener = jest.fn((event, cb) => {
map[event] = cb;
})
before mounting <Foo />
in the test. But it all es back with the same error. Why is that?
- 1 Possible duplicate of jest + enzyme, using mount(), document.getElementById() returns null on ponent which appear after _method call – Agney Commented Feb 12, 2019 at 11:29
1 Answer
Reset to default 5One of reasons why direct DOM access is discouraged in React is because it makes testing more plicated and unpredictable.
DOM can be mocked entirely before mounting a ponent:
const elementMock = { addEventListener: jest.fn() };
jest.spyOn(document, 'getElementById').mockImplementation(() => elementMock);
A stub can be tested that is was called correctly:
expect(elementMock.addEventListener).toBeCalledWith('transitionend', expect.any(Function), false);
And event listener can be tested that it changes the state as expected:
const handler = elementMock.mock.calls[0][1];
handler();
...
本文标签: javascriptHow to mock eventListener when set in componentDidMount()Stack Overflow
版权声明:本文标题:javascript - How to mock eventListener when set in componentDidMount() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741245587a2364810.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论