Jest + react-navigation 没有定位路线/参数
Jest + react-navigation not locating route / params
我正在尝试使用 react-navigation 为应用程序编写测试,我 运行 正在研究正确读取路由和参数的问题。
我收到一个错误
TypeError: Cannot read property 'params' of undefined
于 const [leadId] = useState(route.params.leadId);
我的组件看起来像
export default function AComponent() {
const route = useRoute();
const navigation = useNavigation();
const dispatch = useDispatch();
const [leadId] = useState(route.params.leadId);
}
我已经尝试遵循 https://callstack.github.io/react-native-testing-library/docs/react-navigation/ 但我在包装组件时收到了 Warning: React.createElement: type is invalid
。
我的测试看起来像
import React from 'react';
import { Provider } from 'react-redux';
import { NavigationContainer } from '@react-navigation/native';
import { render, fireEvent, cleanup } from 'react-native-testing-library';
import configureMockStore from 'redux-mock-store';
import AComponent from 'components/contact/AComponent';
const mockStore = configureMockStore([]);
describe('<AComponent />', () => {
let getByTestId, store;
beforeEach(() => {
store = mockStore({});
({ getByTestId } = render(
<Provider store={store}>
<AComponent />
</Provider>
));
});
});
我的模拟是
jest.mock('@react-navigation/native', () => {
return {
useNavigation: () => ({ goBack: jest.fn() }),
useRoute: jest.fn(),
};
});
我不确定我是否没有正确包装组件,或者我是否遗漏了其他东西。
任何想法或帮助将不胜感激。
谢谢。
嘿,我自己解决了这个问题,这是我的解决方案
改变
jest.mock('@react-navigation/native', () => {
return {
useNavigation: () => ({ goBack: jest.fn() }),
useRoute: jest.fn(),
};
});
到
jest.mock('@react-navigation/native', () => ({
...jest.requireActual('@react-navigation/native'),
useNavigation: () => ({ goBack: jest.fn() }),
useRoute: () => ({
params: {
<yourParamName>: '<paramValue>',
<yourParamName2>: '<paramValue2>',
etc...
}
}),
}));
在我的例子中,我将这个代码块放入我的 setup.ts 文件中,然后在 package.json 的我的 jest 配置中指向它。
例子
"setupFiles": [
"./node_modules/react-native-gesture-handler/jestSetup.js",
"./jest/setup.ts"
]
然后在测试本身
const navigation = { navigate: jest.fn() };
const { getByTestId, getByText, queryByTestId } = render(<App navigation={navigation}/>);
我正在尝试使用 react-navigation 为应用程序编写测试,我 运行 正在研究正确读取路由和参数的问题。
我收到一个错误
TypeError: Cannot read property 'params' of undefined
于 const [leadId] = useState(route.params.leadId);
我的组件看起来像
export default function AComponent() {
const route = useRoute();
const navigation = useNavigation();
const dispatch = useDispatch();
const [leadId] = useState(route.params.leadId);
}
我已经尝试遵循 https://callstack.github.io/react-native-testing-library/docs/react-navigation/ 但我在包装组件时收到了 Warning: React.createElement: type is invalid
。
我的测试看起来像
import React from 'react';
import { Provider } from 'react-redux';
import { NavigationContainer } from '@react-navigation/native';
import { render, fireEvent, cleanup } from 'react-native-testing-library';
import configureMockStore from 'redux-mock-store';
import AComponent from 'components/contact/AComponent';
const mockStore = configureMockStore([]);
describe('<AComponent />', () => {
let getByTestId, store;
beforeEach(() => {
store = mockStore({});
({ getByTestId } = render(
<Provider store={store}>
<AComponent />
</Provider>
));
});
});
我的模拟是
jest.mock('@react-navigation/native', () => {
return {
useNavigation: () => ({ goBack: jest.fn() }),
useRoute: jest.fn(),
};
});
我不确定我是否没有正确包装组件,或者我是否遗漏了其他东西。
任何想法或帮助将不胜感激。
谢谢。
嘿,我自己解决了这个问题,这是我的解决方案
改变
jest.mock('@react-navigation/native', () => {
return {
useNavigation: () => ({ goBack: jest.fn() }),
useRoute: jest.fn(),
};
});
到
jest.mock('@react-navigation/native', () => ({
...jest.requireActual('@react-navigation/native'),
useNavigation: () => ({ goBack: jest.fn() }),
useRoute: () => ({
params: {
<yourParamName>: '<paramValue>',
<yourParamName2>: '<paramValue2>',
etc...
}
}),
}));
在我的例子中,我将这个代码块放入我的 setup.ts 文件中,然后在 package.json 的我的 jest 配置中指向它。
例子
"setupFiles": [
"./node_modules/react-native-gesture-handler/jestSetup.js",
"./jest/setup.ts"
]
然后在测试本身
const navigation = { navigate: jest.fn() };
const { getByTestId, getByText, queryByTestId } = render(<App navigation={navigation}/>);