gpt4 book ai didi

reactjs - Jest/Enzyme 中的模拟基本名称

转载 作者:行者123 更新时间:2023-12-03 13:26:58 25 4
gpt4 key购买 nike

我有一个历史课,如下所示:

import createHistory from 'history/createBrowserHistory';

export default createHistory({
basename: '/admin/',
});

当针对连接到存储并使用路由器呈现的类编写/运行任何单元测试时,我在测试中收到以下警告:

console.error node_modules/warning/warning.js:51
Warning: You are attempting to use a basename on a page whose URL path does not begin with the basename. Expected path "blank" to begin with "/admin".

示例测试如下:

import React from 'react';
import { MemoryRouter as Router } from 'react-router-dom';
import { mount } from 'enzyme';
import configureStore from 'redux-mock-store';
import TenantListContainer from '../../../src/containers/TenantList';
import TenantList from '../../../src/containers/TenantList/TenantList';

const mockStore = configureStore();
const store = mockStore({
tenants: {
tenants: ['foo'],
loading: true,
},
});


describe('TenantListContainer', () => {
it('should render the TenantList components', () => {
const wrapper = mount(
<Router>
<TenantListContainer store={store} />
</Router>
);
expect(wrapper.find(<TenantList />)).toBeTruthy();
});
});

如何使用 MemoryRouter 模拟这个历史记录?我尝试传递历史对象,但随后我被告知内存路由器会忽略此 Prop 。

最佳答案

您始终可以从 Jest 配置中模拟出 URL。

我的方法通常是将其包含在我的 package.json

就你而言,我希望这会是这样的 -

 "jest": {
"testURL": "http://some-domain.tld/admin"
}

然后,您可以通过在 beforeEach() block 中包含以下内容,在每次测试的基础上更改此设置

window.history.pushState({}, 'Foo Title', '/admin/foo');

关于reactjs - Jest/Enzyme 中的模拟基本名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47936167/

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