selectors.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { ActionTypeRemote, stateSet } from './actions';
  2. import { GlobalState, initialState } from './reducer';
  3. import { isActiveClient, isFromOurselves, isMaster, willBeMaster } from './selectors';
  4. describe('isMaster', () => {
  5. describe('when the master player is the current client', () => {
  6. it('should return true', () => {
  7. expect.assertions(1);
  8. expect(
  9. isMaster({
  10. player: { ...initialState.player, master: 'my-client-name' },
  11. myClientName: 'my-client-name',
  12. }),
  13. ).toBe(true);
  14. });
  15. });
  16. describe('when the master player is not the current client', () => {
  17. it('should return false', () => {
  18. expect.assertions(1);
  19. expect(
  20. isMaster({
  21. player: { ...initialState.player, master: 'other-client-name' },
  22. myClientName: 'my-client-name',
  23. }),
  24. ).toBe(false);
  25. });
  26. });
  27. describe('when there is no master player', () => {
  28. it('should return false', () => {
  29. expect.assertions(1);
  30. expect(
  31. isMaster({
  32. player: { ...initialState.player, master: '' },
  33. myClientName: 'my-client-name',
  34. }),
  35. ).toBe(false);
  36. });
  37. });
  38. });
  39. describe('isActiveClient', () => {
  40. describe('when the client is master', () => {
  41. it('should return true', () => {
  42. expect.assertions(1);
  43. expect(
  44. isActiveClient({
  45. player: { ...initialState.player, master: 'my-client-name', activeClients: [] },
  46. myClientName: 'my-client-name',
  47. }),
  48. ).toBe(true);
  49. });
  50. });
  51. describe('when the client is a slave', () => {
  52. describe('when the client is in the active clients list', () => {
  53. it('should return true', () => {
  54. expect.assertions(1);
  55. expect(
  56. isActiveClient({
  57. player: {
  58. ...initialState.player,
  59. master: 'some-other-client',
  60. activeClients: ['my-client-name'],
  61. },
  62. myClientName: 'my-client-name',
  63. }),
  64. ).toBe(true);
  65. });
  66. });
  67. describe('when the client is not in the active clients list', () => {
  68. it('should return false', () => {
  69. expect.assertions(1);
  70. expect(
  71. isActiveClient({
  72. player: {
  73. ...initialState.player,
  74. master: 'some-other-client',
  75. activeClients: ['different-client-name'],
  76. },
  77. myClientName: 'my-client-name',
  78. }),
  79. ).toBe(false);
  80. });
  81. });
  82. });
  83. });
  84. describe('isFromOurselves', () => {
  85. describe('when an action was dispatched from the current client', () => {
  86. it('should return true', () => {
  87. expect.assertions(1);
  88. expect(
  89. isFromOurselves(
  90. { myClientName: 'my-client-name' },
  91. {
  92. type: ActionTypeRemote.StateSet,
  93. fromClient: 'my-client-name',
  94. payload: {} as GlobalState,
  95. },
  96. ),
  97. ).toBe(true);
  98. });
  99. });
  100. describe('when an action was dispatched from a different client', () => {
  101. it('should return false', () => {
  102. expect.assertions(1);
  103. expect(
  104. isFromOurselves(
  105. { myClientName: 'my-client-name' },
  106. {
  107. type: ActionTypeRemote.StateSet,
  108. fromClient: 'some-other-client-name',
  109. payload: {} as GlobalState,
  110. },
  111. ),
  112. ).toBe(false);
  113. });
  114. });
  115. describe('when an action was not dispatched from a client', () => {
  116. it('should return false', () => {
  117. expect.assertions(1);
  118. expect(
  119. isFromOurselves(
  120. { myClientName: 'my-client-name' },
  121. {
  122. type: ActionTypeRemote.StateSet,
  123. fromClient: null,
  124. payload: {} as GlobalState,
  125. },
  126. ),
  127. ).toBe(false);
  128. });
  129. });
  130. });
  131. describe('willBeMaster', () => {
  132. describe('when the action will cause the current client to be master', () => {
  133. it('should return true', () => {
  134. expect.assertions(1);
  135. expect(
  136. willBeMaster({ myClientName: 'a-slave-client' }, stateSet({ master: 'a-slave-client' })),
  137. ).toBe(true);
  138. });
  139. });
  140. describe('when the action will not change the master', () => {
  141. it('should return false', () => {
  142. expect.assertions(1);
  143. expect(willBeMaster({ myClientName: 'a-slave-client' }, stateSet({ seekTime: 123 }))).toBe(
  144. false,
  145. );
  146. });
  147. describe('but the current client is already master', () => {
  148. it('should still return false', () => {
  149. expect.assertions(1);
  150. expect(
  151. willBeMaster(
  152. { myClientName: 'a-client', player: { ...initialState.player, master: 'a-client' } },
  153. stateSet({ seekTime: 123 }),
  154. ),
  155. ).toBe(false);
  156. });
  157. });
  158. });
  159. });