作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想 Jest 测试下面的代码。
有人知道如何模拟 document.getElementById() 吗?
if (document.getElementById('estateList')) {
render(
<Provider store={store}>
<EstateList />
</Provider>,
window.document.getElementById('estateList')
);
}
if (document.getElementById('articleList')) {
render(
<Provider store={store}>
<ArticleList />
</Provider>,
window.document.getElementById('articleList')
);
}
if (document.getElementById('articleDetail')) {
render(
<Provider store={store}>
<ArticleDetail />
</Provider>,
window.document.getElementById('articleDetail')
);
}
我想我可以将 render 放在一个函数中,比如:
function estateList() {
render(
<Provider store={store}>
<EstateList />
</Provider>,
window.document.getElementById('estateList')
);
}
然后简单地测试 estateList(),而不是模拟 document.getElementById(),但是有没有模拟 document.getElementById()?
最佳答案
使用 jest.fn(implementation)创建模拟的 getElementById
方法并替换 document
上的方法。
例如
index.tsx
:
import React from 'react';
import { Provider } from 'react-redux';
import { render } from 'react-dom';
import { createStore } from 'redux';
export const store = createStore(() => 0);
export const EstateList = () => <div>EstateList</div>;
export const ArticleList = () => <div>ArticleList</div>;
export const ArticleDetail = () => <div>ArticleDetail</div>;
function bootstrap() {
if (document.getElementById('estateList')) {
render(
<Provider store={store}>
<EstateList />
</Provider>,
window.document.getElementById('estateList'),
);
}
if (document.getElementById('articleList')) {
render(
<Provider store={store}>
<ArticleList />
</Provider>,
window.document.getElementById('articleList'),
);
}
if (document.getElementById('articleDetail')) {
render(
<Provider store={store}>
<ArticleDetail />
</Provider>,
window.document.getElementById('articleDetail'),
);
}
}
export { bootstrap };
index.test.tsx
:
import React from 'react';
import { render } from 'react-dom';
import { bootstrap, store, EstateList, ArticleList, ArticleDetail } from './';
import { Provider } from 'react-redux';
jest.mock('react-dom');
const mockedRender = render as jest.Mocked<typeof render>;
describe('49146453', () => {
let oGetElementById;
beforeAll(() => {
oGetElementById = document.getElementById;
});
afterAll(() => {
document.getElementById = oGetElementById;
});
it('should render estatelist', () => {
document.getElementById = jest.fn().mockImplementation((id) => (id === 'estateList' ? 'estateList-dom' : null));
bootstrap();
expect(mockedRender).toBeCalledWith(
<Provider store={store}>
<EstateList />
</Provider>,
'estateList-dom',
);
});
it('should render articleList', () => {
document.getElementById = jest.fn().mockImplementation((id) => (id === 'articleList' ? 'articleList-dom' : null));
bootstrap();
expect(mockedRender).toBeCalledWith(
<Provider store={store}>
<ArticleList />
</Provider>,
'articleList-dom',
);
});
it('should render articleDetail', () => {
document.getElementById = jest.fn().mockImplementation((id) => (id === 'articleDetail' ? 'articleDetail-dom' : null));
bootstrap();
expect(mockedRender).toBeCalledWith(
<Provider store={store}>
<ArticleDetail />
</Provider>,
'articleDetail-dom',
);
});
});
带有覆盖率报告的单元测试结果:
PASS src/stackoverflow/49146453/index.test.tsx
49146453
✓ should render estatelist (6ms)
✓ should render articleList (1ms)
✓ should render articleDetail (1ms)
-----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files | 84.21 | 100 | 40 | 100 | |
index.tsx | 84.21 | 100 | 40 | 100 | |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 5.618s, estimated 11s
关于jest.js
及相关包的版本,查看源码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/49146453
关于javascript - 单元测试 : How to mock document. getElementById() react ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49146453/
我是一名优秀的程序员,十分优秀!