Skip to content

Commit

Permalink
Fix remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
st3iny committed May 24, 2024
1 parent 0e9058a commit 7c28348
Show file tree
Hide file tree
Showing 6 changed files with 431 additions and 295 deletions.
162 changes: 91 additions & 71 deletions tests/javascript/unit/fullcalendar/eventSources/eventSource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @copyright Copyright (c) 2019 Georg Ehrke
*
* @author Georg Ehrke <[email protected]>
* @author 2024 Richard Steinmetz <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -25,11 +26,15 @@ import { generateTextColorForHex } from '../../../../../src/utils/color.js'
import getTimezoneManager from '../../../../../src/services/timezoneDataProviderService'
import { getUnixTimestampFromDate } from '../../../../../src/utils/date.js'
import { eventSourceFunction } from '../../../../../src/fullcalendar/eventSources/eventSourceFunction.js'
import useFetchedTimeRangesStore from '../../../../../src/store/fetchedTimeRanges.js'
import useCalendarsStore from '../../../../../src/store/calendars.js'

jest.mock('../../../../../src/utils/color.js')
jest.mock('../../../../../src/services/timezoneDataProviderService')
jest.mock('../../../../../src/utils/date.js')
jest.mock('../../../../../src/fullcalendar/eventSources/eventSourceFunction.js')
jest.mock('../../../../../src/store/fetchedTimeRanges.js')
jest.mock('../../../../../src/store/calendars.js')

