Skip to content

Commit

Permalink
🔀 Merge pull request #71 from alexlee-dev/feature/timer-abstraction
Browse files Browse the repository at this point in the history
✨ Abstract Timer Functions
  • Loading branch information
Alex Lee committed Aug 26, 2019
2 parents b6feef7 + ddde9e3 commit 6f1d5d3
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 50 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/components/ItemTimer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ exports[`<ItemTimer /> Should render the <ItemTimer /> component. 1`] = `
>
Item Timer
</h3>
<span>
0 minutes 0 seconds
</span>
<span />
</div>
</DocumentFragment>
`;
46 changes: 46 additions & 0 deletions src/__tests__/util.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
38 changes: 13 additions & 25 deletions src/components/ItemTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Box>
<Heading level="3">Item Timer</Heading>
Expand Down
24 changes: 5 additions & 19 deletions src/components/TravelTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? (
<Box>
Expand Down
40 changes: 40 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 6f1d5d3

Please sign in to comment.