测试Redux - 测试简单的Action

优质
小牛编辑
117浏览
2023-12-01
  1. import {
  2. CounterActions,
  3. INCREMENT_COUNTER,
  4. DECREMENT_COUNTER,
  5. } from './counter';
  6. // Mock out the NgRedux class with just enough to test what we want.
  7. class MockRedux extends NgRedux<any> {
  8. constructor() {
  9. super(null);
  10. }
  11. dispatch = () => undefined;
  12. }
  13. describe('counter action creators', () => {
  14. let mockRedux: NgRedux<any>;
  15. beforeEach(() => {
  16. // Initialize mock NgRedux and create a new instance of the
  17. // ActionCreatorService to be tested.
  18. mockRedux = new MockRedux();
  19. actions = new CounterActions(mockRedux);
  20. });
  21. it('increment should dispatch INCREMENT_COUNTER action', () => {
  22. const expectedAction = {
  23. type: INCREMENT_COUNTER
  24. };
  25. spyOn(mockRedux, 'dispatch');
  26. expect(mockRedux.dispatch).toHaveBeenCalled();
  27. expect(mockRedux.dispatch).toHaveBeenCalledWith(expectedAction);
  28. });
  29. it('decrement should dispatch DECREMENT_COUNTER action', () => {
  30. const expectedAction = {
  31. type: DECREMENT_COUNTER
  32. };
  33. spyOn(mockRedux, 'dispatch');
  34. actions.decrement();
  35. expect(mockRedux.dispatch).toHaveBeenCalled();
  36. expect(mockRedux.dispatch).toHaveBeenCalledWith(expectedAction);
  37. });