diff --git a/package.json b/package.json index 20f7c4c..635a8b8 100644 --- a/package.json +++ b/package.json @@ -49,8 +49,8 @@ "build": "react-scripts build", "eject": "react-scripts eject", "test": "react-scripts test", - "test:coverage": "yarn test --coverage --watchAll=false --collectCoverageFrom=src/components/**/* --collectCoverageFrom=src/views/**/*", - "test:coveralls": "yarn test --coverage --watchAll=false --collectCoverageFrom=src/components/**/* --collectCoverageFrom=src/views/**/* --coverageReporters=text-lcov | coveralls", + "test:coverage": "yarn test --coverage --watchAll=false --collectCoverageFrom=src/components/**/* --collectCoverageFrom=src/views/**/* --collectCoverageFrom=src/util.js", + "test:coveralls": "yarn test --coverage --watchAll=false --collectCoverageFrom=src/components/**/* --collectCoverageFrom=src/views/**/* --collectCoverageFrom=src/util.js --coverageReporters=text-lcov | coveralls", "cypress:open": "cypress open" }, "eslintConfig": { diff --git a/src/__tests__/components/ItemTimer.test.js b/src/__tests__/components/ItemTimer.test.js index 00359e2..1c08ccb 100644 --- a/src/__tests__/components/ItemTimer.test.js +++ b/src/__tests__/components/ItemTimer.test.js @@ -4,8 +4,10 @@ import ItemTimer from '../../components/ItemTimer' jest.mock('../../util', () => { const moment = require('moment') const mockCreateDuration = () => moment.duration({ minutes: 60, seconds: 0 }) + const mockItemTimerLogic = () => {} return { - createDuration: mockCreateDuration + createDuration: mockCreateDuration, + itemTimerLogic: mockItemTimerLogic } }) diff --git a/src/__tests__/components/__snapshots__/ItemTimer.test.js.snap b/src/__tests__/components/__snapshots__/ItemTimer.test.js.snap index 1e525b5..ecb273a 100644 --- a/src/__tests__/components/__snapshots__/ItemTimer.test.js.snap +++ b/src/__tests__/components/__snapshots__/ItemTimer.test.js.snap @@ -10,9 +10,7 @@ exports[` Should render the component. 1`] = ` > Item Timer - - 0 minutes 0 seconds - + `; diff --git a/src/__tests__/util.test.js b/src/__tests__/util.test.js new file mode 100644 index 0000000..1f29084 --- /dev/null +++ b/src/__tests__/util.test.js @@ -0,0 +1,46 @@ +import { + generateItems, + generatePlanets, + loadState, + saveState, + createDuration, + createETA, + generateContracts +} from '../util' + +describe('Utilities', () => { + it('Should test generateItems().', () => { + expect( + generateItems([ + { location: 50, name: 'Mock Planet 1' }, + { location: 100, name: 'Mock Planet 2' } + ]).length + ).toBe(5) + }) + + it('Should test generatePlanets().', () => { + expect(generatePlanets().length).toBe(3) + }) + + it('Should test loadState().', () => { + expect(loadState()).toBe(undefined) + }) + + it('Should test saveState().', () => { + expect(saveState({ mockedState: true })).toBe(undefined) + }) + + it('Should test createDuration().', () => { + expect(typeof createDuration()).toBe('object') + }) + + it('Should test createETA().', () => { + expect(typeof createETA({ value: 0 }, { location: { value: 50 } })).toBe( + 'object' + ) + }) + + it('Should test generateContracts().', () => { + expect(generateContracts().length).toBe(5) + }) +}) diff --git a/src/components/ItemTimer.js b/src/components/ItemTimer.js index 56cb75a..2f897a8 100644 --- a/src/components/ItemTimer.js +++ b/src/components/ItemTimer.js @@ -6,36 +6,24 @@ import { clearItems, refreshItems } from '../redux/actions/world' -import { createDuration } from '../util' +import { itemTimerLogic } from '../util' import { Box, Heading } from 'grommet' const ItemTimer = ({ handleTimerStarted, handleTimerStopped, world }) => { - const { isTimerRunning } = world - - let duration = createDuration() - - const [timeLeft, setTimeLeft] = useState( - `${duration.minutes()} minutes ${duration.seconds()} seconds` + const [timeLeft, setTimeLeft] = useState(null) + + useEffect( + () => + itemTimerLogic( + world, + setTimeLeft, + handleTimerStarted, + handleTimerStopped + ), + // eslint-disable-next-line + [world.isTimerRunning] ) - const startTimer = () => { - if (!isTimerRunning) { - handleTimerStarted() - let timer = setInterval(() => { - duration.subtract(1, 'second') - setTimeLeft( - `${duration.minutes()} minutes ${duration.seconds()} seconds` - ) - if (duration.asMilliseconds() === 0) { - clearInterval(timer) - handleTimerStopped() - } - }, 1000) - } - } - - useEffect(startTimer, [isTimerRunning]) - return ( Item Timer diff --git a/src/components/TravelTimer.js b/src/components/TravelTimer.js index 4236344..16e0f87 100644 --- a/src/components/TravelTimer.js +++ b/src/components/TravelTimer.js @@ -9,29 +9,15 @@ import { setDestination, setShipTraveling } from '../redux/actions/ship' -import { createDiffDuration } from '../util' +import { travelTimerLogic } from '../util' const TravelTimer = ({ handleTimerStopped, ship }) => { const [timeLeft, setTimeLeft] = useState(null) - const timerLogic = () => { - if (ship.isShipTraveling) { - const travelTimer = setInterval(() => { - const diffDuration = createDiffDuration(ship.destination.eta) - - diffDuration.subtract(1, 'second') - - if (diffDuration.asMilliseconds() === 0) { - clearInterval(travelTimer) - handleTimerStopped(ship) - } - - setTimeLeft(diffDuration) - }, 1000) - } - } - - useEffect(timerLogic, [ship.isShipTraveling]) + // eslint-disable-next-line + useEffect(() => travelTimerLogic(ship, setTimeLeft, handleTimerStopped), [ + ship.isShipTraveling + ]) return ship.isShipTraveling ? ( diff --git a/src/util.js b/src/util.js index fac9be0..1e82388 100644 --- a/src/util.js +++ b/src/util.js @@ -122,3 +122,43 @@ export const generateContracts = () => { return contracts } + +export const travelTimerLogic = (ship, setTimeLeft, handleTimerStopped) => { + if (ship.isShipTraveling) { + const travelTimer = setInterval(() => { + const diffDuration = createDiffDuration(ship.destination.eta) + + diffDuration.subtract(1, 'second') + + if (diffDuration.asMilliseconds() === 0) { + clearInterval(travelTimer) + handleTimerStopped(ship) + } + + setTimeLeft(diffDuration) + }, 1000) + } +} + +export const itemTimerLogic = ( + world, + setTimeLeft, + handleTimerStarted, + handleTimerStopped +) => { + const { isTimerRunning } = world + + let duration = createDuration() + + if (!isTimerRunning) { + handleTimerStarted() + let timer = setInterval(() => { + duration.subtract(1, 'second') + setTimeLeft(`${duration.minutes()} minutes ${duration.seconds()} seconds`) + if (duration.asMilliseconds() === 0) { + clearInterval(timer) + handleTimerStopped() + } + }, 1000) + } +}