Replace fragile moment mocks with snapshot testing

Signed-off-by: Georg Ehrke <developer@georgehrke.com>
This commit is contained in:
Georg Ehrke 2020-09-04 14:34:37 +02:00
parent fa5855bf7e
commit c6e52a879c
No known key found for this signature in database
GPG Key ID: 9D98FD9380A1CB43
8 changed files with 91 additions and 507 deletions

View File

@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`format/alarmFormat test suite should format a relative trigger before the event ends 1`] = `"{time} before the event ends"`;
exports[`format/alarmFormat test suite should format a relative trigger before the event starts 1`] = `"{time} before the event starts"`;
exports[`format/alarmFormat test suite should format an alarm for an all-day event days before 1`] = `"%n days before the event at {formattedHourMinute}"`;
exports[`format/alarmFormat test suite should format an alarm for an all-day event not supported in the default range 1`] = `"{time} after the event starts"`;
exports[`format/alarmFormat test suite should format an alarm for an all-day event on the same day at a certain time 1`] = `"on the day of the event at {formattedHourMinute}"`;
exports[`format/alarmFormat test suite should format an alarm for an all-day event weeks weeks before 1`] = `"%n weeks before the event at {formattedHourMinute}"`;

View File

@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`format/dateFormat test suite should format a timed date 1`] = `"1. Jan. 2019 00:00"`;
exports[`format/dateFormat test suite should format an all-day date 1`] = `"1. Jan. 2019"`;

View File

@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`format/dateRangeFormat test suite should provide a format for day view 1`] = `"1. Jan. 2019"`;
exports[`format/dateRangeFormat test suite should provide a format for month view 1`] = `"Januar 2019"`;
exports[`format/dateRangeFormat test suite should provide a format for week view 1`] = `"Week {number} of {year}"`;
exports[`format/dateRangeFormat test suite should provide month as fallback for unknown view 1`] = `"Januar 2019"`;

View File

