简体   繁体   English

Jest TypeScript mocking store.dispatch 导致调用 0

[英]Jest TypeScript mocking store.dispatch results in 0 calls being made

Problem问题

I'm new to testing.我是测试新手。 I've actually gone through all of the docs on jest and they don't cover typescript specific cases and I'm researching what I can.实际上,我已经浏览了所有有关开玩笑的文档,但它们并未涵盖 typescript 个具体案例,我正在尽我所能进行研究。 I'm trying to write a simple test to see if my code was triggered.我正在尝试编写一个简单的测试来查看我的代码是否被触发。

jest.mock('moment', () => {
    return {
        toLocaleString: jest.fn().mockReturnValue('test')
    }
});
jest.mock('@/store/index')
jest.mock('vuex'), () => {
    return {
        store: {
            dispatch: jest.fn()
        }
    }
};
jest.mock('@/services/JanusRequestService')
jest.mock('../../src/axiosConfig')
jest.mock('../../src/router', () => {
    return {
        test: jest.fn()
    }
})
jest.mock('../../src/interfaces/Request')

describe('copyRequestToClipboard', () => {
    let RequestMock = {
        Id: '1',
        createdBy: 'string',
        CreatedOnUtc: new Date(),
        modifiedOn: 'string',
        version: 2,
        workflowId: 'string',
        Status: 'string'
    }
    //@ts-ignore
    let writeTextMock = window.__defineGetter__('navigator', function () {
        return {
            clipboard: {
                writeText: jest.fn(x => x)
            }
        }
    })
    it('should call navigator.clipboard.writeText() correctly', () => {
        globalMixins.methods.copyRequestIdToClipboard(RequestMock)
        expect(writeTextMock).toHaveBeenCalled
    });

    it('should call triggerSnackBar correctly', () => {
        globalMixins.methods.triggerSnackBar = jest.fn()
        globalMixins.methods.copyRequestIdToClipboard(RequestMock)
        expect(globalMixins.methods.triggerSnackBar).toHaveBeenCalled
        expect(globalMixins.methods.triggerSnackBar).toHaveBeenCalledWith('Copied to Clipboard!', '')
    });
});
describe('triggerSnackBar', () => {
    let RequestMock: IRequest
    function constructData() {
        return {
            Id: '1',
            createdBy: 'string',
            CreatedOnUtc: new Date(),
            modifiedOn: 'string',
            version: 2,
            workflowId: 'string',
            Status: 'string'
        }
    }
    beforeEach(() => {
        RequestMock = constructData()
        let dispatchMock = store.dispatch = jest.fn();
    })

    it('should successfully call on dispatch 3 times', () => {
        globalMixins.methods.triggerSnackBar('test', 'green')
        expect(store.dispatch).toBeCalledTimes(3)
    });
    it('should have called with correct params', () => {
        globalMixins.methods.triggerSnackBar('test', 'green')
        expect(store.dispatch).toBeCalledWith({ text: 'test' })
    })
});

The error I get is我得到的错误是在此处输入图像描述

I'm trying to understand what it is I'm doing wrong.我试图了解我做错了什么。 I'm also making notes of how I need to test and what patterns to use.我还记录了我需要如何测试以及使用什么模式。

I found the answer..我找到了答案..

describe('triggerSnackBar', () => {
    let spy: any
    beforeEach(() => {
        spy = jest.spyOn(store, 'dispatch');
    })
    afterEach(() => {
        jest.clearAllMocks()
    })
    it('should successfully call on dispatch 3 times', () => {
        globalMixins.methods.triggerSnackBar('test', 'green')
        expect(store.dispatch).toBeCalledTimes(3)
    });
    it('should have called with correct params', () => {
        globalMixins.methods.triggerSnackBar('test', 'green')
        expect(store.dispatch).nthCalledWith(1, 'triggerSnackbarMessage', { text: 'test' })
        expect(store.dispatch).nthCalledWith(2, 'triggerSnackbarColor', { color: 'green' })
        expect(store.dispatch).nthCalledWith(3, 'triggerSnackbar')
    })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM