测试Redux - 测试简单的Action
优质
小牛编辑
131浏览
2023-12-01
import {
CounterActions,
INCREMENT_COUNTER,
DECREMENT_COUNTER,
} from './counter';
// Mock out the NgRedux class with just enough to test what we want.
class MockRedux extends NgRedux<any> {
constructor() {
super(null);
}
dispatch = () => undefined;
}
describe('counter action creators', () => {
let mockRedux: NgRedux<any>;
beforeEach(() => {
// Initialize mock NgRedux and create a new instance of the
// ActionCreatorService to be tested.
mockRedux = new MockRedux();
actions = new CounterActions(mockRedux);
});
it('increment should dispatch INCREMENT_COUNTER action', () => {
const expectedAction = {
type: INCREMENT_COUNTER
};
spyOn(mockRedux, 'dispatch');
expect(mockRedux.dispatch).toHaveBeenCalled();
expect(mockRedux.dispatch).toHaveBeenCalledWith(expectedAction);
});
it('decrement should dispatch DECREMENT_COUNTER action', () => {
const expectedAction = {
type: DECREMENT_COUNTER
};
spyOn(mockRedux, 'dispatch');
actions.decrement();
expect(mockRedux.dispatch).toHaveBeenCalled();
expect(mockRedux.dispatch).toHaveBeenCalledWith(expectedAction);
});