|
|
@@ -3,40 +3,71 @@ import React from 'react';
|
|
|
|
|
|
import { stateSet } from '../actions';
|
|
|
import { masterStateUpdateTimeout } from '../constants/system';
|
|
|
-import { initialState } from '../reducer';
|
|
|
+import { GlobalState, initialState, nullPlayer } from '../reducer';
|
|
|
|
|
|
import { useMaster } from './master';
|
|
|
|
|
|
describe(useMaster.name, () => {
|
|
|
const dispatch = jest.fn();
|
|
|
|
|
|
- const TestComponent: React.FC<{ master: string; myClientName: string }> = ({
|
|
|
- master,
|
|
|
- myClientName,
|
|
|
- }) => {
|
|
|
- useMaster({ player: { ...initialState.player, master }, myClientName }, dispatch);
|
|
|
+ const TestComponent: React.FC<GlobalState> = (state) => {
|
|
|
+ useMaster(state, dispatch);
|
|
|
return null;
|
|
|
};
|
|
|
|
|
|
describe('when there is no master initially', () => {
|
|
|
+ const stateNoMaster: GlobalState = {
|
|
|
+ ...initialState,
|
|
|
+ initialised: true,
|
|
|
+ myClientName: 'my-client-name',
|
|
|
+ player: {
|
|
|
+ ...nullPlayer,
|
|
|
+ master: '',
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
it('should take control of master', () => {
|
|
|
expect.assertions(2);
|
|
|
- const { unmount } = render(<TestComponent master="" myClientName="my-client-name" />);
|
|
|
+ const { unmount } = render(<TestComponent {...stateNoMaster} />);
|
|
|
|
|
|
expect(dispatch).toHaveBeenCalledTimes(1);
|
|
|
expect(dispatch).toHaveBeenCalledWith(stateSet({ master: 'my-client-name' }));
|
|
|
|
|
|
unmount();
|
|
|
});
|
|
|
+
|
|
|
+ describe('when the state is not initialised', () => {
|
|
|
+ const stateNoMasterUninit: GlobalState = {
|
|
|
+ ...stateNoMaster,
|
|
|
+ initialised: false,
|
|
|
+ };
|
|
|
+
|
|
|
+ it('should not take control of master', () => {
|
|
|
+ expect.assertions(1);
|
|
|
+ const { unmount } = render(<TestComponent {...stateNoMasterUninit} />);
|
|
|
+
|
|
|
+ expect(dispatch).not.toHaveBeenCalled();
|
|
|
+
|
|
|
+ unmount();
|
|
|
+ });
|
|
|
+ });
|
|
|
});
|
|
|
|
|
|
describe('when the client is master', () => {
|
|
|
+ const stateMaster: GlobalState = {
|
|
|
+ ...initialState,
|
|
|
+ initialised: true,
|
|
|
+ myClientName: 'the-master-client',
|
|
|
+ player: {
|
|
|
+ ...nullPlayer,
|
|
|
+ master: 'the-master-client',
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
it('should continually refresh the server with the current state', () => {
|
|
|
expect.assertions(6);
|
|
|
const clock = jest.useFakeTimers();
|
|
|
- const { unmount } = render(
|
|
|
- <TestComponent master="the-master-client" myClientName="the-master-client" />,
|
|
|
- );
|
|
|
+ const { unmount } = render(<TestComponent {...stateMaster} />);
|
|
|
|
|
|
act(() => {
|
|
|
clock.runTimersToTime(masterStateUpdateTimeout - 1);
|
|
|
@@ -66,12 +97,20 @@ describe(useMaster.name, () => {
|
|
|
});
|
|
|
|
|
|
describe('when the client is a slave', () => {
|
|
|
+ const stateSlave: GlobalState = {
|
|
|
+ ...initialState,
|
|
|
+ initialised: true,
|
|
|
+ myClientName: 'a-slave-client',
|
|
|
+ player: {
|
|
|
+ ...nullPlayer,
|
|
|
+ master: 'the-master-client',
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
it('should not send state updates periodically', () => {
|
|
|
expect.assertions(1);
|
|
|
const clock = jest.useFakeTimers();
|
|
|
- const { unmount } = render(
|
|
|
- <TestComponent master="the-master-client" myClientName="my-client-name" />,
|
|
|
- );
|
|
|
+ const { unmount } = render(<TestComponent {...stateSlave} />);
|
|
|
|
|
|
act(() => {
|
|
|
clock.runTimersToTime(masterStateUpdateTimeout);
|