actionCreatorFactory.test.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { actionCreatorFactory, resetAllActionCreatorTypes } from './actionCreatorFactory';
  2. interface Dummy {
  3. n: number;
  4. s: string;
  5. o: {
  6. n: number;
  7. s: string;
  8. b: boolean;
  9. };
  10. b: boolean;
  11. }
  12. const setup = (payload: Dummy) => {
  13. resetAllActionCreatorTypes();
  14. const actionCreator = actionCreatorFactory<Dummy>('dummy').create();
  15. const result = actionCreator(payload);
  16. return {
  17. actionCreator,
  18. result,
  19. };
  20. };
  21. describe('actionCreatorFactory', () => {
  22. describe('when calling create', () => {
  23. it('then it should create correct type string', () => {
  24. const payload = { n: 1, b: true, s: 'dummy', o: { n: 1, b: true, s: 'dummy' } };
  25. const { actionCreator, result } = setup(payload);
  26. expect(actionCreator.type).toEqual('dummy');
  27. expect(result.type).toEqual('dummy');
  28. });
  29. it('then it should create correct payload', () => {
  30. const payload = { n: 1, b: true, s: 'dummy', o: { n: 1, b: true, s: 'dummy' } };
  31. const { result } = setup(payload);
  32. expect(result.payload).toEqual(payload);
  33. });
  34. });
  35. describe('when calling create with existing type', () => {
  36. it('then it should throw error', () => {
  37. const payload = { n: 1, b: true, s: 'dummy', o: { n: 1, b: true, s: 'dummy' } };
  38. setup(payload);
  39. expect(() => {
  40. actionCreatorFactory<Dummy>('dummy').create();
  41. }).toThrow();
  42. });
  43. });
  44. });