-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
431 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
* | ||
|
@@ -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', () => { | ||
|
||
|
@@ -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', | ||
|
@@ -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', | ||
|
@@ -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', | ||
|
@@ -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', | ||
|
@@ -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', | ||
|
@@ -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' }) | ||
|
@@ -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', | ||
|
@@ -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' }) | ||
|
@@ -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', | ||
|
@@ -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' }) | ||
|
@@ -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', | ||
|
@@ -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) | ||
|
||
|
Oops, something went wrong.