diff --git a/src/Farmhand.js b/src/Farmhand.js index 033dd642e..1e6a02edb 100644 --- a/src/Farmhand.js +++ b/src/Farmhand.js @@ -274,18 +274,7 @@ export default class Farmhand extends Component { } componentDidUpdate(prevProps, prevState) { - const { - state: { purchasedCowPen }, - } = this; - - // TODO: Test this. - if (purchasedCowPen !== prevState.purchasedCowPen) { - const { cows } = PURCHASEABLE_COW_PENS.get(purchasedCowPen); - - this.showNotification( - `Purchased a cow pen with capacity for ${cows} cows!` - ); - } + this.showStateChangeNotifications(prevState); } clearPersistedData() { @@ -307,6 +296,23 @@ export default class Farmhand extends Component { })); } + /** + * @param {farmhand.state} prevState + */ + showStateChangeNotifications(prevState) { + const { + state: { purchasedCowPen }, + } = this; + + if (purchasedCowPen !== prevState.purchasedCowPen) { + const { cows } = PURCHASEABLE_COW_PENS.get(purchasedCowPen); + + this.showNotification( + `Purchased a cow pen with capacity for ${cows} cows!` + ); + } + } + incrementDay() { const nextDayState = computeStateForNextDay(this.state); const pendingNotifications = [...nextDayState.newDayNotifications]; diff --git a/src/Farmhand.test.js b/src/Farmhand.test.js index 47f733c4d..23b2be1ab 100644 --- a/src/Farmhand.test.js +++ b/src/Farmhand.test.js @@ -189,6 +189,24 @@ describe('instance methods', () => { }); }); + describe('showStateChangeNotifications', () => { + describe('cow pen purchasing', () => { + test('shows notification', () => { + const showNotification = jest.spyOn( + component.instance(), + 'showNotification' + ); + + component.setState({ purchasedCowPen: 1 }); + component + .instance() + .showStateChangeNotifications({ purchasedCowPen: 0 }); + + expect(showNotification).toHaveBeenCalled(); + }); + }); + }); + describe('incrementDay', () => { beforeEach(() => { jest.spyOn(component.instance().localforage, 'setItem');