Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add destination to item contracts #78

Merged
merged 2 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/__tests__/__snapshots__/App.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ exports[`<App /> Should render the <App /> component. 1`] = `
<div
class="StyledBox__StyledBoxGap-sc-13pk1d4-1 iChEkS"
/>
<span
class="StyledText-sc-1sadyjn-0 bnqisW"
>
Destination
</span>
<div
class="StyledBox__StyledBoxGap-sc-13pk1d4-1 iChEkS"
/>
<span
class="StyledText-sc-1sadyjn-0 fWSbXS"
>
Test Planet 2
</span>
<div
class="StyledBox__StyledBoxGap-sc-13pk1d4-1 iChEkS"
/>
<button
class="StyledButton-sc-323bzc-0 jJyttA"
disabled=""
Expand Down
12 changes: 10 additions & 2 deletions src/__tests__/components/ContractsDisplay.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { customRender } from '../../test-utils'
import ContractsDisplay from '../../components/ContractsDisplay'
import { defaultState } from '../../fixtures'
import { fireEvent } from '@testing-library/dom';
import { fireEvent } from '@testing-library/dom'

const customState = {
...defaultState,
user: {
...defaultState.user,
contracts: [{ id: '0', itemType: 'Plasma', value: 2, volume: 1 }]
contracts: [
{
destination: { name: 'Planet 1', value: 0 },
id: '0',
itemType: 'Plasma',
value: 2,
volume: 1
}
]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ exports[`<ContractsDisplay /> Should render the <ContractsDisplay /> component.
<div
class="StyledBox__StyledBoxGap-sc-13pk1d4-1 iChEkS"
/>
<span
class="StyledText-sc-1sadyjn-0 bnqisW"
>
Destination
</span>
<div
class="StyledBox__StyledBoxGap-sc-13pk1d4-1 iChEkS"
/>
<span
class="StyledText-sc-1sadyjn-0 fWSbXS"
>
Test Planet 2
</span>
<div
class="StyledBox__StyledBoxGap-sc-13pk1d4-1 iChEkS"
/>
<button
class="StyledButton-sc-323bzc-0 jJyttA"
disabled=""
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ describe('Utilities', () => {
})

it('Should test generateContracts().', () => {
expect(generateContracts().length).toBe(5)
expect(
generateContracts([{ location: 50, name: 'Test Planet 2' }]).length
).toBe(5)
})
})
2 changes: 2 additions & 0 deletions src/components/ContractsDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const ContractsDisplay = ({
<Text>{contract.volume}</Text>
<Text weight="bold">Value</Text>
<Text>{contract.value}</Text>
<Text weight="bold">Destination</Text>
<Text>{contract.destination.name}</Text>
<Button
disabled={currentContract !== null}
hoverIndicator
Expand Down
1 change: 1 addition & 0 deletions src/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const defaultState = {
world: {
contracts: [
{
destination: { name: 'Test Planet 2', value: 50 },
id: '0',
itemType: 'Ore',
volume: 1,
Expand Down
14 changes: 13 additions & 1 deletion src/redux/actions/world.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { setShipLocation } from './ship'
import { generatePlanets } from '../../util'
import { generatePlanets, generateContracts } from '../../util'

// * ACTION TYPES
const CLEAR_ITEMS = 'CLEAR_ITEMS'
const REFRESH_ITEMS = 'REFRESH_ITEMS'
const REMOVE_ITEM = 'REMOVE_ITEM'
const SET_CONTRACTS = 'SET_CONTRACTS'
const SET_PLANETS = 'SET_PLANETS'
const SET_TIMER_RUNNING = 'SET_TIMER_RUNNING'

Expand Down Expand Up @@ -32,6 +33,15 @@ export const removeItem = (item, quantity) => ({
}
})

/**
* Sets the contracts array.
* @param {array} contracts Array of contract objects.
*/
export const setContracts = contracts => ({
type: SET_CONTRACTS,
payload: { contracts }
})

/**
* Sets the planet objects in the world.
* @param {array} planets Array of planets.
Expand All @@ -57,9 +67,11 @@ export const initializeApplication = () => dispatch => {
const planets = generatePlanets()
const homePlanet = planets.find(planet => planet.isHomePlanet === true)
const location = { name: homePlanet.name, value: homePlanet.location }
const contracts = generateContracts(planets)

dispatch(setPlanets(planets))
dispatch(setShipLocation(location))
dispatch(setContracts(contracts))
}

export const itemTimerFinish = () => dispatch => {
Expand Down
6 changes: 4 additions & 2 deletions src/redux/reducers/world.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generateItems, generateContracts } from '../../util'
import { generateItems } from '../../util'

const worldDefaultState = {
contracts: generateContracts(),
contracts: [],
isTimerRunning: false,
planets: []
}
Expand Down Expand Up @@ -66,6 +66,8 @@ export default (state = worldDefaultState, action) => {
})

return { ...state, planets: updatedPlanets }
case 'SET_CONTRACTS':
return { ...state, contracts: action.payload.contracts }
case 'SET_PLANETS':
return { ...state, planets: action.payload.planets }
case 'SET_TIMER_RUNNING':
Expand Down
10 changes: 9 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,22 @@ export const createDiffDuration = eta => {

/**
* Generates an array of item contracts randomly.
* @param {array} planets Array of planet objects.
* @returns {array}
*/
export const generateContracts = () => {
export const generateContracts = planets => {
const contracts = []

for (let i = 0; i < 5; i++) {
const itemType = itemList[Math.floor(Math.random() * itemList.length)].name
const destinationPlanet =
planets[Math.floor(Math.random() * planets.length)]

const contract = {
destination: {
name: destinationPlanet.name,
value: destinationPlanet.location
},
id: uuidv4(),
itemType,
value: itemList.find(item => item.name === itemType).value + 1,
Expand Down