我有一个简单的组件,它使用 React Router 的 useLocation 钩子。
// App.jsx
import React from 'react';
import { useLocation } from 'react-router-dom';
function App() {
const location = useLocation();
const pathName = location.pathname;
useEffect(() => {
// Use pathName here
}, [pathName]);
}
Run Code Online (Sandbox Code Playgroud)
// App.test.js
import App from './App';
describe('App component', () => {
it('Renders correctly', () => {
const wrapper = shallow(<App />);
expect(wrapper).toMatchSnapshot();
});
});
Run Code Online (Sandbox Code Playgroud)
// 更新 App.test.js(使用 jest mock)
import App from './App';
describe('App component', () => {
jest.mock('react-router-dom', () => …Run Code Online (Sandbox Code Playgroud)