@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is non-recurring 1`] = `"Does not repeat"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring every day exactly 10 times 1`] = `"Daily %n times"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring every day until a certain date 1`] = `"Daily until {untilDate}"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring infinitely every day 1`] = `"Daily"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring infinitely every month on 15th, 16th, 17th, 18th 1`] = `"Monthly on days {dayOfMonthList}"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring infinitely every month on last weekday 1`] = `"Monthly on the {ordinalNumber} {byDaySet}"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring infinitely every month second Wednesday 1`] = `"Monthly on the {ordinalNumber} {byDaySet}"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring infinitely every other month on 15th 1`] = `"Every %n months on days {dayOfMonthList}"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring infinitely every third week on Tuesday and Thursday 1`] = `"Every %n weeks on {weekdays}"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring infinitely every week on Tuesday 1`] = `"Weekly on {weekdays}"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring infinitely every year in May 1`] = `"Yearly in {monthNames}"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring infinitely every year in May, July, October 1`] = `"Yearly in {monthNames}"`;
exports[`format/recurrenceRuleFormat test suite should format a recurrence-rule that is recurring infinitely every year in May, July, October on third Thursday 1`] = `"Yearly in {monthNames} on the {ordinalNumber} {byDaySet}"`;

View File

@ -20,16 +20,13 @@
*
*/
import alarmFormat from "../../../../src/filters/alarmFormat.js";
import moment from '@nextcloud/moment'
import { translate, translatePlural } from '@nextcloud/l10n'
jest.mock('@nextcloud/moment')
jest.mock('@nextcloud/l10n')
describe('format/alarmFormat test suite', () => {
beforeEach(() => {
moment.mockClear()
translate.mockClear()
translatePlural.mockClear()
@ -58,21 +55,9 @@ describe('format/alarmFormat test suite', () => {
}
expect(alarmFormat(alarm, true, 'Europe/Berlin', 'de')).toEqual('Midnight on the day the event starts')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Midnight on the day the event starts')
expect(translatePlural).toHaveBeenCalledTimes(0)
})
it('should format an alarm for an all-day event days before', () => {
const format = jest.fn()
.mockReturnValue('formatted-LT')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
const alarm = {
type: 'EMAIL',
isRelative: true,
@ -89,38 +74,10 @@ describe('format/alarmFormat test suite', () => {
relativeTrigger: -15 * 60 * 60,
}
expect(alarmFormat(alarm, true, 'Europe/Berlin', 'de')).toEqual('%n days before the event at {formattedHourMinute}')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment.mock.calls[0][0].getHours()).toEqual(9)
expect(moment.mock.calls[0][0].getMinutes()).toEqual(0)
expect(moment.mock.calls[0][0].getSeconds()).toEqual(0)
expect(moment.mock.calls[0][0].getMilliseconds()).toEqual(0)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'LT')
expect(translate).toHaveBeenCalledTimes(0)
expect(translatePlural).toHaveBeenCalledTimes(1)
expect(translatePlural).toHaveBeenNthCalledWith(1,
'calendar',
'%n day before the event at {formattedHourMinute}',
'%n days before the event at {formattedHourMinute}',
1,
{
formattedHourMinute: 'formatted-LT',
},
)
expect(alarmFormat(alarm, true, 'Europe/Berlin', 'de')).toMatchSnapshot()
})
it('should format an alarm for an all-day event weeks weeks before', () => {
const format = jest.fn()
.mockReturnValue('formatted-LT')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
const alarm = {
type: 'EMAIL',
isRelative: true,
@ -137,38 +94,10 @@ describe('format/alarmFormat test suite', () => {
relativeTrigger: -159 * 60 * 60 - 30 * 60,
}
expect(alarmFormat(alarm, true, 'Europe/Berlin', 'de')).toEqual('%n weeks before the event at {formattedHourMinute}')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment.mock.calls[0][0].getHours()).toEqual(9)
expect(moment.mock.calls[0][0].getMinutes()).toEqual(0)
expect(moment.mock.calls[0][0].getSeconds()).toEqual(0)
expect(moment.mock.calls[0][0].getMilliseconds()).toEqual(0)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'LT')
expect(translate).toHaveBeenCalledTimes(0)
expect(translatePlural).toHaveBeenCalledTimes(1)
expect(translatePlural).toHaveBeenNthCalledWith(1,
'calendar',
'%n week before the event at {formattedHourMinute}',
'%n weeks before the event at {formattedHourMinute}',
1,
{
formattedHourMinute: 'formatted-LT',
},
)
expect(alarmFormat(alarm, true, 'Europe/Berlin', 'de')).toMatchSnapshot()
})
it('should format an alarm for an all-day event on the same day at a certain time', () => {
const format = jest.fn()
.mockReturnValue('formatted-LT')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
const alarm = {
type: 'EMAIL',
isRelative: true,
@ -185,36 +114,10 @@ describe('format/alarmFormat test suite', () => {
relativeTrigger: 32400,
}
expect(alarmFormat(alarm, true, 'Europe/Berlin', 'de')).toEqual('on the day of the event at {formattedHourMinute}')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment.mock.calls[0][0].getHours()).toEqual(9)
expect(moment.mock.calls[0][0].getMinutes()).toEqual(0)
expect(moment.mock.calls[0][0].getSeconds()).toEqual(0)
expect(moment.mock.calls[0][0].getMilliseconds()).toEqual(0)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'LT')
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1,
'calendar',
'on the day of the event at {formattedHourMinute}',
{
formattedHourMinute: 'formatted-LT',
},
)
expect(translatePlural).toHaveBeenCalledTimes(0)
expect(alarmFormat(alarm, true, 'Europe/Berlin', 'de')).toMatchSnapshot()
})
it('should format an alarm for an all-day event not supported in the default range', () => {
const humanize = jest.fn()
.mockReturnValue('humanized-time')
const locale = jest.fn()
.mockReturnValue({ humanize })
moment.duration = jest.fn()
.mockReturnValue({ locale })
const alarm = {
type: 'EMAIL',
isRelative: true,
@ -231,20 +134,7 @@ describe('format/alarmFormat test suite', () => {
relativeTrigger: 118800,
}
expect(alarmFormat(alarm, true, 'Europe/Berlin', 'de')).toEqual('{time} after the event starts')
expect(moment).toHaveBeenCalledTimes(0)
expect(moment.duration).toHaveBeenCalledTimes(1)
expect(moment.duration).toHaveBeenNthCalledWith(1, 118800, 'seconds')
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(humanize).toHaveBeenCalledTimes(1)
expect(humanize).toHaveBeenNthCalledWith(1)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', '{time} after the event starts', {
time: 'humanized-time',
})
expect(translatePlural).toHaveBeenCalledTimes(0)
expect(alarmFormat(alarm, true, 'Europe/Berlin', 'de')).toMatchSnapshot()
})
it('should format a relative trigger at the events start', () => {
@ -265,11 +155,6 @@ describe('format/alarmFormat test suite', () => {
}
expect(alarmFormat(alarm, false, 'Europe/Berlin', 'de')).toEqual('at the event\'s start')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'at the event\'s start')
expect(translatePlural).toHaveBeenCalledTimes(0)
})
it('should format a relative trigger at the events end', () => {
@ -290,21 +175,9 @@ describe('format/alarmFormat test suite', () => {
}
expect(alarmFormat(alarm, false, 'Europe/Berlin', 'de')).toEqual('at the event\'s end')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'at the event\'s end')
expect(translatePlural).toHaveBeenCalledTimes(0)
})
it('should format a relative trigger before the event starts', () => {
const humanize = jest.fn()
.mockReturnValue('humanized-time')
const locale = jest.fn()
.mockReturnValue({ humanize })
moment.duration = jest.fn()
.mockReturnValue({ locale })
const alarm = {
type: 'EMAIL',
isRelative: true,
@ -321,30 +194,10 @@ describe('format/alarmFormat test suite', () => {
relativeTrigger: -900,
}
expect(alarmFormat(alarm, false, 'Europe/Berlin', 'de')).toEqual('{time} before the event starts')
expect(moment).toHaveBeenCalledTimes(0)
expect(moment.duration).toHaveBeenCalledTimes(1)
expect(moment.duration).toHaveBeenNthCalledWith(1, 900, 'seconds')
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(humanize).toHaveBeenCalledTimes(1)
expect(humanize).toHaveBeenNthCalledWith(1)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', '{time} before the event starts', {
time: 'humanized-time',
})
expect(translatePlural).toHaveBeenCalledTimes(0)
expect(alarmFormat(alarm, false, 'Europe/Berlin', 'de')).toMatchSnapshot()
})
it('should format a relative trigger before the event ends', () => {
const humanize = jest.fn()
.mockReturnValue('humanized-time')
const locale = jest.fn()
.mockReturnValue({ humanize })
moment.duration = jest.fn()
.mockReturnValue({ locale })
const alarm = {
type: 'EMAIL',
isRelative: true,
@ -361,30 +214,10 @@ describe('format/alarmFormat test suite', () => {
relativeTrigger: -900,
}
expect(alarmFormat(alarm, false, 'Europe/Berlin', 'de')).toEqual('{time} before the event ends')
expect(moment).toHaveBeenCalledTimes(0)
expect(moment.duration).toHaveBeenCalledTimes(1)
expect(moment.duration).toHaveBeenNthCalledWith(1, 900, 'seconds')
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(humanize).toHaveBeenCalledTimes(1)
expect(humanize).toHaveBeenNthCalledWith(1)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', '{time} before the event ends', {
time: 'humanized-time',
})
expect(translatePlural).toHaveBeenCalledTimes(0)
expect(alarmFormat(alarm, false, 'Europe/Berlin', 'de')).toMatchSnapshot()
})
it('should format a relative trigger after the event starts', () => {
const humanize = jest.fn()
.mockReturnValue('humanized-time')
const locale = jest.fn()
.mockReturnValue({ humanize })
moment.duration = jest.fn()
.mockReturnValue({ locale })
const alarm = {
type: 'EMAIL',
isRelative: true,
@ -402,29 +235,9 @@ describe('format/alarmFormat test suite', () => {
}
expect(alarmFormat(alarm, false, 'Europe/Berlin', 'de')).toEqual('{time} after the event starts')
expect(moment).toHaveBeenCalledTimes(0)
expect(moment.duration).toHaveBeenCalledTimes(1)
expect(moment.duration).toHaveBeenNthCalledWith(1, 900, 'seconds')
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(humanize).toHaveBeenCalledTimes(1)
expect(humanize).toHaveBeenNthCalledWith(1)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', '{time} after the event starts', {
time: 'humanized-time',
})
expect(translatePlural).toHaveBeenCalledTimes(0)
})
it('should format a relative trigger after the event ends', () => {
const humanize = jest.fn()
.mockReturnValue('humanized-time')
const locale = jest.fn()
.mockReturnValue({ humanize })
moment.duration = jest.fn()
.mockReturnValue({ locale })
const alarm = {
type: 'EMAIL',
isRelative: true,
@ -442,29 +255,9 @@ describe('format/alarmFormat test suite', () => {
}
expect(alarmFormat(alarm, false, 'Europe/Berlin', 'de')).toEqual('{time} after the event ends')
expect(moment).toHaveBeenCalledTimes(0)
expect(moment.duration).toHaveBeenCalledTimes(1)
expect(moment.duration).toHaveBeenNthCalledWith(1, 900, 'seconds')
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(humanize).toHaveBeenCalledTimes(1)
expect(humanize).toHaveBeenNthCalledWith(1)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', '{time} after the event ends', {
time: 'humanized-time',
})
expect(translatePlural).toHaveBeenCalledTimes(0)
})
it('should format an absolute alarm in the user\'s timezone', () => {
const format = jest.fn()
.mockReturnValue('formatted-LLLL')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
const date = new Date(2019, 0, 1, 0, 0, 0, 0)
const alarm = {
type: 'EMAIL',
@ -483,28 +276,9 @@ describe('format/alarmFormat test suite', () => {
}
expect(alarmFormat(alarm, false, 'Europe/Berlin', 'de')).toEqual('on {time}')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment).toHaveBeenNthCalledWith(1, date)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'LLLL')
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'on {time}', {
time: 'formatted-LLLL'
})
expect(translatePlural).toHaveBeenCalledTimes(0)
})
it('should format an absolute alarm in a different timezone', () => {
const format = jest.fn()
.mockReturnValue('formatted-LLLL')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
const date = new Date(2019, 0, 1, 0, 0, 0, 0)
const alarm = {
type: 'EMAIL',
@ -523,19 +297,6 @@ describe('format/alarmFormat test suite', () => {
}
expect(alarmFormat(alarm, true, 'Europe/Berlin', 'de')).toEqual('on {time} ({timezoneId})')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment).toHaveBeenNthCalledWith(1, date)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'LLLL')
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'on {time} ({timezoneId})', {
time: 'formatted-LLLL',
timezoneId: 'America/New_York',
})
expect(translatePlural).toHaveBeenCalledTimes(0)
})
})

View File

@ -20,54 +20,17 @@
*
*/
import dateFormat from "../../../../src/filters/dateFormat.js";
import moment from '@nextcloud/moment'
jest.mock('@nextcloud/moment')
describe('format/dateFormat test suite', () => {
beforeEach(() => {
moment.mockClear()
})
it('should format an all-day date', () => {
const date = new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))
const format = jest.fn()
.mockReturnValue('formatted-allday-date')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
expect(dateFormat(date, true, 'de')).toEqual('formatted-allday-date')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment).toHaveBeenNthCalledWith(1, date)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'll')
const date = new Date(2019, 0, 1, 0, 0, 0, 0)
expect(dateFormat(date, true, 'de')).toMatchSnapshot()
})
it('should format a timed date', () => {
const date = new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))
const format = jest.fn()
.mockReturnValue('formatted-allday-date')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
expect(dateFormat(date, false, 'de')).toEqual('formatted-allday-date')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment).toHaveBeenNthCalledWith(1, date)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'lll')
const date = new Date(2019, 0, 1, 0, 0, 0, 0)
expect(dateFormat(date, false, 'de')).toMatchSnapshot()
})
})

View File

@ -20,119 +20,39 @@
*
*/
import moment from '@nextcloud/moment'
import dateRangeFormat from "../../../../src/filters/dateRangeFormat.js";
import { translate } from '@nextcloud/l10n'
jest.mock('@nextcloud/moment')
jest.mock('@nextcloud/l10n')
describe('format/dateRangeFormat test suite', () => {
beforeEach(() => {
moment.mockClear()
translate.mockClear()
translate
.mockImplementation((app, str) => str)
})
it('should provide a format for day view', () => {
const date = new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))
const format = jest.fn()
.mockReturnValue('formatted-allday-date')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
expect(dateRangeFormat(date, 'timeGridDay', 'de')).toEqual('formatted-allday-date')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment).toHaveBeenNthCalledWith(1, date)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'll')
expect(translate).toHaveBeenCalledTimes(0)
const date = new Date(2019, 0, 1, 0, 0, 0, 0)
expect(dateRangeFormat(date, 'timeGridDay', 'de')).toMatchSnapshot()
})
it('should provide a format for week view', () => {
translate
.mockImplementation((app, str) => str)
const date = new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))
const week = jest.fn()
.mockReturnValue('week-no')
const weekYear = jest.fn()
.mockReturnValue('week-year')
const locale = jest.fn()
.mockReturnValue({ week, weekYear })
moment
.mockReturnValue({ locale })
expect(dateRangeFormat(date, 'timeGridWeek', 'de')).toEqual('Week {number} of {year}')
expect(moment).toHaveBeenCalledTimes(2)
expect(moment).toHaveBeenNthCalledWith(1, date)
expect(moment).toHaveBeenNthCalledWith(2, date)
expect(locale).toHaveBeenCalledTimes(2)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(locale).toHaveBeenNthCalledWith(2, 'de')
expect(week).toHaveBeenCalledTimes(1)
expect(week).toHaveBeenNthCalledWith(1)
expect(weekYear).toHaveBeenCalledTimes(1)
expect(weekYear).toHaveBeenNthCalledWith(1)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Week {number} of {year}', {
number: 'week-no',
year: 'week-year',
})
const date = new Date(2019, 0, 1, 0, 0, 0, 0)
expect(dateRangeFormat(date, 'timeGridWeek', 'de')).toMatchSnapshot()
})
it('should provide a format for month view', () => {
const date = new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))
const format = jest.fn()
.mockReturnValue('formatted-allday-month-year')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
expect(dateRangeFormat(date, 'dayGridMonth', 'de')).toEqual('formatted-allday-month-year')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment).toHaveBeenNthCalledWith(1, date)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'MMMM YYYY')
expect(translate).toHaveBeenCalledTimes(0)
const date = new Date(2019, 0, 1, 0, 0, 0, 0)
expect(dateRangeFormat(date, 'dayGridMonth', 'de')).toMatchSnapshot()
})
it('should provide month as fallback for unknown view', () => {
const date = new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))
const format = jest.fn()
.mockReturnValue('formatted-allday-month-year')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
expect(dateRangeFormat(date, 'fooBarUnknownView', 'de')).toEqual('formatted-allday-month-year')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment).toHaveBeenNthCalledWith(1, date)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'MMMM YYYY')
expect(translate).toHaveBeenCalledTimes(0)
const date = new Date(2019, 0, 1, 0, 0, 0, 0)
expect(dateRangeFormat(date, 'fooBarUnknownView', 'de')).toMatchSnapshot()
})
})

View File

@ -20,17 +20,13 @@
*
*/
import recurrenceRuleFormat from '../../../../src/filters/recurrenceRuleFormat.js'
import moment from '@nextcloud/moment'
import { translate, translatePlural, getDayNames, getMonthNames } from '@nextcloud/l10n'
jest.mock('@nextcloud/moment')
jest.mock('@nextcloud/l10n')
describe('format/recurrenceRuleFormat test suite', () => {
beforeEach(() => {
moment.mockClear()
translate.mockClear()
translatePlural.mockClear()
@ -56,11 +52,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: null,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Does not repeat')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Does not repeat')
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring infinitely every day', () => {
@ -75,11 +67,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: null,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Daily')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Daily')
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring infinitely every week on Tuesday', () => {
@ -94,16 +82,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: null,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Weekly on {weekdays}')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Weekly')
expect(translatePlural).toHaveBeenCalledTimes(1)
expect(translatePlural).toHaveBeenNthCalledWith(1, 'calendar', 'on {weekday}', 'on {weekdays}', 1, {
weekday: 'Tuesday',
weekdays: 'Tuesday',
})
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring infinitely every third week on Tuesday and Thursday', () => {
@ -118,16 +97,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: null,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Every %n weeks on {weekdays}')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(0)
expect(translatePlural).toHaveBeenCalledTimes(2)
expect(translatePlural).toHaveBeenNthCalledWith(1, 'calendar', 'Every %n week', 'Every %n weeks', 3)
expect(translatePlural).toHaveBeenNthCalledWith(2, 'calendar', 'on {weekday}', 'on {weekdays}', 2, {
weekday: 'Tuesday, Thursday',
weekdays: 'Tuesday, Thursday',
})
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring infinitely every other month on 15th', () => {
@ -142,15 +112,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: null,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Every %n months on days {dayOfMonthList}')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(0)
expect(translatePlural).toHaveBeenCalledTimes(2)
expect(translatePlural).toHaveBeenNthCalledWith(1, 'calendar', 'Every %n month', 'Every %n months', 2)
expect(translatePlural).toHaveBeenNthCalledWith(2, 'calendar', 'on day {dayOfMonthList}', 'on days {dayOfMonthList}', 1, {
dayOfMonthList: '15',
})
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring infinitely every month on 15th, 16th, 17th, 18th', () => {
@ -165,15 +127,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: null,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Monthly on days {dayOfMonthList}')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Monthly')
expect(translatePlural).toHaveBeenCalledTimes(1)
expect(translatePlural).toHaveBeenNthCalledWith(1, 'calendar', 'on day {dayOfMonthList}', 'on days {dayOfMonthList}', 4, {
dayOfMonthList: '15, 16, 17, 18',
})
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring infinitely every month on last weekday', () => {
@ -188,16 +142,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: -1,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Monthly on the {ordinalNumber} {byDaySet}')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(3)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Monthly')
expect(translate).toHaveBeenNthCalledWith(2, 'calendar', 'last')
expect(translate).toHaveBeenNthCalledWith(3, 'calendar', 'on the {ordinalNumber} {byDaySet}', {
ordinalNumber: 'last',
byDaySet: 'Monday, Tuesday, Wednesday, Thursday, Friday',
})
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring infinitely every month second Wednesday', () => {
@ -212,16 +157,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: 2,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Monthly on the {ordinalNumber} {byDaySet}')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(3)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Monthly')
expect(translate).toHaveBeenNthCalledWith(2, 'calendar', 'second')
expect(translate).toHaveBeenNthCalledWith(3, 'calendar', 'on the {ordinalNumber} {byDaySet}', {
ordinalNumber: 'second',
byDaySet: 'Wednesday',
})
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring infinitely every year in May', () => {
@ -236,14 +172,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: null,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Yearly in {monthNames}')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(2)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Yearly')
expect(translate).toHaveBeenNthCalledWith(2, 'calendar', 'in {monthNames}', {
monthNames: 'May'
})
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring infinitely every year in May, July, October', () => {
@ -258,14 +187,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: null,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Yearly in {monthNames}')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(2)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Yearly')
expect(translate).toHaveBeenNthCalledWith(2, 'calendar', 'in {monthNames}', {
monthNames: 'May, July, October'
})
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring infinitely every year in May, July, October on third Thursday', () => {
@ -280,29 +202,11 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: 3,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Yearly in {monthNames} on the {ordinalNumber} {byDaySet}')
expect(moment).toHaveBeenCalledTimes(0)
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(3)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Yearly')
expect(translate).toHaveBeenNthCalledWith(2, 'calendar', 'third')
expect(translate).toHaveBeenNthCalledWith(3, 'calendar', 'in {monthNames} on the {ordinalNumber} {byDaySet}', {
monthNames: 'May, July, October',
ordinalNumber: 'third',
byDaySet: 'Thursday'
})
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring every day until a certain date', () => {
const date = new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))
const format = jest.fn()
.mockReturnValue('formatted-allday-date')
const locale = jest.fn()
.mockReturnValue({ format })
moment
.mockReturnValue({ locale })
const date = new Date(2019, 0, 1, 0, 0, 0, 0)
expect(recurrenceRuleFormat({
frequency: 'DAILY',
@ -315,19 +219,7 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: null,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Daily until {untilDate}')
expect(moment).toHaveBeenCalledTimes(1)
expect(moment).toHaveBeenNthCalledWith(1, date)
expect(locale).toHaveBeenCalledTimes(1)
expect(locale).toHaveBeenNthCalledWith(1, 'de')
expect(format).toHaveBeenCalledTimes(1)
expect(format).toHaveBeenNthCalledWith(1, 'L')
expect(translate).toHaveBeenCalledTimes(2)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Daily')
expect(translate).toHaveBeenNthCalledWith(2, 'calendar', 'until {untilDate}', {
untilDate: 'formatted-allday-date'
})
}, 'de')).toMatchSnapshot()
})
it('should format a recurrence-rule that is recurring every day exactly 10 times', () => {
@ -342,12 +234,6 @@ describe('format/recurrenceRuleFormat test suite', () => {
bySetPosition: null,
isUnsupported: false,
recurrenceRuleValue: null,
}, 'de')).toEqual('Daily %n times')
expect(moment).toHaveBeenCalledTimes(0)
expect(translate).toHaveBeenCalledTimes(1)
expect(translate).toHaveBeenNthCalledWith(1, 'calendar', 'Daily')
expect(translatePlural).toHaveBeenCalledTimes(1)
expect(translatePlural).toHaveBeenNthCalledWith(1, 'calendar', '%n time', '%n times', 42)
}, 'de')).toMatchSnapshot()
})
})