actionCreatorFactory.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {
  2. actionCreatorFactory,
  3. resetAllActionCreatorTypes,
  4. noPayloadActionCreatorFactory,
  5. } from './actionCreatorFactory';
  6. interface Dummy {
  7. n: number;
  8. s: string;
  9. o: {
  10. n: number;
  11. s: string;
  12. b: boolean;
  13. };
  14. b: boolean;
  15. }
  16. const setup = (payload?: Dummy) => {
  17. resetAllActionCreatorTypes();
  18. const actionCreator = actionCreatorFactory<Dummy>('dummy').create();
  19. const noPayloadactionCreator = noPayloadActionCreatorFactory('NoPayload').create();
  20. const result = actionCreator(payload);
  21. const noPayloadResult = noPayloadactionCreator();
  22. return { actionCreator, noPayloadactionCreator, result, noPayloadResult };
  23. };
  24. describe('actionCreatorFactory', () => {
  25. describe('when calling create', () => {
  26. it('then it should create correct type string', () => {
  27. const payload = { n: 1, b: true, s: 'dummy', o: { n: 1, b: true, s: 'dummy' } };
  28. const { actionCreator, result } = setup(payload);
  29. expect(actionCreator.type).toEqual('dummy');
  30. expect(result.type).toEqual('dummy');
  31. });
  32. it('then it should create correct payload', () => {
  33. const payload = { n: 1, b: true, s: 'dummy', o: { n: 1, b: true, s: 'dummy' } };
  34. const { result } = setup(payload);
  35. expect(result.payload).toEqual(payload);
  36. });
  37. });
  38. describe('when calling create with existing type', () => {
  39. it('then it should throw error', () => {
  40. const payload = { n: 1, b: true, s: 'dummy', o: { n: 1, b: true, s: 'dummy' } };
  41. setup(payload);
  42. expect(() => {
  43. noPayloadActionCreatorFactory('DuMmY').create();
  44. }).toThrow();
  45. });
  46. });
  47. });
  48. describe('noPayloadActionCreatorFactory', () => {
  49. describe('when calling create', () => {
  50. it('then it should create correct type string', () => {
  51. const { noPayloadResult, noPayloadactionCreator } = setup();
  52. expect(noPayloadactionCreator.type).toEqual('NoPayload');
  53. expect(noPayloadResult.type).toEqual('NoPayload');
  54. });
  55. it('then it should create correct payload', () => {
  56. const { noPayloadResult } = setup();
  57. expect(noPayloadResult.payload).toBeUndefined();
  58. });
  59. });
  60. describe('when calling create with existing type', () => {
  61. it('then it should throw error', () => {
  62. setup();
  63. expect(() => {
  64. actionCreatorFactory<Dummy>('nOpAyLoAd').create();
  65. }).toThrow();
  66. });
  67. });
  68. });