Skip to content

Commit

Permalink
test: Update calendar test
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Mar 24, 2020
1 parent b25be90 commit 29a7e74
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/plugin/calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export default (o, c, d) => {
sameElse: L
}
const proto = c.prototype
proto.calendar = function (referenceTime, callbacks) {
const format = callbacks || this.$locale().calendar || calendarFormat
proto.calendar = function (referenceTime, formats) {
const format = formats || this.$locale().calendar || calendarFormat
const referenceStartOfDay = d(referenceTime || undefined).startOf('d')
const diff = this.diff(referenceStartOfDay, 'd', true)
const sameElse = 'sameElse'
Expand All @@ -25,10 +25,9 @@ export default (o, c, d) => {
/* eslint-enable no-nested-ternary */
const currentFormat = format[retVal] || calendarFormat[retVal]
if (typeof currentFormat === 'function') {
return currentFormat(referenceStartOfDay)
return currentFormat.call(this, d())
}
const formattedDate = this.format(currentFormat)
return formattedDate
return this.format(currentFormat)
}
}

13 changes: 10 additions & 3 deletions test/plugin/calendar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,17 @@ it('Calls callback', () => {

it('callback is a function with the scope of the current moment', () => {
const callbacks = {
sameDay: jest.fn(),
sameElse: jest.fn()
sameDay: jest.fn()
}
expect(dayjs().calendar(null, callbacks)).toEqual(callbacks.sameDay())
const callbacks2 = {
sameDay: function cb() {
return this
}
}
const result = dayjs().calendar(null, callbacks2)
expect(result.format).not.toBeUndefined()
expect(dayjs.isDayjs(result)).toBeTruthy()
})

it('callback is a function and first argument a moment that depicts now', () => {
Expand All @@ -116,7 +123,7 @@ it('callback is a function and first argument a moment that depicts now', () =>
}
const now = dayjs()
dayjs(now).calendar(now, callbacks)
expect(callbacks.sameDay).toBeCalledWith(now.startOf('d'))
expect(callbacks.sameDay).toBeCalledWith(now)
})

it('set global calendar in locale file', () => {
Expand Down

0 comments on commit 29a7e74

Please sign in to comment.