Skip to content

Commit 7829235

Browse files
prayerslayermartijnrusschen
authored andcommitted
Optional moment.js Part 2 (Hacker0x01#1052)
* Remove moment from calendar_test * Remove moment from date_utils_test * Refactor repetition of key data * Remove moment from datepicker_test * Remove moment from day_test * Remove moment from exclude_time_period_test * Remove moment from exclude_times_test * Remove moment from month_dropdown_test * Remove moment from month_test * Remove moment from timezone_date_picker * Remove moment from week_test * Remove moment from year_dropdown_test * Fix missed instances of moment calls in datepicker_test * Ignore .DS_Store files (MacOS)
1 parent f3c9d89 commit 7829235

13 files changed

+497
-376
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules
22
.sass-cache
33
npm-debug.log
4+
.DS_Store
45
.idea
56
.vscode
67
dist

src/date_utils.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ function getStartOf (date, unit) {
3232
return date.startOf(unit)
3333
}
3434

35+
function getEndOf (date, unit) {
36+
return date.endOf(unit)
37+
}
38+
39+
function getDiff (date1, date2, unit) {
40+
return date1.diff(date2, unit)
41+
}
42+
3543
function isSame (date1, date2, unit) {
3644
return date1.isSame(date2, unit)
3745
}
@@ -92,6 +100,10 @@ export function setYear (date, year) {
92100
return set(date, 'year', year)
93101
}
94102

103+
export function setUTCOffset (date, offset) {
104+
return date.utcOffset(offset)
105+
}
106+
95107
// ** Date Getters **
96108

97109
export function getSecond (date) {
@@ -106,6 +118,7 @@ export function getHour (date) {
106118
return get(date, 'hour')
107119
}
108120

121+
// Returns day of week
109122
export function getDay (date) {
110123
return get(date, 'day')
111124
}
@@ -122,6 +135,7 @@ export function getYear (date) {
122135
return get(date, 'year')
123136
}
124137

138+
// Returns day of month
125139
export function getDate (date) {
126140
return get(date, 'date')
127141
}
@@ -151,6 +165,16 @@ export function getStartOfDate (date) {
151165
return getStartOf(date, 'date')
152166
}
153167

168+
// *** End of ***
169+
170+
export function getEndOfWeek (date) {
171+
return getEndOf(date, 'week')
172+
}
173+
174+
export function getEndOfMonth (date) {
175+
return getEndOf(date, 'month')
176+
}
177+
154178
// ** Date Math **
155179

156180
// *** Addition ***
@@ -202,6 +226,10 @@ export function isAfter (date1, date2) {
202226
return date1.isAfter(date2)
203227
}
204228

229+
export function equals (date1, date2) {
230+
return date1.isSame(date2)
231+
}
232+
205233
export function isSameMonth (date1, date2) {
206234
return isSame(date1, date2, 'month')
207235
}
@@ -228,12 +256,30 @@ export function isDayInRange (day, startDate, endDate) {
228256
return day.clone().startOf('day').isBetween(before, after)
229257
}
230258

259+
// *** Diffing ***
260+
261+
export function getDaysDiff (date1, date2) {
262+
return getDiff(date1, date2, 'days')
263+
}
264+
231265
// ** Date Localization **
232266

233267
export function localizeDate (date, locale) {
234268
return date.clone().locale(locale || moment.locale())
235269
}
236270

271+
export function getDefaultLocale () {
272+
return moment.locale()
273+
}
274+
275+
export function getDefaultLocaleData () {
276+
return moment.localeData()
277+
}
278+
279+
export function registerLocale (localeName, localeData) {
280+
moment.defineLocale(localeName, localeData)
281+
}
282+
237283
export function getLocaleData (date) {
238284
return date.localeData()
239285
}

test/calendar_test.js

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import React from 'react'
2-
import moment from 'moment'
32
import Calendar from '../src/calendar'
43
import Month from '../src/month'
54
import Day from '../src/day'
65
import YearDropdown from '../src/year_dropdown'
76
import MonthDropdown from '../src/month_dropdown'
87
import { shallow, mount } from 'enzyme'
98
import sinon from 'sinon'
9+
import * as utils from '../src/date_utils'
10+
11+
// TODO Possibly rename
12+
const DATE_FORMAT = 'MM/DD/YYYY'
1013

1114
describe('Calendar', function () {
1215
const dateFormat = 'MMMM YYYY'
@@ -24,105 +27,105 @@ describe('Calendar', function () {
2427
}
2528

2629
it('should start with the current date in view if no date range', function () {
27-
const now = moment()
30+
const now = utils.newDate()
2831
const calendar = getCalendar()
29-
assert(calendar.state().date.isSame(now, 'day'))
32+
assert(utils.isSameDay(calendar.state().date, now))
3033
})
3134

3235
it('should start with the today date with specified time zone', function () {
3336
const utcOffset = 12
3437
const calendar = getCalendar({utcOffset})
35-
assert(calendar.state().date.isSame(moment.utc().utcOffset(utcOffset), 'day'))
38+
assert(utils.isSameDay(calendar.state().date, utils.newDateWithOffset(utcOffset)))
3639
})
3740

3841
it('should start with the selected date in view if provided', function () {
39-
const selected = moment().add(1, 'year')
42+
const selected = utils.addYears(utils.newDate(), 1)
4043
const calendar = getCalendar({selected})
41-
assert(calendar.state().date.isSame(selected, 'day'))
44+
assert(utils.isSameDay(calendar.state().date, selected))
4245
})
4346

4447
it('should start with the pre-selected date in view if provided', function () {
45-
const preSelected = moment().add(2, 'year')
46-
const selected = moment().add(1, 'year')
48+
const preSelected = utils.addYears(utils.newDate(), 2)
49+
const selected = utils.addYears(utils.newDate(), 1)
4750
const calendar = getCalendar({ preSelected, selected })
48-
assert(calendar.state().date.isSame(selected, 'day'))
51+
assert(utils.isSameDay(calendar.state().date, selected))
4952
})
5053

5154
it('should start with the current date in view if in date range', function () {
52-
const now = moment()
53-
const minDate = now.clone().subtract(1, 'year')
54-
const maxDate = now.clone().add(1, 'year')
55+
const now = utils.newDate()
56+
const minDate = utils.subtractYears(utils.cloneDate(now), 1)
57+
const maxDate = utils.addYears(utils.cloneDate(now), 1)
5558
const calendar = getCalendar({ minDate, maxDate })
56-
assert(calendar.state().date.isSame(now, 'day'))
59+
assert(utils.isSameDay(calendar.state().date, now))
5760
})
5861

5962
it('should start with the min date in view if after the current date', function () {
60-
const minDate = moment().add(1, 'year')
63+
const minDate = utils.addYears(utils.newDate(), 1)
6164
const calendar = getCalendar({ minDate })
62-
assert(calendar.state().date.isSame(minDate, 'day'))
65+
assert(utils.isSameDay(calendar.state().date, minDate))
6366
})
6467

6568
it('should start with the min include date in view if after the current date', function () {
66-
const minDate = moment().add(1, 'year')
69+
const minDate = utils.addYears(utils.newDate(), 1)
6770
const calendar = getCalendar({ includeDates: [minDate] })
68-
assert(calendar.state().date.isSame(minDate, 'day'))
71+
assert(utils.isSameDay(calendar.state().date, minDate))
6972
})
7073

7174
it('should start with the max date in view if before the current date', function () {
72-
const maxDate = moment().subtract(1, 'year')
75+
const maxDate = utils.subtractYears(utils.newDate(), 1)
7376
const calendar = getCalendar({ maxDate })
74-
assert(calendar.state().date.isSame(maxDate, 'day'))
77+
assert(utils.isSameDay(calendar.state().date, maxDate))
7578
})
7679

7780
it('should start with the max include date in view if before the current date', function () {
78-
const maxDate = moment().subtract(1, 'year')
81+
const maxDate = utils.subtractYears(utils.newDate(), 1)
7982
const calendar = getCalendar({ includeDates: [maxDate] })
80-
assert(calendar.state().date.isSame(maxDate, 'day'))
83+
assert(utils.isSameDay(calendar.state().date, maxDate))
8184
})
8285

8386
it('should start with the open to date in view if given and no selected/min/max dates given', function () {
84-
const openToDate = moment('09/28/1993', 'MM/DD/YYYY')
87+
const openToDate = utils.parseDate('09/28/1993', { dateFormat: DATE_FORMAT })
8588
const calendar = getCalendar({ openToDate })
86-
assert(calendar.state().date.isSame(openToDate, 'day'))
89+
assert(utils.isSameDay(calendar.state().date, openToDate))
8790
})
8891

8992
it('should start with the open to date in view if given and after a min date', function () {
90-
const openToDate = moment('09/28/1993', 'MM/DD/YYYY')
91-
const minDate = moment('01/01/1993', 'MM/DD/YYYY')
93+
const openToDate = utils.parseDate('09/28/1993', { dateFormat: DATE_FORMAT })
94+
const minDate = utils.parseDate('01/01/1993', { dateFormat: DATE_FORMAT })
9295
const calendar = getCalendar({ openToDate, minDate })
93-
assert(calendar.state().date.isSame(openToDate, 'day'))
96+
assert(utils.isSameDay(calendar.state().date, openToDate))
9497
})
9598

9699
it('should start with the open to date in view if given and before a max date', function () {
97-
const openToDate = moment('09/28/1993', 'MM/DD/YYYY')
98-
const maxDate = moment('12/31/1993', 'MM/DD/YYYY')
100+
const openToDate = utils.parseDate('09/28/1993', { dateFormat: DATE_FORMAT })
101+
const maxDate = utils.parseDate('12/31/1993', { dateFormat: DATE_FORMAT })
99102
const calendar = getCalendar({ openToDate, maxDate })
100-
assert(calendar.state().date.isSame(openToDate, 'day'))
103+
assert(utils.isSameDay(calendar.state().date, openToDate))
101104
})
102105

103106
it('should start with the open to date in view if given and in range of the min/max dates', function () {
104-
const openToDate = moment('09/28/1993', 'MM/DD/YYYY')
105-
const minDate = moment('01/01/1993', 'MM/DD/YYYY')
106-
const maxDate = moment('12/31/1993', 'MM/DD/YYYY')
107+
const openToDate = utils.parseDate('09/28/1993', { dateFormat: DATE_FORMAT })
108+
const minDate = utils.parseDate('01/01/1993', { dateFormat: DATE_FORMAT })
109+
const maxDate = utils.parseDate('12/31/1993', { dateFormat: DATE_FORMAT })
107110
const calendar = getCalendar({ openToDate, minDate, maxDate })
108-
assert(calendar.state().date.isSame(openToDate, 'day'))
111+
assert(utils.isSameDay(calendar.state().date, openToDate))
109112
})
110113

111114
it('should open on openToDate date rather than selected date when both are specified', function () {
112-
var openToDate = moment('09/28/1993', 'MM/DD/YYYY')
113-
var selected = moment('09/28/1995', 'MM/DD/YYYY')
115+
var openToDate = utils.parseDate('09/28/1993', { dateFormat: DATE_FORMAT })
116+
var selected = utils.parseDate('09/28/1995', { dateFormat: DATE_FORMAT })
114117
var calendar = getCalendar({ openToDate, selected })
115-
assert(calendar.state().date.isSame(openToDate, 'day'))
118+
assert(utils.isSameDay(calendar.state().date, openToDate))
116119
})
117120

118121
it('should trigger date change when openToDate prop is set after calcInitialState()', () => {
119-
const openToDate = moment('09/28/1993', 'MM/DD/YYYY')
120-
const oneMonthFromOpenToDate = moment('10/28/1993', 'MM/DD/YYYY')
122+
const openToDate = utils.parseDate('09/28/1993', { dateFormat: DATE_FORMAT })
123+
const oneMonthFromOpenToDate = utils.parseDate('10/28/1993', { dateFormat: DATE_FORMAT })
121124
const calendar = getCalendar({ openToDate })
122125

123-
assert(calendar.state().date.isSame(openToDate, 'day'))
126+
assert(utils.isSameDay(calendar.state().date, openToDate))
124127
calendar.setProps({ openToDate: oneMonthFromOpenToDate })
125-
assert(calendar.state().date.isSame(oneMonthFromOpenToDate))
128+
assert(utils.isSameDay(calendar.state().date, oneMonthFromOpenToDate))
126129
})
127130

128131
it('should not show the year dropdown menu by default', function () {
@@ -138,7 +141,7 @@ describe('Calendar', function () {
138141
})
139142

140143
it('should show month navigation if toggled on', function () {
141-
const calendar = getCalendar({ includeDates: [moment()], forceShowMonthNavigation: true })
144+
const calendar = getCalendar({ includeDates: [utils.newDate()], forceShowMonthNavigation: true })
142145
const nextNavigationButton = calendar.find('.react-datepicker__navigation--next')
143146
expect(nextNavigationButton).to.have.length(1)
144147
})
@@ -172,15 +175,15 @@ describe('Calendar', function () {
172175
const calendar = getCalendar({ todayButton: 'Vandaag' })
173176
const todayButton = calendar.find('.react-datepicker__today-button')
174177
todayButton.simulate('click')
175-
expect(calendar.state().date.isSame(moment(), 'day'))
178+
expect(calendar.state().date.isSame(utils.newDate(), 'day'))
176179
})
177180

178181
it('should set custom today date when pressing todayButton', () => {
179-
const todayInAuckland = moment.utc().utcOffset(12)
182+
const todayInAuckland = utils.newDateWithOffset(12)
180183
const calendar = getCalendar({ todayButton: 'Vandaag', utcOffset: 12 })
181184
const todayButton = calendar.find('.react-datepicker__today-button')
182185
todayButton.simulate('click')
183-
expect(calendar.state().date.isSame(todayInAuckland, 'day'))
186+
expect(utils.isSameDay(calendar.state().date, todayInAuckland))
184187
})
185188

186189
it('should use a hash for week label if weekLabel is NOT provided', () => {
@@ -207,7 +210,7 @@ describe('Calendar', function () {
207210
const month = calendar.find(Month).first()
208211
day.simulate('mouseenter')
209212
expect(month.prop('selectingDate')).to.exist
210-
expect(month.prop('selectingDate').isSame(day.prop('day'), 'day')).to.be.true
213+
expect(utils.isSameDay(month.prop('selectingDate'), day.prop('day'))).to.be.true
211214
})
212215

213216
it('should clear the hovered day when the mouse leaves', () => {
@@ -218,15 +221,15 @@ describe('Calendar', function () {
218221
onClickOutside={() => {}}
219222
onSelect={() => {}}/>
220223
)
221-
calendar.setState({ selectingDate: moment() })
224+
calendar.setState({ selectingDate: utils.newDate() })
222225
const month = calendar.find(Month).first()
223226
expect(month.prop('selectingDate')).to.exist
224227
month.simulate('mouseleave')
225228
expect(month.prop('selectingDate')).not.to.exist
226229
})
227230

228-
it('uses MomentJS\'s weekdaysShort instead of weekdaysMin provided useWeekdaysShort prop is present', () => {
229-
moment.defineLocale('weekDaysLocale', {
231+
it('uses weekdaysShort instead of weekdaysMin provided useWeekdaysShort prop is present', () => {
232+
utils.registerLocale('weekDaysLocale', {
230233
parentLocale: 'en',
231234
weekdaysMin: 'AA_BB_CC_DD_EE_FF_GG'.split('_'),
232235
weekdaysShort: 'AAA_BBB_CCC_DDD_EEE_FFF_GGG'.split('_')
@@ -333,39 +336,39 @@ describe('Calendar', function () {
333336

334337
describe('localization', function () {
335338
function testLocale (calendar, selected, locale) {
336-
const localized = selected.clone().locale(locale)
339+
const localized = utils.localizeDate(selected, locale)
337340

338341
const calendarText = calendar.find('.react-datepicker__current-month')
339-
expect(calendarText.text()).to.equal(localized.format(dateFormat))
342+
expect(calendarText.text()).to.equal(utils.formatDate(localized, dateFormat))
340343

341-
const firstDateOfWeek = localized.clone().startOf('week')
342-
const firstWeekDayMin = firstDateOfWeek.localeData().weekdaysMin(firstDateOfWeek)
344+
const firstDateOfWeek = utils.getStartOfWeek(utils.cloneDate(localized))
345+
const firstWeekDayMin = utils.getWeekdayMinInLocale(utils.getLocaleData(firstDateOfWeek), firstDateOfWeek)
343346
const firstHeader = calendar.find('.react-datepicker__day-name').at(0)
344347
expect(firstHeader.text()).to.equal(firstWeekDayMin)
345348
}
346349

347350
it('should use the globally-defined locale by default', function () {
348-
const selected = moment()
351+
const selected = utils.newDate()
349352
const calendar = getCalendar({ selected })
350-
testLocale(calendar, selected, moment.locale())
353+
testLocale(calendar, selected, utils.getDefaultLocale())
351354
})
352355

353356
it('should use the locale specified as a prop', function () {
354357
const locale = 'fr'
355-
const selected = moment().locale(locale)
358+
const selected = utils.localizeDate(utils.newDate(), locale)
356359
const calendar = getCalendar({ selected, locale })
357360
testLocale(calendar, selected, locale)
358361
})
359362

360363
it('should override the locale of the date with the globally-defined locale', function () {
361-
const selected = moment().locale('fr')
364+
const selected = utils.localizeDate(utils.newDate(), 'fr')
362365
const calendar = getCalendar({ selected })
363-
testLocale(calendar, selected, moment.locale())
366+
testLocale(calendar, selected, utils.getDefaultLocale())
364367
})
365368

366369
it('should override the locale of the date with the locale prop', function () {
367370
const locale = 'fr'
368-
const selected = moment()
371+
const selected = utils.newDate()
369372
const calendar = getCalendar({ selected, locale })
370373
testLocale(calendar, selected, locale)
371374
})

0 commit comments

Comments
 (0)