selectors.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { ActionTypeRemote, stateSet } from './actions';
  2. import { GlobalState, initialState } from './reducer';
  3. import { isFromOurselves, isMaster, willBeMaster } from './selectors';
  4. describe(isMaster.name, () => {
  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(isFromOurselves.name, () => {
  40. describe('when an action was dispatched from the current client', () => {
  41. it('should return true', () => {
  42. expect.assertions(1);
  43. expect(
  44. isFromOurselves(
  45. { myClientName: 'my-client-name' },
  46. {
  47. type: ActionTypeRemote.StateSet,
  48. fromClient: 'my-client-name',
  49. payload: {} as GlobalState,
  50. },
  51. ),
  52. ).toBe(true);
  53. });
  54. });
  55. describe('when an action was dispatched from a different client', () => {
  56. it('should return false', () => {
  57. expect.assertions(1);
  58. expect(
  59. isFromOurselves(
  60. { myClientName: 'my-client-name' },
  61. {
  62. type: ActionTypeRemote.StateSet,
  63. fromClient: 'some-other-client-name',
  64. payload: {} as GlobalState,
  65. },
  66. ),
  67. ).toBe(false);
  68. });
  69. });
  70. describe('when an action was not dispatched from a client', () => {
  71. it('should return false', () => {
  72. expect.assertions(1);
  73. expect(
  74. isFromOurselves(
  75. { myClientName: 'my-client-name' },
  76. {
  77. type: ActionTypeRemote.StateSet,
  78. fromClient: null,
  79. payload: {} as GlobalState,
  80. },
  81. ),
  82. ).toBe(false);
  83. });
  84. });
  85. });
  86. describe(willBeMaster.name, () => {
  87. describe('when the action will cause the current client to be master', () => {
  88. it('should return true', () => {
  89. expect.assertions(1);
  90. expect(
  91. willBeMaster({ myClientName: 'a-slave-client' }, stateSet({ master: 'a-slave-client' })),
  92. ).toBe(true);
  93. });
  94. });
  95. describe('when the action will not change the master', () => {
  96. it('should return false', () => {
  97. expect.assertions(1);
  98. expect(willBeMaster({ myClientName: 'a-slave-client' }, stateSet({ seekTime: 123 }))).toBe(
  99. false,
  100. );
  101. });
  102. describe('but the current client is already master', () => {
  103. it('should still return false', () => {
  104. expect.assertions(1);
  105. expect(
  106. willBeMaster(
  107. { myClientName: 'a-client', player: { ...initialState.player, master: 'a-client' } },
  108. stateSet({ seekTime: 123 }),
  109. ),
  110. ).toBe(false);
  111. });
  112. });
  113. });
  114. });