0

我在下面有以下简单示例,我尝试更改 withRouter 的模拟实现。不幸的是,这似乎没有按预期工作。第二个模拟实现根本不会被调用。

零件

import { withRouter } from 'next/router';

const PaymentChange = ({ router }) => {
  console.log(router);
  return <></>;
};

export default withRouter(PaymentChange);

测试文件

import { render } from '@testing-library/react';
import { withRouter } from 'next/router';
import PaymentChange from './PaymentChange';
import '@testing-library/jest-dom/extend-expect';

jest.mock('next/router', () => ({
  withRouter: jest.fn().mockImplementation((component) => {
    component.defaultProps = {
      ...component.defaultProps,
      router: { pathname: 'mocked-path' }
    };
    return component;
  })
}));

describe('<PaymentChange />', () => {
  it('Should not render modal when retargetModal', () => {
    withRouter.mockImplementation((component) => {
      component.defaultProps = {
        ...component.defaultProps,
        router: { pathname: 'mocked-pathasdasd' }
      };
      return component;
    });

    render(<PaymentChange />);
  });
});

输出

{ pathname: 'mocked-path' }
4

0 回答 0