From 104e4d51c16a1140065219a3317e2d1b15d1c88e Mon Sep 17 00:00:00 2001 From: SebastianKrupinski Date: Fri, 28 Aug 2026 11:36:30 -0400 Subject: [PATCH] fix: event creation with alarm Signed-off-by: SebastianKrupinski --- src/store/calendarObjectInstance.js | 18 +++-- .../unit/store/calendarObjectInstance.test.ts | 66 +++++++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/src/store/calendarObjectInstance.js b/src/store/calendarObjectInstance.js index edfe579f05..f5eebe1e33 100644 --- a/src/store/calendarObjectInstance.js +++ b/src/store/calendarObjectInstance.js @@ -1101,17 +1101,19 @@ export default defineStore('calendarObjectInstance', { /** * * @param {object} data The destructuring object + * @param {object=} data.calendarObjectInstance The calendar-object-instance to add the alarm to, defaults to the current one * @param {string} data.type Type of alarm * @param {number} data.totalSeconds Total amount of seconds for new alarm * @param {boolean=} data.isDefault Whether this is the default alarm */ addAlarmToCalendarObjectInstance({ + calendarObjectInstance = this.calendarObjectInstance, type, totalSeconds, isDefault = false, }) { - if (this.calendarObjectInstance.eventComponent) { - const eventComponent = this.calendarObjectInstance.eventComponent + if (calendarObjectInstance.eventComponent) { + const eventComponent = calendarObjectInstance.eventComponent const duration = DurationValue.fromSeconds(totalSeconds) const alarmComponent = eventComponent.addRelativeAlarm(type, duration) @@ -1122,7 +1124,7 @@ export default defineStore('calendarObjectInstance', { const alarmObject = mapAlarmComponentToAlarmObject(alarmComponent) - this.calendarObjectInstance.alarms.push(alarmObject) + calendarObjectInstance.alarms.push(alarmObject) logger.debug(alarmObject.alarmComponent.toICALJs().toString()) } @@ -1131,13 +1133,15 @@ export default defineStore('calendarObjectInstance', { /** * * @param {object} data The destructuring object + * @param {object=} data.calendarObjectInstance The calendar-object-instance to remove the alarm from, defaults to the current one * @param {object} data.alarm The alarm object */ removeAlarmFromCalendarObjectInstance({ + calendarObjectInstance = this.calendarObjectInstance, alarm, }) { if (alarm.alarmComponent) { - const alarmIterator = this.calendarObjectInstance.eventComponent.getAlarmIterator() + const alarmIterator = calendarObjectInstance.eventComponent.getAlarmIterator() let matchedAlarm = null const targetSeconds = alarm.alarmComponent.trigger.value.totalSeconds const targetAction = alarm.alarmComponent.action @@ -1149,12 +1153,12 @@ export default defineStore('calendarObjectInstance', { } if (matchedAlarm) { - this.calendarObjectInstance.eventComponent.removeAlarm(matchedAlarm) + calendarObjectInstance.eventComponent.removeAlarm(matchedAlarm) } - const index = this.calendarObjectInstance.alarms.indexOf(alarm) + const index = calendarObjectInstance.alarms.indexOf(alarm) if (index !== -1) { - this.calendarObjectInstance.alarms.splice(index, 1) + calendarObjectInstance.alarms.splice(index, 1) } } }, diff --git a/tests/javascript/unit/store/calendarObjectInstance.test.ts b/tests/javascript/unit/store/calendarObjectInstance.test.ts index e356ef236d..33b22e8d1f 100644 --- a/tests/javascript/unit/store/calendarObjectInstance.test.ts +++ b/tests/javascript/unit/store/calendarObjectInstance.test.ts @@ -4,14 +4,17 @@ */ import { createPinia, setActivePinia } from 'pinia' import { describe, expect, it, vi } from 'vitest' +import { mapAlarmComponentToAlarmObject } from '@/models/alarm.js' import { copyCalendarObjectInstanceIntoEventComponent, mapEventComponentToEventObject } from '@/models/event.js' import useCalendarObjectInstanceStore from '@/store/calendarObjectInstance.js' import useCalendarObjectsStore from '@/store/calendarObjects.js' import { getObjectAtRecurrenceId } from '@/utils/calendarObject.js' +vi.mock('@/models/alarm.js') vi.mock('@/models/event.js') vi.mock('@/utils/calendarObject.js') +const mockedMapAlarmComponentToAlarmObject = vi.mocked(mapAlarmComponentToAlarmObject) const mockedCopyCalendarObjectInstanceIntoEventComponent = vi.mocked(copyCalendarObjectInstanceIntoEventComponent) const mockedMapEventComponentToEventObject = vi.mocked(mapEventComponentToEventObject) const mockedGetObjectAtRecurrenceId = vi.mocked(getObjectAtRecurrenceId) @@ -20,6 +23,7 @@ describe('store/calendarObjectInstance test suite', () => { beforeEach(() => { setActivePinia(createPinia()) + mockedMapAlarmComponentToAlarmObject.mockReset() mockedCopyCalendarObjectInstanceIntoEventComponent.mockReset() mockedMapEventComponentToEventObject.mockReset().mockReturnValue({ eventComponent: {} }) mockedGetObjectAtRecurrenceId.mockReset().mockReturnValue({}) @@ -72,4 +76,66 @@ describe('store/calendarObjectInstance test suite', () => { expect(store.calendarObject).toStrictEqual(newCalendarObject) }) }) + + describe('addAlarmToCalendarObjectInstance', () => { + it('adds the alarm to an explicitly given calendar-object-instance instead of the store state', () => { + const store = useCalendarObjectInstanceStore() + // No event is currently loaded into the store, e.g. when creating the very first event of a session + store.calendarObjectInstance = null + + const alarmComponent = { addProperty: vi.fn(), toICALJs: vi.fn().mockReturnValue({ toString: () => '' }) } + const eventComponent = { addRelativeAlarm: vi.fn().mockReturnValue(alarmComponent) } + const calendarObjectInstance = { eventComponent, alarms: [] } + const alarmObject = { alarmComponent } + mockedMapAlarmComponentToAlarmObject.mockReturnValue(alarmObject) + + expect(() => store.addAlarmToCalendarObjectInstance({ + calendarObjectInstance, + type: 'DISPLAY', + totalSeconds: -600, + })).not.toThrow() + + expect(calendarObjectInstance.alarms).toContain(alarmObject) + }) + + it('falls back to the calendar-object-instance in the store when none is given', () => { + const store = useCalendarObjectInstanceStore() + const alarmComponent = { addProperty: vi.fn(), toICALJs: vi.fn().mockReturnValue({ toString: () => '' }) } + const eventComponent = { addRelativeAlarm: vi.fn().mockReturnValue(alarmComponent) } + store.calendarObjectInstance = { eventComponent, alarms: [] } + const alarmObject = { alarmComponent } + mockedMapAlarmComponentToAlarmObject.mockReturnValue(alarmObject) + + store.addAlarmToCalendarObjectInstance({ + type: 'DISPLAY', + totalSeconds: -600, + }) + + expect(store.calendarObjectInstance.alarms).toContainEqual(alarmObject) + }) + }) + + describe('removeAlarmFromCalendarObjectInstance', () => { + it('removes the alarm from an explicitly given calendar-object-instance instead of the store state', () => { + const store = useCalendarObjectInstanceStore() + // No event is currently loaded into the store, e.g. when creating the very first event of a session + store.calendarObjectInstance = null + + const matchedAlarmComponent = { trigger: { value: { totalSeconds: -600 } }, action: 'DISPLAY' } + const eventComponent = { + getAlarmIterator: () => [matchedAlarmComponent], + removeAlarm: vi.fn(), + } + const alarm = { alarmComponent: matchedAlarmComponent } + const calendarObjectInstance = { eventComponent, alarms: [alarm] } + + expect(() => store.removeAlarmFromCalendarObjectInstance({ + calendarObjectInstance, + alarm, + })).not.toThrow() + + expect(eventComponent.removeAlarm).toHaveBeenCalledWith(matchedAlarmComponent) + expect(calendarObjectInstance.alarms).not.toContain(alarm) + }) + }) })