describe('fullcalendar/eventSource test suite', () => {

Expand All @@ -38,10 +43,15 @@ describe('fullcalendar/eventSource test suite', () => {
getTimezoneManager.mockClear()
getUnixTimestampFromDate.mockClear()
eventSourceFunction.mockClear()
useFetchedTimeRangesStore.mockClear()
useCalendarsStore.mockClear()
})

it('should provide an eventSource for a given calendar', () => {
const store = {}
const calendarsStore = {}
useCalendarsStore.mockReturnValue(calendarsStore)
const fetchedTimeRangesStore = {}
useFetchedTimeRangesStore.mockReturnValue(fetchedTimeRangesStore)
const calendar = {
id: 'calendar-id-123',
color: '#ff00ff',
Expand All @@ -51,7 +61,7 @@ describe('fullcalendar/eventSource test suite', () => {
generateTextColorForHex
.mockReturnValue('#00ff00')

const eventSourceFunction = eventSource(store)
const eventSourceFunction = eventSource()
expect(eventSourceFunction(calendar)).toEqual({
id: 'calendar-id-123',
backgroundColor: '#ff00ff',
Expand All @@ -65,7 +75,10 @@ describe('fullcalendar/eventSource test suite', () => {
})

it('should provide an eventSource for a given read-only calendar', () => {
const store = {}
const calendarsStore = {}
useCalendarsStore.mockReturnValue(calendarsStore)
const fetchedTimeRangesStore = {}
useFetchedTimeRangesStore.mockReturnValue(fetchedTimeRangesStore)
const calendar = {
id: 'calendar-id-123',
color: '#ff00ff',
Expand All @@ -75,7 +88,7 @@ describe('fullcalendar/eventSource test suite', () => {
generateTextColorForHex
.mockReturnValue('#00ff00')

const eventSourceFunction = eventSource(store)
const eventSourceFunction = eventSource()
expect(eventSourceFunction(calendar)).toEqual({
id: 'calendar-id-123',
backgroundColor: '#ff00ff',
Expand All @@ -90,16 +103,18 @@ describe('fullcalendar/eventSource test suite', () => {
})

it('should provide an eventSource function to provide events - fetch new timerange', async () => {
const store = {
dispatch: jest.fn()
.mockReturnValueOnce(42),
getters: {
getTimeRangeForCalendarCoveringRange: jest.fn()
.mockReturnValueOnce(false),
getCalendarObjectsByTimeRangeId: jest.fn()
.mockReturnValueOnce([{ calendarObjectId: 1 }, { calendarObjectId: 2 }])
}
const calendarsStore = {
getEventsFromCalendarInTimeRange: jest.fn()
.mockResolvedValueOnce(42),
}
useCalendarsStore.mockReturnValue(calendarsStore)
const fetchedTimeRangesStore = {
getTimeRangeForCalendarCoveringRange: jest.fn()
.mockReturnValueOnce(false),
getCalendarObjectsByTimeRangeId: jest.fn()
.mockReturnValueOnce([{ calendarObjectId: 1 }, { calendarObjectId: 2 }])
}
useFetchedTimeRangesStore.mockReturnValue(fetchedTimeRangesStore)

const calendar = {
id: 'calendar-id-123',
Expand Down Expand Up @@ -131,22 +146,22 @@ describe('fullcalendar/eventSource test suite', () => {
const successCallback = jest.fn()
const failureCallback = jest.fn()

const eventSourceFn = eventSource(store)
const eventSourceFn = eventSource()
const { events } = eventSourceFn(calendar)
await events({ start, end, timeZone }, successCallback, failureCallback)

expect(store.getters.getTimeRangeForCalendarCoveringRange).toHaveBeenCalledTimes(1)
expect(store.getters.getTimeRangeForCalendarCoveringRange).toHaveBeenNthCalledWith(1, 'calendar-id-123', 1234, 5678)
expect(fetchedTimeRangesStore.getTimeRangeForCalendarCoveringRange).toHaveBeenCalledTimes(1)
expect(fetchedTimeRangesStore.getTimeRangeForCalendarCoveringRange).toHaveBeenNthCalledWith(1, 'calendar-id-123', 1234, 5678)

expect(store.dispatch).toHaveBeenCalledTimes(1)
expect(store.dispatch).toHaveBeenNthCalledWith(1, 'getEventsFromCalendarInTimeRange', {
expect(calendarsStore.getEventsFromCalendarInTimeRange).toHaveBeenCalledTimes(1)
expect(calendarsStore.getEventsFromCalendarInTimeRange).toHaveBeenNthCalledWith(1, {
calendar,
from: start,
to: end
})

expect(store.getters.getCalendarObjectsByTimeRangeId).toHaveBeenCalledTimes(1)
expect(store.getters.getCalendarObjectsByTimeRangeId).toHaveBeenNthCalledWith(1, 42)
expect(fetchedTimeRangesStore.getCalendarObjectsByTimeRangeId).toHaveBeenCalledTimes(1)
expect(fetchedTimeRangesStore.getCalendarObjectsByTimeRangeId).toHaveBeenNthCalledWith(1, 42)

expect(eventSourceFunction).toHaveBeenCalledTimes(1)
expect(eventSourceFunction).toHaveBeenNthCalledWith(1, [{ calendarObjectId: 1 }, { calendarObjectId: 2 }], calendar, start, end, { calendarJsTimezone: true, tzid: 'America/New_York' })
Expand All @@ -158,18 +173,20 @@ describe('fullcalendar/eventSource test suite', () => {
})

it('should provide an eventSource function to provide events - existing timerange', async () => {
const store = {
dispatch: jest.fn()
.mockReturnValueOnce(42),
getters: {
getTimeRangeForCalendarCoveringRange: jest.fn()
.mockReturnValueOnce({
id: 42
}),
getCalendarObjectsByTimeRangeId: jest.fn()
.mockReturnValueOnce([{ calendarObjectId: 1 }, { calendarObjectId: 2 }])
}
const calendarsStore = {
getEventsFromCalendarInTimeRange: jest.fn()
.mockResolvedValueOnce(42),
}
useCalendarsStore.mockReturnValue(calendarsStore)
const fetchedTimeRangesStore = {
getTimeRangeForCalendarCoveringRange: jest.fn()
.mockReturnValueOnce({
id: 42,
}),
getCalendarObjectsByTimeRangeId: jest.fn()
.mockReturnValueOnce([{ calendarObjectId: 1 }, { calendarObjectId: 2 }])
}
useFetchedTimeRangesStore.mockReturnValue(fetchedTimeRangesStore)

const calendar = {
id: 'calendar-id-123',
Expand Down Expand Up @@ -201,17 +218,17 @@ describe('fullcalendar/eventSource test suite', () => {
const successCallback = jest.fn()
const failureCallback = jest.fn()

const eventSourceFn = eventSource(store)
const eventSourceFn = eventSource()
const { events } = eventSourceFn(calendar)
await events({ start, end, timeZone }, successCallback, failureCallback)

expect(store.getters.getTimeRangeForCalendarCoveringRange).toHaveBeenCalledTimes(1)
expect(store.getters.getTimeRangeForCalendarCoveringRange).toHaveBeenNthCalledWith(1, 'calendar-id-123', 1234, 5678)
expect(fetchedTimeRangesStore.getTimeRangeForCalendarCoveringRange).toHaveBeenCalledTimes(1)
expect(fetchedTimeRangesStore.getTimeRangeForCalendarCoveringRange).toHaveBeenNthCalledWith(1, 'calendar-id-123', 1234, 5678)

expect(store.dispatch).toHaveBeenCalledTimes(0)
expect(calendarsStore.getEventsFromCalendarInTimeRange).toHaveBeenCalledTimes(0)

expect(store.getters.getCalendarObjectsByTimeRangeId).toHaveBeenCalledTimes(1)
expect(store.getters.getCalendarObjectsByTimeRangeId).toHaveBeenNthCalledWith(1, 42)
expect(fetchedTimeRangesStore.getCalendarObjectsByTimeRangeId).toHaveBeenCalledTimes(1)
expect(fetchedTimeRangesStore.getCalendarObjectsByTimeRangeId).toHaveBeenNthCalledWith(1, 42)

expect(eventSourceFunction).toHaveBeenCalledTimes(1)
expect(eventSourceFunction).toHaveBeenNthCalledWith(1, [{ calendarObjectId: 1 }, { calendarObjectId: 2 }], calendar, start, end, { calendarJsTimezone: true, tzid: 'America/New_York' })
Expand All @@ -223,18 +240,20 @@ describe('fullcalendar/eventSource test suite', () => {
})

it('should provide an eventSource function to provide events - existing timerange - unknown timezone', async () => {
const store = {
dispatch: jest.fn()
.mockReturnValueOnce(42),
getters: {
getTimeRangeForCalendarCoveringRange: jest.fn()
.mockReturnValueOnce({
id: 42
}),
getCalendarObjectsByTimeRangeId: jest.fn()
.mockReturnValueOnce([{ calendarObjectId: 1 }, { calendarObjectId: 2 }])
}
const calendarsStore = {
getEventsFromCalendarInTimeRange: jest.fn()
.mockResolvedValueOnce(42),
}
useCalendarsStore.mockReturnValue(calendarsStore)
const fetchedTimeRangesStore = {
getTimeRangeForCalendarCoveringRange: jest.fn()
.mockReturnValueOnce({
id: 42,
}),
getCalendarObjectsByTimeRangeId: jest.fn()
.mockReturnValueOnce([{ calendarObjectId: 1 }, { calendarObjectId: 2 }])
}
useFetchedTimeRangesStore.mockReturnValue(fetchedTimeRangesStore)

const calendar = {
id: 'calendar-id-123',
Expand Down Expand Up @@ -267,21 +286,21 @@ describe('fullcalendar/eventSource test suite', () => {
const successCallback = jest.fn()
const failureCallback = jest.fn()

const eventSourceFn = eventSource(store)
const eventSourceFn = eventSource()
const { events } = eventSourceFn(calendar)
await events({ start, end, timeZone }, successCallback, failureCallback)

expect(getTimezoneForId).toHaveBeenCalledTimes(2)
expect(getTimezoneForId).toHaveBeenNthCalledWith(1, 'America/New_York')
expect(getTimezoneForId).toHaveBeenNthCalledWith(2, 'UTC')

expect(store.getters.getTimeRangeForCalendarCoveringRange).toHaveBeenCalledTimes(1)
expect(store.getters.getTimeRangeForCalendarCoveringRange).toHaveBeenNthCalledWith(1, 'calendar-id-123', 1234, 5678)
expect(fetchedTimeRangesStore.getTimeRangeForCalendarCoveringRange).toHaveBeenCalledTimes(1)
expect(fetchedTimeRangesStore.getTimeRangeForCalendarCoveringRange).toHaveBeenNthCalledWith(1, 'calendar-id-123', 1234, 5678)

expect(store.dispatch).toHaveBeenCalledTimes(0)
expect(calendarsStore.getEventsFromCalendarInTimeRange).toHaveBeenCalledTimes(0)

expect(store.getters.getCalendarObjectsByTimeRangeId).toHaveBeenCalledTimes(1)
expect(store.getters.getCalendarObjectsByTimeRangeId).toHaveBeenNthCalledWith(1, 42)
expect(fetchedTimeRangesStore.getCalendarObjectsByTimeRangeId).toHaveBeenCalledTimes(1)
expect(fetchedTimeRangesStore.getCalendarObjectsByTimeRangeId).toHaveBeenNthCalledWith(1, 42)

expect(eventSourceFunction).toHaveBeenCalledTimes(1)
expect(eventSourceFunction).toHaveBeenNthCalledWith(1, [{ calendarObjectId: 1 }, { calendarObjectId: 2 }], calendar, start, end, { calendarJsTimezone: true, tzid: 'UTC' })
Expand All @@ -293,18 +312,18 @@ describe('fullcalendar/eventSource test suite', () => {
})

it('should provide an eventSource function that catches errors while fetching', async () => {
const store = {
dispatch: jest.fn()
.mockImplementationOnce(() => {
throw new Error()
}),
getters: {
getTimeRangeForCalendarCoveringRange: jest.fn()
.mockReturnValueOnce(false),
getCalendarObjectsByTimeRangeId: jest.fn()
.mockReturnValueOnce([{ calendarObjectId: 1 }, { calendarObjectId: 2 }])
}
const calendarsStore = {
getEventsFromCalendarInTimeRange: jest.fn()
.mockRejectedValueOnce(new Error()),
}
useCalendarsStore.mockReturnValue(calendarsStore)
const fetchedTimeRangesStore = {
getTimeRangeForCalendarCoveringRange: jest.fn()
.mockReturnValueOnce(false),
getCalendarObjectsByTimeRangeId: jest.fn()
.mockReturnValueOnce([{ calendarObjectId: 1 }, { calendarObjectId: 2 }])
}
useFetchedTimeRangesStore.mockReturnValue(fetchedTimeRangesStore)

const calendar = {
id: 'calendar-id-123',
Expand Down Expand Up @@ -336,21 +355,22 @@ describe('fullcalendar/eventSource test suite', () => {
const successCallback = jest.fn()
const failureCallback = jest.fn()

const eventSourceFn = eventSource(store)
const eventSourceFn = eventSource()
const { events } = eventSourceFn(calendar)
await events({ start, end, timeZone }, successCallback, failureCallback)

expect(store.getters.getTimeRangeForCalendarCoveringRange).toHaveBeenCalledTimes(1)
expect(store.getters.getTimeRangeForCalendarCoveringRange).toHaveBeenNthCalledWith(1, 'calendar-id-123', 1234, 5678)
expect(fetchedTimeRangesStore.getTimeRangeForCalendarCoveringRange).toHaveBeenCalledTimes(1)
expect(fetchedTimeRangesStore.getTimeRangeForCalendarCoveringRange).toHaveBeenNthCalledWith(1, 'calendar-id-123', 1234, 5678)

expect(store.dispatch).toHaveBeenCalledTimes(1)
expect(store.dispatch).toHaveBeenNthCalledWith(1, 'getEventsFromCalendarInTimeRange', {
expect(calendarsStore.getEventsFromCalendarInTimeRange).toHaveBeenCalledTimes(1)
expect(calendarsStore.getEventsFromCalendarInTimeRange).toHaveBeenNthCalledWith(1, {
calendar,
from: start,
to: end
})

expect(store.getters.getCalendarObjectsByTimeRangeId).toHaveBeenCalledTimes(0)
expect(fetchedTimeRangesStore.getCalendarObjectsByTimeRangeId).toHaveBeenCalledTimes(0)

expect(eventSourceFunction).toHaveBeenCalledTimes(0)
expect(successCallback).toHaveBeenCalledTimes(0)

Expand Down
Loading

0 comments on commit 7c28348

Please sign in to comment.