我有一个历史课,看起来像:
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模拟这个历史道具?我试过传递历史对象,但是我被告知这个道具被内存路由器忽略了 .
1 回答
你总是可以在你的Jest配置中模拟出url .
我的方法通常包括在我的
package.json
中在你的情况下,我希望这是像 -
然后,您可以通过在
beforeEach()
块中包含以下内容,在每个测试的基础上更改此设置