From 6397358d4cff7e87f9827e49d9fe02cfabc6abe8 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Sat, 5 Aug 2023 14:49:00 -0500 Subject: [PATCH] refactor(utils): use global noop --- src/components/AppBar/AppBar.test.js | 4 +- src/components/CowCard/CowCard.test.js | 9 +- src/components/CowPen/CowPen.test.js | 5 +- .../CowPenContextMenu.test.js | 17 +-- src/components/DebugMenu/DebugMenu.test.js | 6 +- src/components/Farmhand/Farmhand.js | 106 +++++++++--------- src/components/Field/Field.test.js | 9 +- src/components/Item/Item.js | 3 +- src/components/Navigation/Navigation.test.js | 18 +-- src/components/Plot/Plot.rtl.test.js | 32 +++--- src/components/Plot/Plot.test.js | 5 +- .../QuantityInput/QuantityInput.test.js | 8 +- .../QuickSelect/QuickSelect.test.js | 4 +- .../SettingsView/SettingsView.test.js | 16 +-- src/components/Shop/Shop.test.js | 12 +- src/components/Toolbelt/Toolbelt.js | 3 +- src/game-logic/reducers/fertilizePlot.test.js | 2 - src/utils/noop.js | 1 + 18 files changed, 135 insertions(+), 125 deletions(-) create mode 100644 src/utils/noop.js diff --git a/src/components/AppBar/AppBar.test.js b/src/components/AppBar/AppBar.test.js index ac5f0af6b..70330f1ac 100644 --- a/src/components/AppBar/AppBar.test.js +++ b/src/components/AppBar/AppBar.test.js @@ -1,6 +1,8 @@ import React from 'react' import { shallow } from 'enzyme' +import { noop } from '../../utils/noop' + import AppBar from './AppBar' let component @@ -9,7 +11,7 @@ beforeEach(() => { component = shallow( {}, + handleClickNotificationIndicator: noop, money: 0, showNotifications: false, todaysNotifications: [], diff --git a/src/components/CowCard/CowCard.test.js b/src/components/CowCard/CowCard.test.js index 58f432fe3..9aef44ce8 100644 --- a/src/components/CowCard/CowCard.test.js +++ b/src/components/CowCard/CowCard.test.js @@ -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' @@ -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, diff --git a/src/components/CowPen/CowPen.test.js b/src/components/CowPen/CowPen.test.js index 35b16283a..7831af53c 100644 --- a/src/components/CowPen/CowPen.test.js +++ b/src/components/CowPen/CowPen.test.js @@ -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' @@ -27,8 +28,8 @@ describe('Cow', () => { color: cowColors.WHITE, }, cowInventory: [], - handleCowPenUnmount: () => {}, - handleCowClick: () => {}, + handleCowPenUnmount: noop, + handleCowClick: noop, id: '', isSelected: false, }} diff --git a/src/components/CowPenContextMenu/CowPenContextMenu.test.js b/src/components/CowPenContextMenu/CowPenContextMenu.test.js index 2e5beb87b..f3257fd66 100644 --- a/src/components/CowPenContextMenu/CowPenContextMenu.test.js +++ b/src/components/CowPenContextMenu/CowPenContextMenu.test.js @@ -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' @@ -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: '', } diff --git a/src/components/DebugMenu/DebugMenu.test.js b/src/components/DebugMenu/DebugMenu.test.js index 9fad5e20a..3d23e6225 100644 --- a/src/components/DebugMenu/DebugMenu.test.js +++ b/src/components/DebugMenu/DebugMenu.test.js @@ -1,6 +1,8 @@ import React from 'react' import { shallow } from 'enzyme' +import { noop } from '../../utils/noop' + import { DebugMenu } from './DebugMenu' let component @@ -9,8 +11,8 @@ beforeEach(() => { component = shallow( {}, - handleItemPurchaseClick: () => {}, + handleAddMoneyClick: noop, + handleItemPurchaseClick: noop, }} /> ) diff --git a/src/components/Farmhand/Farmhand.js b/src/components/Farmhand/Farmhand.js index 5d9860ea1..9f35b23b7 100644 --- a/src/components/Farmhand/Farmhand.js +++ b/src/components/Farmhand/Farmhand.js @@ -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' @@ -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: [], @@ -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, @@ -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 = () => { diff --git a/src/components/Field/Field.test.js b/src/components/Field/Field.test.js index b84060422..fdfd1ce8f 100644 --- a/src/components/Field/Field.test.js +++ b/src/components/Field/Field.test.js @@ -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' @@ -22,8 +23,8 @@ beforeEach(() => { {...{ columns: 0, hoveredPlotRangeSize: 0, - handleCombineEnabledChange: () => {}, - handleFieldActionRangeChange: () => {}, + handleCombineEnabledChange: noop, + handleFieldActionRangeChange: noop, itemsSold: {}, rows: 0, field: [ @@ -48,7 +49,7 @@ describe('field rendering', () => { {}, + handleCombineEnabledChange: noop, field: [ [null, null], [null, null], @@ -61,7 +62,7 @@ describe('field rendering', () => { itemsSold: {}, purchasedCombine: 0, rows: 0, - setHoveredPlot: () => {}, + setHoveredPlot: noop, }} /> ) diff --git a/src/components/Item/Item.js b/src/components/Item/Item.js index 3d5886d9e..b133cb165 100644 --- a/src/components/Item/Item.js +++ b/src/components/Item/Item.js @@ -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, @@ -31,8 +32,6 @@ import AnimatedNumber from '../AnimatedNumber' import './Item.sass' -const noop = () => {} - const ValueIndicator = ({ poorValue }) => ( { 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: {}, diff --git a/src/components/Plot/Plot.rtl.test.js b/src/components/Plot/Plot.rtl.test.js index bc9fba1a0..2ce9b384f 100644 --- a/src/components/Plot/Plot.rtl.test.js +++ b/src/components/Plot/Plot.rtl.test.js @@ -5,7 +5,7 @@ import userEvent from '@testing-library/user-event' import { cropLifeStage } from '../../enums' import { testCrop, testShoveledPlot } from '../../test-utils' import { getCropFromItemId, getPlotContentFromItemId } from '../../utils' - +import { noop } from '../../utils/noop' import { items } from '../../img' import { Plot } from './Plot' @@ -19,10 +19,10 @@ describe('class states', () => { render( {}, + handlePlotClick: noop, isInHoverRange: false, selectedItemId: '', - setHoveredPlot: () => {}, + setHoveredPlot: noop, x: 0, y: 0, }} @@ -44,10 +44,10 @@ describe('plot label', () => { render( {}, + handlePlotClick: noop, isInHoverRange: false, selectedItemId: '', - setHoveredPlot: () => {}, + setHoveredPlot: noop, plotContent: getCropFromItemId('carrot'), x: 0, y: 0, @@ -63,10 +63,10 @@ describe('plot label', () => { render( {}, + handlePlotClick: noop, isInHoverRange: false, selectedItemId: '', - setHoveredPlot: () => {}, + setHoveredPlot: noop, plotContent: getCropFromItemId('grape-chardonnay'), x: 0, y: 0, @@ -82,10 +82,10 @@ describe('plot label', () => { render( {}, + handlePlotClick: noop, isInHoverRange: false, selectedItemId: '', - setHoveredPlot: () => {}, + setHoveredPlot: noop, plotContent: { ...getCropFromItemId('carrot'), daysOld: 9, @@ -108,14 +108,14 @@ describe('background image', () => { render( {}, + handlePlotClick: noop, isInHoverRange: false, lifeStage: cropLifeStage.GROWN, plotContent: testCrop({ itemId: 'sample-crop-1', }), selectedItemId: '', - setHoveredPlot: () => {}, + setHoveredPlot: noop, x: 0, y: 0, }} @@ -159,7 +159,7 @@ describe('background image', () => { plotProps: { isInHoverRange: false, selectedItemId: '', - setHoveredPlot: () => {}, + setHoveredPlot: noop, x: 0, y: 0, }, @@ -194,13 +194,13 @@ describe('background image', () => { render( {}, + handlePlotClick: noop, isInHoverRange: false, plotContent: { ...getPlotContentFromItemId('scarecrow'), }, selectedItemId: '', - setHoveredPlot: () => {}, + setHoveredPlot: noop, x: 0, y: 0, }} @@ -219,13 +219,13 @@ describe('background image', () => { render( {}, + handlePlotClick: noop, isInHoverRange: false, plotContent: { ...getPlotContentFromItemId('sprinkler'), }, selectedItemId: '', - setHoveredPlot: () => {}, + setHoveredPlot: noop, x: 0, y: 0, }} diff --git a/src/components/Plot/Plot.test.js b/src/components/Plot/Plot.test.js index d3eb2a45c..e7d8ba34e 100644 --- a/src/components/Plot/Plot.test.js +++ b/src/components/Plot/Plot.test.js @@ -2,6 +2,7 @@ import React from 'react' import { shallow } from 'enzyme' import { getPlotContentFromItemId } from '../../utils' +import { noop } from '../../utils/noop' import { testCrop } from '../../test-utils' import { pixel, plotStates } from '../../img' import { cropLifeStage, fertilizerType } from '../../enums' @@ -21,11 +22,11 @@ beforeEach(() => { component = shallow( {}, + handlePlotClick: noop, isInHoverRange: false, lifeStage: cropLifeStage.SEED, selectedItemId: '', - setHoveredPlot: () => {}, + setHoveredPlot: noop, x: 0, y: 0, }} diff --git a/src/components/QuantityInput/QuantityInput.test.js b/src/components/QuantityInput/QuantityInput.test.js index 0506d87a4..e38431d5c 100644 --- a/src/components/QuantityInput/QuantityInput.test.js +++ b/src/components/QuantityInput/QuantityInput.test.js @@ -1,6 +1,8 @@ import React from 'react' import { shallow } from 'enzyme' +import { noop } from '../../utils/noop' + import QuantityInput from './QuantityInput' let component @@ -9,10 +11,10 @@ beforeEach(() => { component = shallow( {}, - handleUpdateNumber: () => {}, + handleSubmit: noop, + handleUpdateNumber: noop, maxQuantity: 0, - setQuantity: () => {}, + setQuantity: noop, value: 0, }} /> diff --git a/src/components/QuickSelect/QuickSelect.test.js b/src/components/QuickSelect/QuickSelect.test.js index aa433788b..9c4bbce80 100644 --- a/src/components/QuickSelect/QuickSelect.test.js +++ b/src/components/QuickSelect/QuickSelect.test.js @@ -1,6 +1,8 @@ import React from 'react' import { shallow } from 'enzyme' +import { noop } from '../../utils/noop' + import QuickSelect from './QuickSelect' let component @@ -10,7 +12,7 @@ beforeEach(() => { {}, + handleItemSelectClick: noop, playerInventoryQuantities: {}, plantableCropInventory: [], selectedItemId: '', diff --git a/src/components/SettingsView/SettingsView.test.js b/src/components/SettingsView/SettingsView.test.js index 0fcf0daa1..576e28723 100644 --- a/src/components/SettingsView/SettingsView.test.js +++ b/src/components/SettingsView/SettingsView.test.js @@ -1,6 +1,8 @@ import React from 'react' import { shallow } from 'enzyme' +import { noop } from '../../utils/noop' + import SettingsView from './SettingsView' let component @@ -10,13 +12,13 @@ beforeEach(() => { {}, - handleExportDataClick: () => {}, - handleImportDataClick: () => {}, - handleSaveButtonClick: () => {}, - handleShowNotificationsChange: () => {}, - handleUseAlternateEndDayButtonPositionChange: () => {}, - handleShowHomeScreenChange: () => {}, + handleClearPersistedDataClick: noop, + handleExportDataClick: noop, + handleImportDataClick: noop, + handleSaveButtonClick: noop, + handleShowNotificationsChange: noop, + handleUseAlternateEndDayButtonPositionChange: noop, + handleShowHomeScreenChange: noop, showNotifications: true, useAlternateEndDayButtonPosition: false, showHomeScreen: false, diff --git a/src/components/Shop/Shop.test.js b/src/components/Shop/Shop.test.js index 436810a27..52450e97d 100644 --- a/src/components/Shop/Shop.test.js +++ b/src/components/Shop/Shop.test.js @@ -2,8 +2,8 @@ import React from 'react' import { shallow } from 'enzyme' import Inventory from '../Inventory' - import { INFINITE_STORAGE_LIMIT } from '../../constants' +import { noop } from '../../utils/noop' import { Shop } from './Shop' @@ -13,11 +13,11 @@ beforeEach(() => { component = shallow( {}, - handleCowPenPurchase: () => {}, - handleCellarPurchase: () => {}, - handleFieldPurchase: () => {}, - handleStorageExpansionPurchase: () => {}, + handleCombinePurchase: noop, + handleCowPenPurchase: noop, + handleCellarPurchase: noop, + handleFieldPurchase: noop, + handleStorageExpansionPurchase: noop, inventoryLimit: INFINITE_STORAGE_LIMIT, money: 0, purchasedCombine: 0, diff --git a/src/components/Toolbelt/Toolbelt.js b/src/components/Toolbelt/Toolbelt.js index 1b44fc6ef..a74ea4d9e 100644 --- a/src/components/Toolbelt/Toolbelt.js +++ b/src/components/Toolbelt/Toolbelt.js @@ -8,6 +8,7 @@ import Tooltip from '@material-ui/core/Tooltip' import { toolLevel } from '../../enums' import { memoize } from '../../utils/memoize' +import { noop } from '../../utils/noop' import FarmhandContext from '../Farmhand/Farmhand.context' import toolsData from '../../data/tools' @@ -15,8 +16,6 @@ import { tools as toolImages, craftedItems, pixel } from '../../img' import './Toolbelt.sass' -const noop = () => {} - const getTools = memoize(toolLevels => { const tools = [] diff --git a/src/game-logic/reducers/fertilizePlot.test.js b/src/game-logic/reducers/fertilizePlot.test.js index 2bf10c46b..793574ef4 100644 --- a/src/game-logic/reducers/fertilizePlot.test.js +++ b/src/game-logic/reducers/fertilizePlot.test.js @@ -118,8 +118,6 @@ describe('fertilizePlot', () => { describe('FERTILIZE field mode updating', () => { describe('multiple fertilizer units remaining', () => { - beforeEach(() => {}) - test('does not change fieldMode', () => { const state = fertilizePlot( { diff --git a/src/utils/noop.js b/src/utils/noop.js new file mode 100644 index 000000000..9f9f125dd --- /dev/null +++ b/src/utils/noop.js @@ -0,0 +1 @@ +export const noop = () => {}