Skip to content

Commit

Permalink
Test showStateChangeNotifications
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Jun 23, 2019
1 parent 8b615a2 commit 5da2630
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/Farmhand.js
Expand Up @@ -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() {
Expand All @@ -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];
Expand Down
18 changes: 18 additions & 0 deletions src/Farmhand.test.js
Expand Up @@ -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');
Expand Down

0 comments on commit 5da2630

Please sign in to comment.