|
|
@@ -1,7 +1,7 @@
|
|
|
import { act, render } from '@testing-library/react';
|
|
|
import React from 'react';
|
|
|
|
|
|
-import { stateSet } from '../actions';
|
|
|
+import { masterRetaken, stateSet } from '../actions';
|
|
|
import { masterStateUpdateTimeout } from '../constants/system';
|
|
|
import { GlobalState, initialState, nullPlayer } from '../reducer';
|
|
|
|
|
|
@@ -53,11 +53,99 @@ describe(useMaster.name, () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
+ describe('when master goes away', () => {
|
|
|
+ const stateWithMaster: GlobalState = {
|
|
|
+ ...initialState,
|
|
|
+ initialised: true,
|
|
|
+ myClientName: 'my-client-name',
|
|
|
+ clientList: [
|
|
|
+ { name: 'master-client-a', lastPing: 0 },
|
|
|
+ { name: 'my-client-name', lastPing: 0 },
|
|
|
+ { name: 'other-slave-client', lastPing: 0 },
|
|
|
+ ],
|
|
|
+ player: {
|
|
|
+ songId: 123,
|
|
|
+ playing: true,
|
|
|
+ currentTime: 17,
|
|
|
+ seekTime: -1,
|
|
|
+ master: 'master-client-a',
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
+ const stateMasterWentAway: GlobalState = {
|
|
|
+ ...stateWithMaster,
|
|
|
+ clientList: [
|
|
|
+ { name: 'my-client-name', lastPing: 0 },
|
|
|
+ { name: 'other-slave-client', lastPing: 0 },
|
|
|
+ ],
|
|
|
+ };
|
|
|
+
|
|
|
+ it('should take control of master after a delay, and pause the client', () => {
|
|
|
+ expect.assertions(2);
|
|
|
+ jest.useFakeTimers();
|
|
|
+
|
|
|
+ const { container, unmount } = render(<TestComponent {...stateWithMaster} />);
|
|
|
+
|
|
|
+ act(() => {
|
|
|
+ render(<TestComponent {...stateMasterWentAway} />, { container });
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(dispatch).not.toHaveBeenCalled();
|
|
|
+
|
|
|
+ act(() => {
|
|
|
+ jest.runAllTimers();
|
|
|
+ });
|
|
|
+
|
|
|
+ expect(dispatch).toHaveBeenCalledWith(masterRetaken());
|
|
|
+
|
|
|
+ unmount();
|
|
|
+ jest.useRealTimers();
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('and a third client takes over control', () => {
|
|
|
+ const stateMasterWentAwayAnotherTookControl: GlobalState = {
|
|
|
+ ...stateMasterWentAway,
|
|
|
+ clientList: [
|
|
|
+ { name: 'my-client-name', lastPing: 0 },
|
|
|
+ { name: 'other-slave-client', lastPing: 0 },
|
|
|
+ ],
|
|
|
+ player: {
|
|
|
+ ...stateMasterWentAway.player,
|
|
|
+ master: 'other-slave-client',
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
+ it('should not take control of master', () => {
|
|
|
+ expect.assertions(1);
|
|
|
+ jest.useFakeTimers();
|
|
|
+
|
|
|
+ const { container, unmount } = render(<TestComponent {...stateWithMaster} />);
|
|
|
+ act(() => {
|
|
|
+ render(<TestComponent {...stateMasterWentAway} />, { container });
|
|
|
+ });
|
|
|
+
|
|
|
+ setImmediate(() => {
|
|
|
+ act(() => {
|
|
|
+ render(<TestComponent {...stateMasterWentAwayAnotherTookControl} />, { container });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ jest.runAllTimers();
|
|
|
+
|
|
|
+ expect(dispatch).not.toHaveBeenCalled();
|
|
|
+
|
|
|
+ unmount();
|
|
|
+ jest.useRealTimers();
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
describe('when the client is master', () => {
|
|
|
const stateMaster: GlobalState = {
|
|
|
...initialState,
|
|
|
initialised: true,
|
|
|
myClientName: 'the-master-client',
|
|
|
+ clientList: [{ name: 'the-master-client', lastPing: 0 }],
|
|
|
player: {
|
|
|
...nullPlayer,
|
|
|
master: 'the-master-client',
|
|
|
@@ -101,6 +189,10 @@ describe(useMaster.name, () => {
|
|
|
...initialState,
|
|
|
initialised: true,
|
|
|
myClientName: 'a-slave-client',
|
|
|
+ clientList: [
|
|
|
+ { name: 'the-master-client', lastPing: 0 },
|
|
|
+ { name: 'a-slave-client', lastPing: 0 },
|
|
|
+ ],
|
|
|
player: {
|
|
|
...nullPlayer,
|
|
|
master: 'the-master-client',
|