Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/store/calendarObjectInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought(non-blocking): addAlarmToCalendarObjectInstance and removeAlarmFromCalendarObjectInstance should eventually move to util/alarm.js.

util/alarm.js#updateDefaultAlarm should not access the calendarObjectInstanceStore because in general stores can access utils, but utils should never access stores.

Also use of calendarsStore() can be removed by passing calendar as parameter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

util/alarm.js#updateDefaultAlarm should not access the calendarObjectInstanceStore because in general stores can access utils, but utils should never access stores.

I agree. This will need to be refactored at some point.

And mp, we all miss things... 10 years of spaghetti code to fix.

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)
Expand All @@ -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())
}
Expand All @@ -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
Expand All @@ -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)
}
}
},
Expand Down
66 changes: 66 additions & 0 deletions tests/javascript/unit/store/calendarObjectInstance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -20,6 +23,7 @@ describe('store/calendarObjectInstance test suite', () => {
beforeEach(() => {
setActivePinia(createPinia())

mockedMapAlarmComponentToAlarmObject.mockReset()
mockedCopyCalendarObjectInstanceIntoEventComponent.mockReset()
mockedMapEventComponentToEventObject.mockReset().mockReturnValue({ eventComponent: {} })
mockedGetObjectAtRecurrenceId.mockReset().mockReturnValue({})
Expand Down Expand Up @@ -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)
})
})
})
Loading