Jelajahi Sumber

visibleRange should receive zoned date

Adam Shaw 9 tahun lalu
induk
melakukan
fb54981fca

+ 7 - 2
src/common/View.date-range.js

@@ -284,7 +284,10 @@ View.mixin({
 	// Builds a normalized range object for the "visible" range,
 	// which is a way to define the currentRange and activeRange at the same time.
 	buildCustomVisibleRange: function(date) {
-		var visibleRange = this.getRangeOption('visibleRange', date);
+		var visibleRange = this.getRangeOption(
+			'visibleRange',
+			this.calendar.moment(date) // correct zone. also generates new obj that avoids mutations
+		);
 
 		if (visibleRange && (!visibleRange.start || !visibleRange.end)) {
 			return null;
@@ -340,7 +343,9 @@ View.mixin({
 	},
 
 
-	// arguments after name will be forwarded to a hypothetical function value
+	// Arguments after name will be forwarded to a hypothetical function value
+	// WARNING: passed-in arguments will be given to generator functions as-is and can cause side-effects.
+	// Always clone your objects if you fear mutation.
 	getRangeOption: function(name) {
 		var val = this.opt(name);
 

+ 32 - 2
tests/automated-better/view-dates/visibleRange.js

@@ -78,8 +78,9 @@ describe('visibleRange', function() {
 		});
 
 		describe('when a function', function() {
-			it('receives the calendar\'s defaultDate', function() {
-				var defaultDateInput = '2017-06-08T12:30:00';
+			var defaultDateInput = '2017-06-08T12:30:00';
+
+			it('receives the calendar\'s defaultDate, timezoneless', function() {
 				var matched = false;
 
 				initCalendar({
@@ -95,6 +96,35 @@ describe('visibleRange', function() {
 
 				expect(matched).toBe(true);
 			});
+
+			it('receives the calendar\'s defaultDate, with UTC timezone', function() {
+				var matched = false;
+
+				initCalendar({
+					timezone: 'UTC',
+					defaultDate: defaultDateInput,
+					visibleRange: function(date) {
+						console.log(date.format());
+						// this function will receive the date for prev/next,
+						// which should be ignored. make sure just one call matches.
+						if (date.format() === defaultDateInput + 'Z') {
+							matched = true;
+						}
+					}
+				});
+
+				expect(matched).toBe(true);
+			});
+
+			it('does not cause side effects when given date is mutated', function() {
+				initCalendar({
+					defaultDate: defaultDateInput,
+					visibleRange: function(date) {
+						date.add(1, 'year');
+					}
+				});
+				expect(currentCalendar.getDate()).toEqualMoment(defaultDateInput);
+			});
 		});
 
 		describe('when given an invalid range', function() {