gpt4 book ai didi

javascript - 单元测试 : How to mock document. getElementById() react ?

转载 作者:行者123 更新时间:2023-12-05 05:17:27 26 4
gpt4 key购买 nike

我想 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com