Skip to content

Commit

Permalink
Fixes the issue where a full next week was tacked on when the next mo…
Browse files Browse the repository at this point in the history
…nth began at the beginning of the week
  • Loading branch information
Maja Wichrowska committed Sep 6, 2016
1 parent 2f0ec77 commit 58030d2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/utils/getCalendarMonthWeeks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ export default function getCalendarMonthWeeks(month, enableOutsideDays) {
}
}

// days belonging to the next month
for (let k = currentDay.weekday(), count = 0; k < 7; k++, count++) {
const nextDay = enableOutsideDays && currentDay.clone().add(count, 'day');
currentWeek.push(nextDay);
}
// weekday() returns the index of the day of the week according to the locale
// this means if the week starts on Monday, weekday() will return 0 for a Monday date, not 1
if (currentDay.weekday() !== 0) {
// days belonging to the next month
for (let k = currentDay.weekday(), count = 0; k < 7; k++, count++) {
const nextDay = enableOutsideDays && currentDay.clone().add(count, 'day');
currentWeek.push(nextDay);
}

weeksInMonth.push(currentWeek);
weeksInMonth.push(currentWeek);
}

return weeksInMonth;
}
30 changes: 29 additions & 1 deletion test/utils/getCalendarMonthWeeks_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,41 @@ describe('getCalendarMonthWeeks', () => {
expect(containsFirstOfMonth).to.equal(true);
});

it('contains last of the month', () => {
it('last week contains last of the month', () => {
const lastOfMonth = today.clone().endOf('month');
const containsLastOfMonth = weeks[weeksWithOutsideDays.length - 1]
.filter(day => lastOfMonth.isSame(day, 'day')).length > 0;
expect(containsLastOfMonth).to.equal(true);
});

it('last week contains last of the month if next month begins on Sunday', () => {
const december2016 = moment('2016-12-01');
const lastOfMonth = december2016.clone().endOf('month');
const weeksInDecember = getCalendarMonthWeeks(december2016);
const containsLastOfMonth = weeksInDecember[weeksInDecember.length - 1]
.filter(day => lastOfMonth.isSame(day, 'day')).length > 0;
expect(containsLastOfMonth).to.equal(true);
});

it('last week contains last of the month if next month begins on Monday', () => {
moment.locale('es');
const april2017 = moment('2017-04-01');
const lastOfMonth = april2017.clone().endOf('month');
const weeksInApril = getCalendarMonthWeeks(april2017);
const containsLastOfMonth = weeksInApril[weeksInApril.length - 1]
.filter(day => lastOfMonth.isSame(day, 'day')).length > 0;
expect(containsLastOfMonth).to.equal(true);
});

it('last week contains last of the month if next month begins on Saturday', () => {
const september2016 = moment('2016-09-01');
const lastOfMonth = september2016.clone().endOf('month');
const weeksInSeptember = getCalendarMonthWeeks(september2016);
const containsLastOfMonth = weeksInSeptember[weeksInSeptember.length - 1]
.filter(day => lastOfMonth.isSame(day, 'day')).length > 0;
expect(containsLastOfMonth).to.equal(true);
});

it('each week has 7 non-null elements', () => {
const hasNoNullElements = weeksWithOutsideDays
.reduce((w1, w2) => w1 && w2.reduce((d1, d2) => d1 && !!d2, true), true);
Expand Down

0 comments on commit 58030d2

Please sign in to comment.