reducer.spec.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {
  2. ActionClientConnected,
  3. ActionClientDisconnected,
  4. ActionStateSetLocal,
  5. ActionStateSetRemote,
  6. ActionTypeLocal,
  7. ActionTypeRemote,
  8. } from '../actions';
  9. import { composedGlobalReducer, globalReducer, initialState } from './reducer';
  10. describe(globalReducer.name, () => {
  11. describe(ActionTypeRemote.StateSet, () => {
  12. const action: ActionStateSetRemote = {
  13. type: ActionTypeRemote.StateSet,
  14. payload: {
  15. songId: 123,
  16. playing: true,
  17. playTimeSeconds: 75,
  18. currentClient: 'some-client',
  19. },
  20. };
  21. it('should set the player state', () => {
  22. expect.assertions(1);
  23. const result = globalReducer(initialState, action);
  24. expect(result.player).toStrictEqual({
  25. songId: 123,
  26. playing: true,
  27. playTimeSeconds: 75,
  28. currentClient: 'some-client',
  29. });
  30. });
  31. });
  32. describe(ActionTypeLocal.StateSet, () => {
  33. const action: ActionStateSetLocal = {
  34. type: ActionTypeLocal.StateSet,
  35. payload: {
  36. songId: 123,
  37. playing: true,
  38. playTimeSeconds: 75,
  39. currentClient: 'some-client',
  40. },
  41. };
  42. it('should set the player state', () => {
  43. expect.assertions(1);
  44. const result = globalReducer(initialState, action);
  45. expect(result.player).toStrictEqual({
  46. songId: 123,
  47. playing: true,
  48. playTimeSeconds: 75,
  49. currentClient: 'some-client',
  50. });
  51. });
  52. });
  53. const actionClientConnected: ActionClientConnected = {
  54. type: ActionTypeRemote.ClientConnected,
  55. payload: ['client1', 'client2'],
  56. };
  57. const actionClientDisconnected: ActionClientDisconnected = {
  58. type: ActionTypeRemote.ClientDisconnected,
  59. payload: ['client1'],
  60. };
  61. describe.each`
  62. actionType | action | expectedClientList
  63. ${ActionTypeRemote.ClientConnected} | ${actionClientConnected} | ${['client1', 'client2']}
  64. ${ActionTypeRemote.ClientDisconnected} | ${actionClientDisconnected} | ${['client1']}
  65. `('$actionType', ({ action, expectedClientList }) => {
  66. it('should update the client list', () => {
  67. expect.assertions(1);
  68. const result = globalReducer(initialState, action);
  69. expect(result.clientList).toStrictEqual(expectedClientList);
  70. });
  71. });
  72. });
  73. describe(composedGlobalReducer.name, () => {
  74. it('should set the lastAction prop', () => {
  75. expect.assertions(1);
  76. const action: ActionStateSetRemote = {
  77. type: ActionTypeRemote.StateSet,
  78. payload: null,
  79. };
  80. const result = composedGlobalReducer(initialState, action);
  81. expect(result.lastAction).toBe(action);
  82. });
  83. });