Skip to content

Commit

Permalink
Modifies all dates in the calendar to have their time set to noon
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Wichrowska committed Oct 12, 2016
1 parent 73b3ff6 commit 1f1c5d0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/utils/getCalendarMonthWeeks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default function getCalendarMonthWeeks(month, enableOutsideDays) {
// set utc offset to get correct dates in future (when timezone changes)
const baseDate = month.clone().utcOffset(month.utcOffset());
const firstOfMonth = baseDate.clone().startOf('month');
const lastOfMonth = baseDate.clone().endOf('month');
const baseDate = month.clone();
const firstOfMonth = baseDate.clone().startOf('month').hour(12);
const lastOfMonth = baseDate.clone().endOf('month').hour(12);

const currentDay = firstOfMonth.clone();
let currentWeek = [];
Expand Down
2 changes: 1 addition & 1 deletion src/utils/toMomentObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export default function toMomentObject(dateString, customFormat) {
[DISPLAY_FORMAT, ISO_FORMAT];

const date = moment(dateString, dateFormats, true);
return date.isValid() ? date : null;
return date.isValid() ? date.hour(12) : null;
}
9 changes: 6 additions & 3 deletions test/components/DateRangePicker_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import DayPicker from '../../src/components/DayPicker';

import OutsideClickHandler from '../../src/components/OutsideClickHandler';

import isSameDay from '../../src/utils/isSameDay';
import isInclusivelyAfterDay from '../../src/utils/isInclusivelyAfterDay';

import {
Expand Down Expand Up @@ -417,7 +418,7 @@ describe('DateRangePicker', () => {

const onDatesChangeArgs = onDatesChangeStub.getCall(0).args[0];
expect(onDatesChangeArgs.startDate).to.equal(wrapper.props().startDate);
expect(onDatesChangeArgs.endDate.isSame(validFutureDateString)).to.equal(true);
expect(isSameDay(onDatesChangeArgs.endDate, moment(validFutureDateString))).to.equal(true);
});

describe('props.onFocusChange', () => {
Expand Down Expand Up @@ -651,7 +652,8 @@ describe('DateRangePicker', () => {
expect(onDatesChangeStub.callCount).to.equal(1);

const onDatesChangeArgs = onDatesChangeStub.getCall(0).args[0];
expect(onDatesChangeArgs.startDate.isSame(validFutureDateString)).to.equal(true);
const futureDate = moment(validFutureDateString);
expect(isSameDay(onDatesChangeArgs.startDate, futureDate)).to.equal(true);
expect(onDatesChangeArgs.endDate).to.equal(endDate);
});

Expand Down Expand Up @@ -696,7 +698,8 @@ describe('DateRangePicker', () => {
expect(onDatesChangeStub.callCount).to.equal(1);

const onDatesChangeArgs = onDatesChangeStub.getCall(0).args[0];
expect(onDatesChangeArgs.startDate.isSame(validFutureDateString)).to.equal(true);
const futureDate = moment(validFutureDateString);
expect(isSameDay(onDatesChangeArgs.startDate, futureDate)).to.equal(true);
expect(onDatesChangeArgs.endDate).to.equal(null);
});

Expand Down
5 changes: 4 additions & 1 deletion test/components/SingleDatePicker_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import OutsideClickHandler from '../../src/components/OutsideClickHandler';
import SingleDatePickerInput from '../../src/components/SingleDatePickerInput';
import SingleDatePicker from '../../src/components/SingleDatePicker';

import isSameDay from '../../src/utils/isSameDay';

describe('SingleDatePicker', () => {
describe('#render', () => {
it('is .SingleDatePicker class', () => {
Expand Down Expand Up @@ -177,7 +179,8 @@ describe('SingleDatePicker', () => {
const onDateChangeStub = sinon.stub();
const wrapper = shallow(<SingleDatePicker id="date" onDateChange={onDateChangeStub} />);
wrapper.instance().onChange(futureDateString);
expect(onDateChangeStub.getCall(0).args[0].isSame(futureDateString)).to.equal(true);
const newDate = onDateChangeStub.getCall(0).args[0];
expect(isSameDay(newDate, moment(futureDateString))).to.equal(true);
});

it('calls props.onFocusChange once', () => {
Expand Down
35 changes: 35 additions & 0 deletions test/utils/getCalendarMonthWeeks_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import moment from 'moment';
import { expect } from 'chai';

import isSameDay from '../../src/utils/isSameDay';
import getCalendarMonthWeeks from '../../src/utils/getCalendarMonthWeeks';

const today = moment();
Expand All @@ -27,6 +28,40 @@ describe('getCalendarMonthWeeks', () => {
expect(isIncluded).to.equal(true);
});

it('all days have a time of 12PM', () => {
weeks.forEach((week) => {
week.forEach((day) => {
if (day) {
expect(day.hours()).to.equal(12);
}
});
});
});

describe('Daylight Savings Time issues', () => {
it('last of February does not equal first of March', () => {
const february = getCalendarMonthWeeks(today.clone().month(1));
const lastWeekOfFebruary = february[february.length - 1].filter(Boolean);
const lastOfFebruary = lastWeekOfFebruary[lastWeekOfFebruary.length - 1];

const march = getCalendarMonthWeeks(today.clone().month(2));
const firstOfMarch = march[0].filter(Boolean)[0];

expect(isSameDay(lastOfFebruary, firstOfMarch)).to.equal(false);
});

it('last of March does not equal first of April', () => {
const march = getCalendarMonthWeeks(today.clone().month(2));
const lastWeekOfMarch = march[march.length - 1].filter(Boolean);
const lastOfMarch = lastWeekOfMarch[lastWeekOfMarch.length - 1];

const april = getCalendarMonthWeeks(today.clone().month(3));
const firstOfApril = april[0].filter(Boolean)[0];

expect(isSameDay(lastOfMarch, firstOfApril)).to.equal(false);
});
});

describe('enableOutsideDays arg is false', () => {
it('first non-null element is first of the month', () => {
const firstOfMonth = today.clone().startOf('month');
Expand Down
15 changes: 15 additions & 0 deletions test/utils/toMomentObject_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import moment from 'moment';
import { expect } from 'chai';

import isSameDay from '../../src/utils/isSameDay';
import toMomentObject from '../../src/utils/toMomentObject';

describe('toMomentObject', () => {
Expand All @@ -20,6 +21,10 @@ describe('toMomentObject', () => {
expect(toMomentObject()).to.equal(null);
});

it('output has time of 12PM', () => {
expect(toMomentObject('1991-07-13').hour()).to.equal(12);
});

it('parses custom format', () => {
const date = toMomentObject('1991---13/07', 'YYYY---DD/MM');

Expand All @@ -29,6 +34,16 @@ describe('toMomentObject', () => {
expect(date.year()).to.equal(1991);
});

describe('Daylight Savings Time issues', () => {
it('last of February does not equal first of March', () => {
expect(isSameDay(toMomentObject('2017-02-28'), toMomentObject('2017-03-01'))).to.equal(false);
});

it('last of March does not equal first of April', () => {
expect(isSameDay(toMomentObject('2017-03-31'), toMomentObject('2017-04-01'))).to.equal(false);
});
});

describe('Catalan locale', () => {
before(() => {
moment.locale('ca');
Expand Down

0 comments on commit 1f1c5d0

Please sign in to comment.