相关疑难解决方法(0)

如何在玩笑中模拟 useHistory 钩子?

我在带有打字稿的 react router v5.1.2 中使用 UseHistory 钩子吗?运行单元测试时,我遇到了问题。

类型错误:无法读取未定义的属性“历史”。

import { mount } from 'enzyme';
import React from 'react';
import {Action} from 'history';
import * as router from 'react-router';
import { QuestionContainer } from './QuestionsContainer';

describe('My questions container', () => {
    beforeEach(() => {
        const historyHistory= {
            replace: jest.fn(),
            length: 0,
            location: { 
                pathname: '',
                search: '',
                state: '',
                hash: ''
            },
            action: 'REPLACE' as Action,
            push: jest.fn(),
            go: jest.fn(),
            goBack: jest.fn(),
            goForward: jest.fn(),
            block: jest.fn(),
            listen: jest.fn(),
            createHref: jest.fn()
        };//fake …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs jestjs react-router enzyme

48
推荐指数
6
解决办法
3万
查看次数

标签 统计

enzyme ×1

jestjs ×1

react-router ×1

reactjs ×1

typescript ×1