From f7aa7bfb2f6115029eecb04836da2f3e5510c0e3 Mon Sep 17 00:00:00 2001 From: Giampaolo Bellavite Date: Mon, 5 Mar 2018 09:42:04 +0100 Subject: [PATCH 1/2] Return if node is not found --- src/DayPicker.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DayPicker.js b/src/DayPicker.js index 6fb17ceee1..c60af374ae 100644 --- a/src/DayPicker.js +++ b/src/DayPicker.js @@ -300,7 +300,7 @@ export default class DayPicker extends Component { focusPreviousDay(dayNode) { const dayNodes = Helpers.getDayNodes(this.dayPicker, this.props.classNames); const dayNodeIndex = Helpers.nodeListToArray(dayNodes).indexOf(dayNode); - + if (dayNodeIndex === -1) return; if (dayNodeIndex === 0) { this.showPreviousMonth(() => this.focusLastDayOfMonth()); } else { @@ -311,7 +311,7 @@ export default class DayPicker extends Component { focusNextDay(dayNode) { const dayNodes = Helpers.getDayNodes(this.dayPicker, this.props.classNames); const dayNodeIndex = Helpers.nodeListToArray(dayNodes).indexOf(dayNode); - + if (dayNodeIndex === -1) return; if (dayNodeIndex === dayNodes.length - 1) { this.showNextMonth(() => this.focusFirstDayOfMonth()); } else { From cf40a4cbc99cd7158b97b01fe24e6733aa7ca8b3 Mon Sep 17 00:00:00 2001 From: Giampaolo Bellavite Date: Mon, 5 Mar 2018 09:42:11 +0100 Subject: [PATCH 2/2] Add unit tests for not found nodes --- test/daypicker/methods.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/daypicker/methods.js b/test/daypicker/methods.js index ec55860ec4..90e706442e 100644 --- a/test/daypicker/methods.js +++ b/test/daypicker/methods.js @@ -258,6 +258,11 @@ describe('DayPicker’s methods', () => { expect(document.activeElement.innerHTML).toBe('31'); expect(instance.state.currentMonth.getMonth()).toBe(4); }); + it('should not throw an error when the node is not found', () => { + expect(() => + instance.focusPreviousDay(document.createElement('div')) + ).not.toThrow(); + }); }); describe('focusNextDay()', () => { @@ -301,6 +306,11 @@ describe('DayPicker’s methods', () => { expect(document.activeElement.innerHTML).toBe('1'); expect(instance.state.currentMonth.getMonth()).toBe(2); }); + it('should not throw an error when the node is not found', () => { + expect(() => + instance.focusNextDay(document.createElement('div')) + ).not.toThrow(); + }); }); describe('focusNextWeek()', () => {