Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifies all dates in the calendar to have their time set to noon #114

Merged
merged 1 commit into from
Oct 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be nice to test the actual conditions - like, that the thing resulting in the extra day being highlighted returns the correct result

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This becomes a bit harder to test I think because it... depends on a lot of different factors. I mean, I guess we can check that the first of april/last of march and first of march/last of april do not make isSameDay true but that seems like a bad test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would test the actual bug this had reported, and assuming the dates were somewhat hardcoded, would likely prevent a regression. Changing the times to 12:01, for example, shouldn't fail tests, because it wouldn't trigger the underlying bug.

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