Skip to content

Commit

Permalink
refactor(utils): use global noop
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Aug 5, 2023
1 parent b2c440e commit 6397358
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 125 deletions.
4 changes: 3 additions & 1 deletion src/components/AppBar/AppBar.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react'
import { shallow } from 'enzyme'

import { noop } from '../../utils/noop'

import AppBar from './AppBar'

let component
Expand All @@ -9,7 +11,7 @@ beforeEach(() => {
component = shallow(
<AppBar
{...{
handleClickNotificationIndicator: () => {},
handleClickNotificationIndicator: noop,
money: 0,
showNotifications: false,
todaysNotifications: [],
Expand Down
9 changes: 5 additions & 4 deletions src/components/CowCard/CowCard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import { render, screen } from '@testing-library/react'

import { generateCow } from '../../utils'
import { noop } from '../../utils/noop'
import { PURCHASEABLE_COW_PENS } from '../../constants'
import { cowColors, genders } from '../../enums'

Expand All @@ -23,10 +24,10 @@ describe('CowCard', () => {
daysUntilBirth: -1,
},
cowIdOfferedForTrade: '',
handleCowSelect: () => {},
handleCowNameInputChange: () => {},
handleCowPurchaseClick: () => {},
handleCowTradeClick: () => {},
handleCowSelect: noop,
handleCowNameInputChange: noop,
handleCowPurchaseClick: noop,
handleCowTradeClick: noop,
id: '',
isSelected: false,
isOnline: false,
Expand Down
5 changes: 3 additions & 2 deletions src/components/CowPen/CowPen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { shallow } from 'enzyme'
import { generateCow } from '../../utils'
import { cowColors } from '../../enums'
import { pixel } from '../../img'
import { noop } from '../../utils/noop'

import { Cow } from './CowPen'

Expand All @@ -27,8 +28,8 @@ describe('Cow', () => {
color: cowColors.WHITE,
},
cowInventory: [],
handleCowPenUnmount: () => {},
handleCowClick: () => {},
handleCowPenUnmount: noop,
handleCowClick: noop,
id: '',
isSelected: false,
}}
Expand Down
17 changes: 9 additions & 8 deletions src/components/CowPenContextMenu/CowPenContextMenu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import { render, screen } from '@testing-library/react'

import { generateCow } from '../../utils'
import { noop } from '../../utils/noop'

import { CowPenContextMenu } from './CowPenContextMenu'

Expand All @@ -23,14 +24,14 @@ describe('CowPenContextMenu', () => {
cowBreedingPen: { cowId1: null, cowId2: null, daysUntilBirth: -1 },
cowForSale: generateCow(),
cowInventory: [],
handleCowAutomaticHugChange: () => {},
handleCowBreedChange: () => {},
handleCowHugClick: () => {},
handleCowNameInputChange: () => {},
handleCowOfferClick: () => {},
handleCowSelect: () => {},
handleCowSellClick: () => {},
handleCowWithdrawClick: () => {},
handleCowAutomaticHugChange: noop,
handleCowBreedChange: noop,
handleCowHugClick: noop,
handleCowNameInputChange: noop,
handleCowOfferClick: noop,
handleCowSelect: noop,
handleCowSellClick: noop,
handleCowWithdrawClick: noop,
purchasedCowPen: 1,
selectedCowId: '',
}
Expand Down
6 changes: 4 additions & 2 deletions src/components/DebugMenu/DebugMenu.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react'
import { shallow } from 'enzyme'

import { noop } from '../../utils/noop'

import { DebugMenu } from './DebugMenu'

let component
Expand All @@ -9,8 +11,8 @@ beforeEach(() => {
component = shallow(
<DebugMenu
{...{
handleAddMoneyClick: () => {},
handleItemPurchaseClick: () => {},
handleAddMoneyClick: noop,
handleItemPurchaseClick: noop,
}}
/>
)
Expand Down
106 changes: 52 additions & 54 deletions src/components/Farmhand/Farmhand.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
sleep,
transformStateDataForImport,
} from '../../utils'
import { noop } from '../../utils/noop'
import { getLevelEntitlements } from '../../utils/getLevelEntitlements'
import { memoize } from '../../utils/memoize'
import { getData, postData } from '../../fetch-utils'
Expand Down Expand Up @@ -425,10 +426,10 @@ export default class Farmhand extends FarmhandReducers {
farmName: 'Unnamed',
field: createNewField(),
fieldMode: OBSERVE,
getCowAccept: () => {},
getCowReject: () => {},
getCowTradeRequest: () => {},
getPeerMetadata: () => {},
getCowAccept: noop,
getCowReject: noop,
getCowTradeRequest: noop,
getPeerMetadata: noop,
hasBooted: false,
heartbeatTimeoutId: null,
historicalDailyLosses: [],
Expand Down Expand Up @@ -470,15 +471,15 @@ export default class Farmhand extends FarmhandReducers {
revenue: 0,
redirect: '',
room: decodeURIComponent(this.props.match.params.room || DEFAULT_ROOM),
sendCowAccept: () => {},
sendCowReject: () => {},
sendCowAccept: noop,
sendCowReject: noop,
purchasedCombine: 0,
purchasedComposter: 0,
purchasedCowPen: 0,
purchasedCellar: 0,
purchasedField: 0,
purchasedSmelter: 0,
sendCowTradeRequest: () => {},
sendCowTradeRequest: noop,
showHomeScreen: true,
showNotifications: true,
stageFocus: stageFocusType.HOME,
Expand Down Expand Up @@ -784,64 +785,61 @@ export default class Farmhand extends FarmhandReducers {
* @param {farmhand.cow} peerPlayerCow
*/
tradeForPeerCow(peerPlayerCow) {
this.setState(
state => {
const {
cowIdOfferedForTrade,
cowInventory,
peers,
sendCowTradeRequest,
} = state

if (!sendCowTradeRequest) return null
this.setState(state => {
const {
cowIdOfferedForTrade,
cowInventory,
peers,
sendCowTradeRequest,
} = state

const { ownerId } = peerPlayerCow
if (!sendCowTradeRequest) return null

const [peerId] =
Object.entries(peers).find(([, peer]) => peer?.id === ownerId) ?? []
const { ownerId } = peerPlayerCow

if (!peerId) {
console.error(
`Owner not found for cow ${JSON.stringify(peerPlayerCow)}`
)
return null
}
const [peerId] =
Object.entries(peers).find(([, peer]) => peer?.id === ownerId) ?? []

const playerAlreadyOwnsRequestedCow = cowInventory.find(
({ id }) => id === peerPlayerCow.id
if (!peerId) {
console.error(
`Owner not found for cow ${JSON.stringify(peerPlayerCow)}`
)
return null
}

if (playerAlreadyOwnsRequestedCow) {
console.error(`Cow ID ${peerPlayerCow.id} is already in inventory`)
return reducers.showNotification(state, COW_ALREADY_OWNED, 'error')
}
const playerAlreadyOwnsRequestedCow = cowInventory.find(
({ id }) => id === peerPlayerCow.id
)

const cowToTradeAway = cowInventory.find(
({ id }) => id === cowIdOfferedForTrade
)
if (playerAlreadyOwnsRequestedCow) {
console.error(`Cow ID ${peerPlayerCow.id} is already in inventory`)
return reducers.showNotification(state, COW_ALREADY_OWNED, 'error')
}

if (!cowToTradeAway) {
console.error(`Cow ID ${cowIdOfferedForTrade} not found`)
return null
}
const cowToTradeAway = cowInventory.find(
({ id }) => id === cowIdOfferedForTrade
)

const cowTradeTimeoutId = setTimeout(
this.handleCowTradeTimeout,
COW_TRADE_TIMEOUT
)
if (!cowToTradeAway) {
console.error(`Cow ID ${cowIdOfferedForTrade} not found`)
return null
}

sendCowTradeRequest(
{
cowOffered: { ...cowToTradeAway, isUsingHuggingMachine: false },
cowRequested: peerPlayerCow,
},
peerId
)
const cowTradeTimeoutId = setTimeout(
this.handleCowTradeTimeout,
COW_TRADE_TIMEOUT
)

return { cowTradeTimeoutId, isAwaitingCowTradeRequest: true }
},
() => {}
)
sendCowTradeRequest(
{
cowOffered: { ...cowToTradeAway, isUsingHuggingMachine: false },
cowRequested: peerPlayerCow,
},
peerId
)

return { cowTradeTimeoutId, isAwaitingCowTradeRequest: true }
}, noop)
}

handleCowTradeTimeout = () => {
Expand Down
9 changes: 5 additions & 4 deletions src/components/Field/Field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fieldMode } from '../../enums'
import { testItem } from '../../test-utils'

import { INFINITE_STORAGE_LIMIT } from '../../constants'
import { noop } from '../../utils/noop'

import { Field, FieldContent, isInHoverRange, MemoPlot } from './Field'

Expand All @@ -22,8 +23,8 @@ beforeEach(() => {
{...{
columns: 0,
hoveredPlotRangeSize: 0,
handleCombineEnabledChange: () => {},
handleFieldActionRangeChange: () => {},
handleCombineEnabledChange: noop,
handleFieldActionRangeChange: noop,
itemsSold: {},
rows: 0,
field: [
Expand All @@ -48,7 +49,7 @@ describe('field rendering', () => {
<FieldContent
{...{
columns: 0,
handleCombineEnabledChange: () => {},
handleCombineEnabledChange: noop,
field: [
[null, null],
[null, null],
Expand All @@ -61,7 +62,7 @@ describe('field rendering', () => {
itemsSold: {},
purchasedCombine: 0,
rows: 0,
setHoveredPlot: () => {},
setHoveredPlot: noop,
}}
/>
)
Expand Down
3 changes: 1 addition & 2 deletions src/components/Item/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import FarmhandContext from '../Farmhand/Farmhand.context'
import { items } from '../../img'
import { itemsMap } from '../../data/maps'
import { itemIds as shopItemIds } from '../../data/shop-inventory'
import { noop } from '../../utils/noop'
import { moneyString } from '../../utils/moneyString'
import {
inventorySpaceRemaining,
Expand All @@ -31,8 +32,6 @@ import AnimatedNumber from '../AnimatedNumber'

import './Item.sass'

const noop = () => {}

const ValueIndicator = ({ poorValue }) => (
<Tooltip
{...{
Expand Down
18 changes: 9 additions & 9 deletions src/components/Navigation/Navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { shallow } from 'enzyme'
import MenuItem from '@material-ui/core/MenuItem'

import { dialogView, stageFocusType } from '../../enums'

import { INFINITE_STORAGE_LIMIT } from '../../constants'
import { noop } from '../../utils/noop'

import { Navigation } from './Navigation'

Expand All @@ -19,14 +19,14 @@ beforeEach(() => {
currentDialogView: dialogView.NONE,
dayCount: 0,
farmName: '',
handleActivePlayerButtonClick: () => {},
handleClickDialogViewButton: () => {},
handleCloseDialogView: () => {},
handleDialogViewExited: () => {},
handleFarmNameUpdate: () => {},
handleOnlineToggleChange: () => {},
handleRoomChange: () => {},
handleViewChange: () => {},
handleActivePlayerButtonClick: noop,
handleClickDialogViewButton: noop,
handleCloseDialogView: noop,
handleDialogViewExited: noop,
handleFarmNameUpdate: noop,
handleOnlineToggleChange: noop,
handleRoomChange: noop,
handleViewChange: noop,
inventory: [],
inventoryLimit: INFINITE_STORAGE_LIMIT,
itemsSold: {},
Expand Down
Loading

0 comments on commit 6397358

Please sign in to comment.