如何使用另一个类示例模拟从文件导出的类示例

ffdz8vbo  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(287)

我有这个档案

// src/history
import { createBrowserHistory } from 'history'
const history = createBrowserHistory();
export default history;
``` `history` 是一个例子 `BrowserHistory` 班级。它在实际代码中使用:

// src/SampleCode.js
import history from '../../history';
export default function SampleCode = () => {
const onClick = () => history.push(/login);
return (<Button onCLick={onClick}>Click)
}

我想嘲笑一下 `history` 使用另一个类的示例 `MemoryHistory` . 大概是这样的:

// src/sample.test.js
it('should redirect to /login', () => {
const mockHistory = createMemoryHistory();
const historyPush = jest.spyOn(mockHistory, 'push').mockResolvedValue({});
jest.mock('./history', () => mockHistory);
// Click button here
await waitFor(() => expect(historyPush).toBeCalledWith('/login'));
}

但这种方法不起作用: `historyPush` 根本没人打电话
gmol1639

gmol16391#

使用jest.domock(modulename、factory、options)确保模拟 ./history 模块,然后再导入 component .
例如 sample.jsx :

import React from 'react';
import history from './history';

export default function SampleCode() {
  const onClick = () => history.push(`/login`);
  return <button onClick={onClick}>Click</button>;
}
``` `history.js` :

import { createBrowserHistory } from 'history';
const history = createBrowserHistory();
export default history;
``` sample.test.jsx :

import { createMemoryHistory } from 'history';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

describe('68311534', () => {
  beforeEach(() => {
    jest.resetModules();
  });
  it('should pass', () => {
    const memoryHistory = createMemoryHistory();
    const pushSpy = jest.spyOn(memoryHistory, 'push');
    jest.doMock('./history', () => memoryHistory);
    const { default: Sample } = require('./sample');
    render(<Sample />);
    const button = screen.getByText('Click');
    userEvent.click(button);
    expect(pushSpy).toBeCalledWith('/login');
  });
});

测试结果:

PASS  examples/68311534/sample.test.jsx (15.495 s)
  68311534
    ✓ should pass (11006 ms)

------------|---------|----------|---------|---------|-------------------
File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------|---------|----------|---------|---------|-------------------
All files   |     100 |      100 |     100 |     100 |                   
 sample.jsx |     100 |      100 |     100 |     100 |                   
------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        17.53 s

相关问题