监视 redux 调度方法

qui*_*mmo 4 reactjs jestjs redux react-redux react-tsx

我是一个开玩笑的新手,我正在为我的 React 应用程序编写单元测试,该应用程序使用 redux 并使用 Typescript 编写。

我的容器组件包含这段代码:

const mapDispatchToProps = (dispatch: Dispatch<any>) => ({
    onSelectScenario: (selectedScenario: any) => {
        dispatch(selectScenario(selectedScenario));
    }
});
Run Code Online (Sandbox Code Playgroud)

我想编写一个单元测试,检查当我从测试 ( onSelectScenario) 调用此道具时,dispatch将使用正确的参数调用该方法。

知道如何监视这个吗dispatch

这是我的单元测试,我在其中调用 prop 方法:

it('should dispatch', () => {
    component.props().onSelectScenario('New Selected Scenario');
});
Run Code Online (Sandbox Code Playgroud)

这是测试的设置,我在其中定义提供模拟商店的容器组件:

const mockStore = configureMockStore();
let store = mockStore({
    scenarios: ['Scenario 1', 'Scenario 2']
});
let component: ShallowWrapper<any, any>;

describe('ScenarioListGroupContainer Component', () => {
    beforeEach(() => {
        component = shallow(<ScenarioListGroupContainer store={store} />);
    });
    // ...
});
Run Code Online (Sandbox Code Playgroud)

Sha*_*ker 7

如果它对任何人有帮助,这就是我正在使用的:

const spy = jest.spyOn(store, 'dispatch');
Run Code Online (Sandbox Code Playgroud)