Преглед на файлове

bunch of bugfixes, getting tests to work

Adam Shaw преди 7 години
родител
ревизия
59da1034ee

+ 2 - 2
src/datelib/duration.ts

@@ -36,7 +36,7 @@ const PARSE_RE = /^(?:(\d+)\.)?(\d\d):(\d\d)(?::(\d\d)(?:\.(\d\d\d))?)?/
 export function createDuration(input, unit?: string) {
   if (typeof input === 'string') {
     return parseString(input)
-  } else if (typeof input === 'object') {
+  } else if (typeof input === 'object' && input) { // non-null object
     return normalizeObject(input)
   } else if (typeof input === 'number') {
     return normalizeObject({ [unit || 'millisecond']: input })
@@ -107,7 +107,7 @@ export function addDurations(d0: Duration, d1: Duration) {
   }
 }
 
-export function diffDurations(d0: Duration, d1: Duration): Duration {
+export function subtractDurations(d1: Duration, d0: Duration): Duration {
   return {
     year: d1.year - d0.year,
     month: d1.month - d0.month,

+ 2 - 0
src/datelib/moment.ts

@@ -15,6 +15,8 @@ registerCmdFormatter('moment', function(cmdStr: string, arg: VerboseFormattingAr
     mom = (moment as any).tz(arg.date.array, arg.timeZone)
   }
 
+  // what about accepting a forced timezone if .tz isn't present?
+
   mom.locale(arg.localeCodes[0])
 
   return mom.format(cmdStr)

+ 5 - 3
src/models/event-source/FuncEventSource.ts

@@ -32,9 +32,11 @@ export default class FuncEventSource extends EventSource {
     unpromisify( // allow the func to return a promise
       this.func.bind(
         this.calendar,
-        dateEnv.toDate(start),
-        dateEnv.toDate(end),
-        dateEnv.timeZone
+        {
+          start: dateEnv.toDate(start),
+          end: dateEnv.toDate(end),
+          timeZone: dateEnv.timeZone
+        }
       ),
       (rawEventDefs) => {
         this.calendar.popLoading()

+ 14 - 16
src/models/event/EventDefDateMutation.ts

@@ -1,7 +1,7 @@
 import Calendar from '../../Calendar'
 import EventDateProfile from './EventDateProfile'
 import { startOfDay, diffWholeDays, diffDayAndTime } from '../../datelib/marker'
-import { Duration, createDuration, diffDurations } from '../../datelib/duration'
+import { Duration, createDuration, subtractDurations } from '../../datelib/duration'
 
 export default class EventDefDateMutation {
 
@@ -18,7 +18,7 @@ export default class EventDefDateMutation {
 
   static createFromDiff(dateProfile0, dateProfile1, largeUnit, calendar: Calendar) {
     const dateEnv = calendar.dateEnv
-    let clearEnd = dateProfile0.end && !dateProfile1.end
+    let clearEnd = dateProfile0.hasEnd && !dateProfile1.hasEnd
     let forceTimed = dateProfile0.isAllDay && !dateProfile1.isAllDay
     let forceAllDay = !dateProfile0.isAllDay && dateProfile1.isAllDay
     let dateDelta
@@ -32,22 +32,20 @@ export default class EventDefDateMutation {
         return createDuration(dateEnv.diffWholeYears(date0, date1), 'year')
       } else if (largeUnit === 'month') {
         return createDuration(dateEnv.diffWholeMonths(date0, date1), 'month')
-      } else if (dateProfile1.isAllDay) {
-        return createDuration(diffWholeDays(date0, date1), 'day')
       } else {
         return diffDayAndTime(date0, date1) // returns a duration
       }
     }
 
-    dateDelta = diffDates(dateProfile0.start, dateProfile1.start)
+    dateDelta = diffDates(dateProfile0.unzonedRange.start, dateProfile1.unzonedRange.start)
 
-    if (dateProfile1.end) {
+    if (dateProfile1.hasEnd) {
       // use unzonedRanges because dateProfile0.end might be null
       endDiff = diffDates(
         dateProfile0.unzonedRange.end,
         dateProfile1.unzonedRange.end
       )
-      endDelta = diffDurations(dateDelta, endDiff)
+      endDelta = subtractDurations(endDiff, dateDelta)
     }
 
     mutation = new EventDefDateMutation()
@@ -79,15 +77,7 @@ export default class EventDefDateMutation {
     if (eventDateProfile.hasEnd && !this.clearEnd) {
       endMarker = eventDateProfile.unzonedRange.end
     } else if (this.endDelta && !endMarker) { // won't always be null?
-      endMarker = calendar.getDefaultEventEnd(isAllDay, startMarker)
-    }
-
-    if (this.forceAllDay) {
-      startMarker = startOfDay(startMarker)
-
-      if (endMarker) {
-        endMarker = startOfDay(endMarker)
-      }
+      endMarker = calendar.getDefaultEventEnd(eventDateProfile.isAllDay, startMarker)
     }
 
     if (this.dateDelta) {
@@ -107,6 +97,14 @@ export default class EventDefDateMutation {
       startMarker = dateEnv.add(startMarker, this.startDelta)
     }
 
+    if (this.forceAllDay) {
+      startMarker = startOfDay(startMarker)
+
+      if (endMarker) {
+        endMarker = startOfDay(endMarker)
+      }
+    }
+
     // TODO: okay to access calendar option?
     if (!endMarker && calendar.opt('forceEventDuration')) {
       endMarker = calendar.getDefaultEventEnd(isAllDay, startMarker)

+ 20 - 15
tests/automated/event-data/events-function.js

@@ -1,5 +1,8 @@
 
 describe('events as a function', function() {
+  pushOptions({
+    timeZone: 'UTC'
+  })
 
   it('requests the correct dates when days at the start/end of the month are hidden', function(done) {
     initCalendar({
@@ -7,9 +10,11 @@ describe('events as a function', function() {
       defaultDate: '2013-06-01', // June 2013 has first day as Saturday, and last as Sunday!
       weekends: false,
       fixedWeekCount: false,
-      events: function(start, end, timezone, callback) {
-        expect(start).toEqualMoment('2013-06-03')
-        expect(end).toEqualMoment('2013-06-29')
+      events: function(arg, callback) {
+        expect(arg.start).toEqualDate('2013-06-03T00:00:00Z')
+        expect(arg.end).toEqualDate('2013-06-29T00:00:00Z')
+        expect(arg.timeZone).toBe('UTC')
+        expect(typeof callback).toBe('function')
         done()
       }
     })
@@ -20,9 +25,9 @@ describe('events as a function', function() {
       defaultView: 'month',
       defaultDate: '2013-06-01',
       showNonCurrentDates: false,
-      events: function(start, end, timezone, callback) {
-        expect(start).toEqualMoment('2013-06-01')
-        expect(end).toEqualMoment('2013-07-01')
+      events: function(arg, callback) {
+        expect(arg.start).toEqualDate('2013-06-01T00:00:00Z')
+        expect(arg.end).toEqualDate('2013-07-01T00:00:00Z')
         done()
       }
     })
@@ -33,9 +38,9 @@ describe('events as a function', function() {
       defaultView: 'agendaWeek',
       defaultDate: '2017-06-08',
       minTime: { hours: -2 },
-      events: function(start, end, timezone, callback) {
-        expect(start).toEqualMoment('2017-06-03T22:00:00')
-        expect(end).toEqualMoment('2017-06-11T00:00:00')
+      events: function(arg, callback) {
+        expect(arg.start).toEqualDate('2017-06-03T22:00:00Z')
+        expect(arg.end).toEqualDate('2017-06-11T00:00:00Z')
         done()
       }
     })
@@ -46,9 +51,9 @@ describe('events as a function', function() {
       defaultView: 'agendaWeek',
       defaultDate: '2017-06-08',
       maxTime: '26:00',
-      events: function(start, end, timezone, callback) {
-        expect(start).toEqualMoment('2017-06-04T00:00:00')
-        expect(end).toEqualMoment('2017-06-11T02:00:00')
+      events: function(arg, callback) {
+        expect(arg.start).toEqualDate('2017-06-04T00:00:00Z')
+        expect(arg.end).toEqualDate('2017-06-11T02:00:00Z')
         done()
       }
     })
@@ -61,7 +66,7 @@ describe('events as a function', function() {
       loading: function(bool) {
         loadingCallArgs.push(bool)
       },
-      events: function(start, end, timezone, callback) {
+      events: function(arg, callback) {
         setTimeout(function() {
           expect(loadingCallArgs).toEqual([ true ])
           callback([])
@@ -82,12 +87,12 @@ describe('events as a function', function() {
         loadingCallArgs.push(bool)
       },
       eventSources: [
-        function(start, end, timezone, callback) {
+        function(arg, callback) {
           setTimeout(function() {
             callback([])
           }, 0)
         },
-        function(start, end, timezone, callback) {
+        function(arg, callback) {
           setTimeout(function() {
             callback([])
           }, 10)

+ 24 - 23
tests/automated/event-data/lazyFetching.js

@@ -1,6 +1,7 @@
 
 describe('lazyFetching', function() {
   pushOptions({
+    timezone: 'UTC',
     defaultView: 'month',
     defaultDate: '2017-10-04'
   })
@@ -10,10 +11,10 @@ describe('lazyFetching', function() {
       lazyFetching: true
     })
 
-    it('won\'t fetch weeks already queryied', function() {
-      var args
+    it('won\'t fetch weeks already queried', function() {
+      var arg
       var options = {
-        events: function(start, end, timezone, callback) {
+        events: function(arg, callback) {
           callback([])
         }
       }
@@ -27,9 +28,9 @@ describe('lazyFetching', function() {
 
       expect(options.events.calls.count()).toBe(1)
 
-      args = options.events.calls.argsFor(0)
-      expect(args[0]).toEqualMoment('2017-10-01')
-      expect(args[1]).toEqualMoment('2017-11-12')
+      arg = options.events.calls.argsFor(0)[0]
+      expect(arg.start).toEqualDate('2017-10-01T00:00:00Z')
+      expect(arg.end).toEqualDate('2017-11-12T00:00:00Z')
     })
   })
 
@@ -39,9 +40,9 @@ describe('lazyFetching', function() {
     })
 
     it('will fetch each new week range', function() {
-      var args
+      var arg
       var options = {
-        events: function(start, end, timezone, callback) {
+        events: function(arg, callback) {
           callback([])
         }
       }
@@ -55,25 +56,25 @@ describe('lazyFetching', function() {
 
       expect(options.events.calls.count()).toBe(5)
 
-      args = options.events.calls.argsFor(0)
-      expect(args[0]).toEqualMoment('2017-10-01')
-      expect(args[1]).toEqualMoment('2017-11-12')
+      arg = options.events.calls.argsFor(0)[0]
+      expect(arg.start).toEqualDate('2017-10-01T00:00:00Z')
+      expect(arg.end).toEqualDate('2017-11-12T00:00:00Z')
 
-      args = options.events.calls.argsFor(1)
-      expect(args[0]).toEqualMoment('2017-10-01T00:00:00')
-      expect(args[1]).toEqualMoment('2017-10-08T00:00:00')
+      arg = options.events.calls.argsFor(1)[0]
+      expect(arg.start).toEqualDate('2017-10-01T00:00:00Z')
+      expect(arg.end).toEqualDate('2017-10-08T00:00:00Z')
 
-      args = options.events.calls.argsFor(2)
-      expect(args[0]).toEqualMoment('2017-10-08T00:00:00')
-      expect(args[1]).toEqualMoment('2017-10-15T00:00:00')
+      arg = options.events.calls.argsFor(2)[0]
+      expect(arg.start).toEqualDate('2017-10-08T00:00:00Z')
+      expect(arg.end).toEqualDate('2017-10-15T00:00:00Z')
 
-      args = options.events.calls.argsFor(3)
-      expect(args[0]).toEqualMoment('2017-10-15T00:00:00')
-      expect(args[1]).toEqualMoment('2017-10-22T00:00:00')
+      arg = options.events.calls.argsFor(3)[0]
+      expect(arg.start).toEqualDate('2017-10-15T00:00:00Z')
+      expect(arg.end).toEqualDate('2017-10-22T00:00:00Z')
 
-      args = options.events.calls.argsFor(4)
-      expect(args[0]).toEqualMoment('2017-10-22T00:00:00')
-      expect(args[1]).toEqualMoment('2017-10-29T00:00:00')
+      arg = options.events.calls.argsFor(4)[0]
+      expect(arg.start).toEqualDate('2017-10-22T00:00:00Z')
+      expect(arg.end).toEqualDate('2017-10-29T00:00:00Z')
     })
   })
 })

+ 4 - 12
tests/automated/event-data/recurring.js

@@ -16,19 +16,11 @@ describe('recurring events', function() {
 
       var events = currentCalendar.clientEvents()
 
-      expect(events[0].start.format()).toBe(
-        moment('2017-07-04T09:00:00').format() // local
-      )
-      expect(events[0].end.format()).toBe(
-        moment('2017-07-04T11:00:00').format() // local
-      )
+      expect(events[0].start).toEqualDate('2017-07-04T09:00:00') // local
+      expect(events[0].end).toEqualDate('2017-07-04T11:00:00') // local
 
-      expect(events[1].start.format()).toBe(
-        moment('2017-07-06T09:00:00').format() // local
-      )
-      expect(events[1].end.format()).toBe(
-        moment('2017-07-06T11:00:00').format() // local
-      )
+      expect(events[1].start).toEqualDate('2017-07-06T09:00:00') // local
+      expect(events[1].end).toEqualDate('2017-07-06T11:00:00') // local
     })
   })
 

+ 9 - 8
tests/automated/event-data/updateEvent.js

@@ -1,5 +1,8 @@
 
 describe('updateEvent', function() {
+  pushOptions({
+    timezone: 'UTC'
+  })
 
   describe('when changing an event\'s ID', function() {
     pushOptions({
@@ -44,9 +47,9 @@ describe('updateEvent', function() {
       var renderCnt = 0
 
       initCalendar({
-        eventRender: function(eventObj) {
+        eventRender: function(arg) {
           if (allRenderCnt === 1) {
-            expect(eventObj.id).toBe('3')
+            expect(arg.event.id).toBe('3')
             renderCnt++
           }
         },
@@ -103,8 +106,8 @@ describe('updateEvent', function() {
             eventObj = currentCalendar.clientEvents('2')[0]
 
             expect(eventObj.allDay).toBe(true)
-            expect(eventObj.start.format()).toBe('2017-07-14')
-            expect(eventObj.end.format()).toBe('2017-07-15')
+            expect(eventObj.start).toEqualDate('2017-07-14T00:00:00Z')
+            expect(eventObj.end).toEqualDate('2017-07-15T00:00:00Z')
 
             done()
           }
@@ -138,10 +141,8 @@ describe('updateEvent', function() {
 
       event = currentCalendar.clientEvents()[0]
       expect(event.allDay).toBe(false)
-      expect(moment.isMoment(event.start)).toBe(true)
-      expect(moment.isMoment(event.end)).toBe(true)
-      expect(event.start).toEqualMoment('2016-04-29T12:00:00')
-      expect(event.end).toEqualMoment('2016-04-29T14:00:00')
+      expect(event.start).toEqualDate('2016-04-29T12:00:00Z')
+      expect(event.end).toEqualDate('2016-04-29T14:00:00Z')
     })
   })
 

+ 39 - 0
tests/automated/lib/moment.js

@@ -1,4 +1,43 @@
 
+beforeEach(function() {
+  jasmine.addMatchers({
+    toEqualDate() {
+      return {
+        compare: function(actual, expected) {
+          var result;
+
+          if (typeof expected === 'string') {
+            expected = new Date(expected)
+          }
+
+          if (!(actual instanceof Date)) {
+            result = {
+              pass: false,
+              message: 'Actual value ' + actual + 'needs to be an instance of a Date'
+            }
+          } else if (!(expected instanceof Date)) {
+            result = {
+              pass: false,
+              message: 'Expected value ' + expected + 'needs to be an instance of a Date'
+            }
+          } else if (actual.valueOf() !== expected.valueOf()) {
+            result = {
+              pass: false,
+              message: 'Date ' + actual.toUTCString() + ' does not equal ' + expected.toUTCString()
+            }
+          } else {
+            result = { pass: true }
+          }
+
+          return result
+        }
+      }
+    }
+  })
+})
+
+// kill all this...
+
 function serializeDuration(duration) {
   return Math.floor(duration.asDays()) + '.' +
     pad(duration.hours(), 2) + ':' +