Преглед изворни кода

test fixes/improvements to make scheduler pass

Adam Shaw пре 7 година
родитељ
комит
76dc5750aa

+ 2 - 2
src/DateProfileGenerator.ts

@@ -1,7 +1,7 @@
 import View from './View'
 import UnzonedRange from './models/UnzonedRange'
 import { DateMarker, startOfDay, addDays } from './datelib/marker'
-import { Duration, createDuration, getWeeksFromInput, asRoughDays, greatestDurationDenominator } from './datelib/duration'
+import { Duration, createDuration, getWeeksFromInput, asRoughDays, asRoughMs, greatestDurationDenominator } from './datelib/duration'
 
 
 export interface DateProfile {
@@ -246,7 +246,7 @@ export default class DateProfileGenerator {
         dateIncrementDuration = createDuration(dateIncrementInput)
 
         // use the smaller of the two units
-        if (dateIncrementDuration < duration) {
+        if (asRoughMs(dateIncrementDuration) < asRoughMs(duration)) {
           alignment = greatestDurationDenominator(
             dateIncrementDuration,
             !getWeeksFromInput(dateIncrementInput)

+ 2 - 2
src/View.ts

@@ -458,9 +458,9 @@ export default abstract class View extends InteractiveDateComponent {
           update()
 
           if (unit === 'second') {
-            delay = 1000 * 60 // every second
+            delay = 1000 // every second
           } else {
-            delay = 1000 * 60 * 60 // otherwise, every minute
+            delay = 1000 * 60 // otherwise, every minute
           }
 
           this.nowIndicatorIntervalID = setInterval(update, delay) // update every interval

+ 5 - 3
src/component/InteractiveDateComponent.ts

@@ -2,7 +2,7 @@ import { elementClosest } from '../util/dom-manip'
 import { getEvIsTouch, listenBySelector, listenToHoverBySelector } from '../util/dom-event'
 import DateComponent from './DateComponent'
 import GlobalEmitter from '../common/GlobalEmitter'
-import { diffDayAndTime } from '../datelib/marker'
+import { diffDayAndTime, diffWholeWeeks } from '../datelib/marker'
 import { Duration, createDuration } from '../datelib/duration'
 
 
@@ -328,10 +328,12 @@ export default abstract class InteractiveDateComponent extends DateComponent {
 
     if (!this.largeUnit) {
       return diffDayAndTime(a, b) // returns a duration
-    } else if (this.largeUnit === 'year') {
-      return createDuration(dateEnv.diffWholeYears(a, b), 'year')
+    } else if (this.largeUnit === 'week') {
+      return createDuration(diffWholeWeeks(a, b), 'week')
     } else if (this.largeUnit === 'month') {
       return createDuration(dateEnv.diffWholeMonths(a, b), 'month')
+    } else if (this.largeUnit === 'year') {
+      return createDuration(dateEnv.diffWholeYears(a, b), 'year')
     }
   }
 

+ 13 - 7
src/datelib/duration.ts

@@ -28,7 +28,7 @@ export interface Duration {
 }
 
 const INTERNAL_UNITS = [ 'year', 'month', 'day', 'time' ]
-const PARSE_RE = /^(?:(\d+)\.)?(\d+):(\d\d)(?::(\d\d)(?:\.(\d\d\d))?)?/
+const PARSE_RE = /^(-?)(?:(\d+)\.)?(\d+):(\d\d)(?::(\d\d)(?:\.(\d\d\d))?)?/
 
 
 // Parsing and Creation
@@ -48,15 +48,17 @@ export function createDuration(input, unit?: string) {
 function parseString(s: string): Duration {
   let m = PARSE_RE.exec(s)
   if (m) {
+    let sign = m[1] ? -1 : 1
     return {
       year: 0,
       month: 0,
-      day: m[1] ? parseInt(m[1], 10) : 0,
-      time:
-        (m[2] ? parseInt(m[2], 10) : 0) * 60 * 60 * 1000 + // hours
-        (m[3] ? parseInt(m[3], 10) : 0) * 60 * 1000 + // minutes
-        (m[4] ? parseInt(m[4], 10) : 0) * 1000 + // seconds
-        (m[5] ? parseInt(m[5], 10) : 0) // ms
+      day: sign * (m[2] ? parseInt(m[2], 10) : 0),
+      time: sign * (
+        (m[3] ? parseInt(m[3], 10) : 0) * 60 * 60 * 1000 + // hours
+        (m[4] ? parseInt(m[4], 10) : 0) * 60 * 1000 + // minutes
+        (m[5] ? parseInt(m[5], 10) : 0) * 1000 + // seconds
+        (m[6] ? parseInt(m[6], 10) : 0) // ms
+      )
     }
   }
   return null
@@ -178,6 +180,10 @@ export function wholeDivideDurations(numerator: Duration, denominator: Duration)
 
       res = localRes
     }
+    else if (numerator[unit]) {
+      // needs to divide by something but can't!
+      return null
+    }
   }
 
   return res

+ 11 - 7
src/datelib/env.ts

@@ -370,13 +370,17 @@ export class DateEnv {
   }
 
   formatIso(marker: DateMarker, extraOptions: any = {}) {
-    return buildIsoString(
-      marker,
-      extraOptions.forcedTimeZoneOffset != null ?
-        extraOptions.forcedTimeZoneOffset :
-        this.offsetForMarker(marker),
-      extraOptions.omitTime
-    )
+    let timeZoneOffset = null
+
+    if (!extraOptions.omitTimeZoneOffset) {
+      if (extraOptions.forcedTimeZoneOffset != null) {
+        timeZoneOffset = extraOptions.forcedTimeZoneOffset
+      } else {
+        timeZoneOffset = this.offsetForMarker(marker)
+      }
+    }
+
+    return buildIsoString(marker, timeZoneOffset, extraOptions.omitTime)
   }
 
 

+ 1 - 0
src/models/event/EventDef.ts

@@ -1,4 +1,5 @@
 import { assignTo } from '../../util/object'
+import Calendar from '../../Calendar'
 import {
   default as ParsableModelMixin,
   ParsableModelInterface

+ 33 - 0
tests/automated/datelib/utils.js

@@ -32,3 +32,36 @@ export function formatIsoTime(date) {
     pad(date.getUTCMinutes(), 2) + ':' +
     pad(date.getUTCSeconds(), 2)
 }
+
+export function formatIsoWithoutTz(date) {
+  return date.toISOString().replace(/(Z|[-+]\d\d:\d\d)$/, '').replace('.000', '')
+}
+
+export function parseIsoAsUtc(s) {
+
+  if (s.length <= 10) {
+    s += 'T00:00:00Z'
+  } else if (s.indexOf('Z') === -1) {
+    s += 'Z'
+  }
+
+  var d = new Date(s)
+
+  if (isNaN(d.valueOf())) {
+    throw s + ' is not valid date input'
+  }
+
+  return d
+}
+
+export function ensureDate(input) {
+  if (input instanceof Date) {
+    return input
+  } else if (typeof input === 'string') {
+    return parseIsoAsUtc(input)
+  } else if (typeof input === 'number') {
+    return new Date(input)
+  }
+
+  throw input + ' is invalid date input'
+}

+ 2 - 2
tests/automated/globals.js

@@ -126,7 +126,7 @@ const timezoneScenarios = {
   local: {
     description: 'when local timezone',
     value: 'local',
-    date: function(str) {
+    createDate: function(str) {
       if (str.length <= 10) { // doesn't have a time part?
         str += 'T00:00:00' // will force it to parse as local
       }
@@ -136,7 +136,7 @@ const timezoneScenarios = {
   UTC: {
     description: 'when UTC timezone',
     value: 'UTC',
-    date: function(str) {
+    createDate: function(str) {
       if (str.length > 10) { // has a time part?
         str += 'Z' // will force it to parse as UTC
       }

+ 37 - 40
tests/automated/legacy/businessHours.js

@@ -3,6 +3,7 @@
 import { getBoundingRect } from '../lib/dom-geom'
 import { doElsMatchSegs } from '../lib/segs'
 import { getTimeGridTop, getTimeGridDayEls } from '../lib/time-grid'
+import { ensureDate } from '../datelib/utils'
 
 
 describe('businessHours', function() {
@@ -25,24 +26,24 @@ describe('businessHours', function() {
     // timed area
     expect(isTimeGridNonBusinessSegsRendered([
       // sun
-      { start: '2014-12-07T00:00Z', end: '2014-12-08T00:00Z' },
+      { start: '2014-12-07T00:00', end: '2014-12-08T00:00' },
       // mon
-      { start: '2014-12-08T00:00Z', end: '2014-12-08T09:00Z' },
-      { start: '2014-12-08T17:00Z', end: '2014-12-09T00:00Z' },
+      { start: '2014-12-08T00:00', end: '2014-12-08T09:00' },
+      { start: '2014-12-08T17:00', end: '2014-12-09T00:00' },
       // tue
-      { start: '2014-12-09T00:00Z', end: '2014-12-09T09:00Z' },
-      { start: '2014-12-09T17:00Z', end: '2014-12-10T00:00Z' },
+      { start: '2014-12-09T00:00', end: '2014-12-09T09:00' },
+      { start: '2014-12-09T17:00', end: '2014-12-10T00:00' },
       // wed
-      { start: '2014-12-10T00:00Z', end: '2014-12-10T09:00Z' },
-      { start: '2014-12-10T17:00Z', end: '2014-12-11T00:00Z' },
+      { start: '2014-12-10T00:00', end: '2014-12-10T09:00' },
+      { start: '2014-12-10T17:00', end: '2014-12-11T00:00' },
       // thu
-      { start: '2014-12-11T00:00Z', end: '2014-12-11T09:00Z' },
-      { start: '2014-12-11T17:00Z', end: '2014-12-12T00:00Z' },
+      { start: '2014-12-11T00:00', end: '2014-12-11T09:00' },
+      { start: '2014-12-11T17:00', end: '2014-12-12T00:00' },
       // fri
-      { start: '2014-12-12T00:00Z', end: '2014-12-12T09:00Z' },
-      { start: '2014-12-12T17:00Z', end: '2014-12-13T00:00Z' },
+      { start: '2014-12-12T00:00', end: '2014-12-12T09:00' },
+      { start: '2014-12-12T17:00', end: '2014-12-13T00:00' },
       // sat
-      { start: '2014-12-13T00:00Z', end: '2014-12-14T00:00Z' }
+      { start: '2014-12-13T00:00', end: '2014-12-14T00:00' }
     ])).toBe(true)
   })
 
@@ -106,24 +107,24 @@ describe('businessHours', function() {
       // timed area
       expect(isTimeGridNonBusinessSegsRendered([
         // sun
-        { start: '2014-12-07T00:00Z', end: '2014-12-08T00:00Z' },
+        { start: '2014-12-07T00:00', end: '2014-12-08T00:00' },
         // mon
-        { start: '2014-12-08T00:00Z', end: '2014-12-08T08:00Z' },
-        { start: '2014-12-08T18:00Z', end: '2014-12-09T00:00Z' },
+        { start: '2014-12-08T00:00', end: '2014-12-08T08:00' },
+        { start: '2014-12-08T18:00', end: '2014-12-09T00:00' },
         // tue
-        { start: '2014-12-09T00:00Z', end: '2014-12-09T08:00Z' },
-        { start: '2014-12-09T18:00Z', end: '2014-12-10T00:00Z' },
+        { start: '2014-12-09T00:00', end: '2014-12-09T08:00' },
+        { start: '2014-12-09T18:00', end: '2014-12-10T00:00' },
         // wed
-        { start: '2014-12-10T00:00Z', end: '2014-12-10T08:00Z' },
-        { start: '2014-12-10T18:00Z', end: '2014-12-11T00:00Z' },
+        { start: '2014-12-10T00:00', end: '2014-12-10T08:00' },
+        { start: '2014-12-10T18:00', end: '2014-12-11T00:00' },
         // thu
-        { start: '2014-12-11T00:00Z', end: '2014-12-11T10:00Z' },
-        { start: '2014-12-11T16:00Z', end: '2014-12-12T00:00Z' },
+        { start: '2014-12-11T00:00', end: '2014-12-11T10:00' },
+        { start: '2014-12-11T16:00', end: '2014-12-12T00:00' },
         // fri
-        { start: '2014-12-12T00:00Z', end: '2014-12-12T10:00Z' },
-        { start: '2014-12-12T16:00Z', end: '2014-12-13T00:00Z' },
+        { start: '2014-12-12T00:00', end: '2014-12-12T10:00' },
+        { start: '2014-12-12T16:00', end: '2014-12-13T00:00' },
         // sat
-        { start: '2014-12-13T00:00Z', end: '2014-12-14T00:00Z' }
+        { start: '2014-12-13T00:00', end: '2014-12-14T00:00' }
       ])).toBe(true)
     })
 
@@ -148,21 +149,21 @@ describe('businessHours', function() {
       // timed area
       expect(isTimeGridNonBusinessSegsRendered([
         // sun
-        { start: '2014-12-07T00:00Z', end: '2014-12-08T00:00Z' },
+        { start: '2014-12-07T00:00', end: '2014-12-08T00:00' },
         // mon
-        { start: '2014-12-08T00:00Z', end: '2014-12-09T00:00Z' },
+        { start: '2014-12-08T00:00', end: '2014-12-09T00:00' },
         // tue
-        { start: '2014-12-09T00:00Z', end: '2014-12-10T00:00Z' },
+        { start: '2014-12-09T00:00', end: '2014-12-10T00:00' },
         // wed
-        { start: '2014-12-10T00:00Z', end: '2014-12-11T00:00Z' },
+        { start: '2014-12-10T00:00', end: '2014-12-11T00:00' },
         // thu
-        { start: '2014-12-11T00:00Z', end: '2014-12-11T10:00Z' },
-        { start: '2014-12-11T16:00Z', end: '2014-12-12T00:00Z' },
+        { start: '2014-12-11T00:00', end: '2014-12-11T10:00' },
+        { start: '2014-12-11T16:00', end: '2014-12-12T00:00' },
         // fri
-        { start: '2014-12-12T00:00Z', end: '2014-12-12T10:00Z' },
-        { start: '2014-12-12T16:00Z', end: '2014-12-13T00:00Z' },
+        { start: '2014-12-12T00:00', end: '2014-12-12T10:00' },
+        { start: '2014-12-12T16:00', end: '2014-12-13T00:00' },
         // sat
-        { start: '2014-12-13T00:00Z', end: '2014-12-14T00:00Z' }
+        { start: '2014-12-13T00:00', end: '2014-12-14T00:00' }
       ])).toBe(true)
     })
   })
@@ -177,7 +178,7 @@ describe('businessHours', function() {
 
     // timed area
     expect(isTimeGridNonBusinessSegsRendered([
-      { start: '2016-07-23T00:00Z', end: '2016-07-24T00:00Z' }
+      { start: '2016-07-23T00:00', end: '2016-07-24T00:00' }
     ])).toBe(true)
   })
 
@@ -201,12 +202,8 @@ describe('businessHours', function() {
       end = obj.end
     }
 
-    if (typeof start === 'string') {
-      start = new Date(start)
-    }
-    if (typeof end === 'string') {
-      end = new Date(end)
-    }
+    start = ensureDate(start)
+    end = ensureDate(end)
 
     var startDay = FullCalendar.startOfDay(start)
     var endDay = FullCalendar.startOfDay(end)

+ 1 - 1
tests/automated/legacy/current-date.js

@@ -282,7 +282,7 @@ describe('current date', function() {
         true // isEndExclusive
       )
     } else {
-      title = titleStart.format(TITLE_FORMAT)
+      title = currentCalendar.formatDate(titleStart, TITLE_FORMAT)
     }
 
     expect($('.fc-toolbar h2')).toContainText(title)

+ 1 - 1
tests/automated/legacy/weekViewRender.js

@@ -3,7 +3,7 @@ describe('weekViewRender', function() {
   var nowStr = '2018-05-28'
 
   pushOptions({
-    defaultDate: nowStr,
+    now: nowStr,
     defaultView: 'agendaWeek'
   })
 

+ 4 - 14
tests/automated/lib/time-grid.js

@@ -1,5 +1,5 @@
 import { getBoundingRect } from '../lib/dom-geom'
-import { formatIsoDay, formatIsoTime } from '../datelib/utils'
+import { formatIsoDay, formatIsoTime, ensureDate } from '../datelib/utils'
 
 
 export function dragTimeGridEvent(eventEl, dropDate) {
@@ -45,10 +45,7 @@ export function selectTimeGrid(start, inclusiveEnd) {
 
 
 export function getTimeGridPoint(date) {
-
-  if (typeof date === 'string') {
-    date = new Date(date)
-  }
+  date = ensureDate(date)
 
   var day = FullCalendar.startOfDay(date)
   var timeMs = date.valueOf() - day.valueOf()
@@ -67,10 +64,7 @@ export function getTimeGridPoint(date) {
 
 
 export function getTimeGridLine(date) { // not in Scheduler
-
-  if (typeof date === 'string') {
-    date = new Date(date)
-  }
+  date = ensureDate(date)
 
   var day = FullCalendar.startOfDay(date)
   var timeMs = date.valueOf() - day.valueOf()
@@ -140,11 +134,7 @@ export function getTimeGridTop(targetTimeMs) {
 
 
 export function getTimeGridDayEls(date) {
-
-  if (typeof date === 'string') {
-    date = new Date(date)
-  }
-
+  date = ensureDate(date)
   return $('.fc-time-grid .fc-day[data-date="' + formatIsoDay(date) + '"]')
 }