|
@@ -1,8 +1,7 @@
|
|
|
import { act, render, RenderResult } from '@testing-library/react';
|
|
import { act, render, RenderResult } from '@testing-library/react';
|
|
|
import React from 'react';
|
|
import React from 'react';
|
|
|
|
|
|
|
|
-import { masterSet, stateSet } from '../actions';
|
|
|
|
|
-import { masterStateUpdateTimeout } from '../constants/system';
|
|
|
|
|
|
|
+import { masterSet } from '../actions';
|
|
|
import { DispatchContext, StateContext } from '../context/state';
|
|
import { DispatchContext, StateContext } from '../context/state';
|
|
|
import { GlobalState, initialState, nullPlayer } from '../reducer';
|
|
import { GlobalState, initialState, nullPlayer } from '../reducer';
|
|
|
|
|
|
|
@@ -46,7 +45,7 @@ describe(useMaster.name, () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
expect(dispatch).toHaveBeenCalledTimes(1);
|
|
expect(dispatch).toHaveBeenCalledTimes(1);
|
|
|
- expect(dispatch).toHaveBeenCalledWith(stateSet({ master: 'my-client-name' }));
|
|
|
|
|
|
|
+ expect(dispatch).toHaveBeenCalledWith(masterSet('my-client-name'));
|
|
|
|
|
|
|
|
unmount();
|
|
unmount();
|
|
|
jest.useRealTimers();
|
|
jest.useRealTimers();
|
|
@@ -143,82 +142,4 @@ describe(useMaster.name, () => {
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
- describe('when the client is master', () => {
|
|
|
|
|
- const stateMaster: GlobalState = {
|
|
|
|
|
- ...initialState,
|
|
|
|
|
- myClientName: 'the-master-client',
|
|
|
|
|
- clientList: [{ name: 'the-master-client', lastPing: 0 }],
|
|
|
|
|
- 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 } = setup(stateMaster);
|
|
|
|
|
- act(() => {
|
|
|
|
|
- clock.runOnlyPendingTimers();
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- dispatch.mockClear();
|
|
|
|
|
-
|
|
|
|
|
- act(() => {
|
|
|
|
|
- clock.runTimersToTime(masterStateUpdateTimeout - 1);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- expect(dispatch).toHaveBeenCalledTimes(0);
|
|
|
|
|
- act(() => {
|
|
|
|
|
- clock.runTimersToTime(1);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- expect(dispatch).toHaveBeenCalledTimes(1);
|
|
|
|
|
- expect(dispatch).toHaveBeenCalledWith(stateSet());
|
|
|
|
|
-
|
|
|
|
|
- dispatch.mockClear();
|
|
|
|
|
- expect(dispatch).toHaveBeenCalledTimes(0);
|
|
|
|
|
-
|
|
|
|
|
- act(() => {
|
|
|
|
|
- clock.runTimersToTime(masterStateUpdateTimeout);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- expect(dispatch).toHaveBeenCalledTimes(1);
|
|
|
|
|
- expect(dispatch).toHaveBeenCalledWith(stateSet());
|
|
|
|
|
-
|
|
|
|
|
- unmount();
|
|
|
|
|
- jest.useRealTimers();
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- describe('when the client is a slave', () => {
|
|
|
|
|
- const stateSlave: GlobalState = {
|
|
|
|
|
- ...initialState,
|
|
|
|
|
- myClientName: 'a-slave-client',
|
|
|
|
|
- clientList: [
|
|
|
|
|
- { name: 'the-master-client', lastPing: 0 },
|
|
|
|
|
- { name: 'a-slave-client', lastPing: 0 },
|
|
|
|
|
- ],
|
|
|
|
|
- player: {
|
|
|
|
|
- ...nullPlayer,
|
|
|
|
|
- master: 'the-master-client',
|
|
|
|
|
- },
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- it('should not send state updates periodically', () => {
|
|
|
|
|
- expect.assertions(1);
|
|
|
|
|
- const clock = jest.useFakeTimers();
|
|
|
|
|
- const { unmount } = setup(stateSlave);
|
|
|
|
|
-
|
|
|
|
|
- act(() => {
|
|
|
|
|
- clock.runTimersToTime(masterStateUpdateTimeout);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- expect(dispatch).not.toHaveBeenCalled();
|
|
|
|
|
-
|
|
|
|
|
- unmount();
|
|
|
|
|
- jest.useRealTimers();
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
});
|
|
});
|