|
| 1 | +import { AxiosResponse } from "axios"; |
| 2 | +import { ID_OPTIMISTIC_PREFIX } from "@core/constants/core.constants"; |
| 3 | +import { Schema_Event } from "@core/types/event.types"; |
| 4 | +import { createMockStandaloneEvent } from "@core/util/test/ccal.event.factory"; |
| 5 | +import { createStoreWithEvents } from "@web/__tests__/utils/state/store.test.util"; |
| 6 | +import { sagaMiddleware } from "@web/common/store/middlewares"; |
| 7 | +import { Schema_GridEvent } from "@web/common/types/web.event.types"; |
| 8 | +import { isOptimisticEvent } from "@web/common/utils/event/event.util"; |
| 9 | +import { EventApi } from "@web/ducks/events/event.api"; |
| 10 | +import { selectEventById } from "@web/ducks/events/selectors/event.selectors"; |
| 11 | +import { createEventSlice } from "@web/ducks/events/slices/event.slice"; |
| 12 | +import { RootState } from "@web/store"; |
| 13 | +import { sagas } from "@web/store/sagas"; |
| 14 | +import { OnSubmitParser } from "@web/views/Calendar/components/Draft/hooks/actions/submit.parser"; |
| 15 | + |
| 16 | +jest.mock("@web/ducks/events/event.api"); |
| 17 | + |
| 18 | +describe("createEvent saga - optimistic rendering", () => { |
| 19 | + let store: ReturnType<typeof createStoreWithEvents>; |
| 20 | + let mockCreateApi: jest.SpyInstance; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + jest.clearAllMocks(); |
| 24 | + store = createStoreWithEvents([]); |
| 25 | + sagaMiddleware.run(sagas); |
| 26 | + |
| 27 | + mockCreateApi = jest.spyOn(EventApi, "create").mockImplementation(() => { |
| 28 | + return Promise.resolve({ |
| 29 | + status: 200, |
| 30 | + } as unknown as AxiosResponse<void>); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should immediately add event with optimistic ID when created", () => { |
| 35 | + const gridEvent = createMockStandaloneEvent() as Schema_GridEvent; |
| 36 | + const event = new OnSubmitParser(gridEvent).parse() as Schema_Event; |
| 37 | + const action = createEventSlice.actions.request(event); |
| 38 | + |
| 39 | + store.dispatch(action); |
| 40 | + |
| 41 | + // Get all events from state |
| 42 | + const state = store.getState(); |
| 43 | + const eventEntities = state.events.entities.value || {}; |
| 44 | + const eventIds = Object.keys(eventEntities); |
| 45 | + |
| 46 | + // Should have exactly one event |
| 47 | + expect(eventIds).toHaveLength(1); |
| 48 | + |
| 49 | + const optimisticId = eventIds[0]; |
| 50 | + const optimisticEvent = eventEntities[optimisticId]; |
| 51 | + |
| 52 | + // Event should have optimistic ID prefix |
| 53 | + expect(optimisticId).toMatch(new RegExp(`^${ID_OPTIMISTIC_PREFIX}-`)); |
| 54 | + expect(optimisticEvent._id).toBe(optimisticId); |
| 55 | + expect(isOptimisticEvent(optimisticEvent)).toBe(true); |
| 56 | + |
| 57 | + // Event should be in week and day event lists |
| 58 | + const weekEventIds = state.events.getWeekEvents.value?.data || []; |
| 59 | + const dayEventIds = state.events.getDayEvents.value?.data || []; |
| 60 | + |
| 61 | + expect(weekEventIds).toContain(optimisticId); |
| 62 | + expect(dayEventIds).toContain(optimisticId); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should keep event in state during API call", async () => { |
| 66 | + // Create a promise that we can control |
| 67 | + let resolveApiCall: (value: AxiosResponse<void>) => void; |
| 68 | + const apiPromise = new Promise<AxiosResponse<void>>((resolve) => { |
| 69 | + resolveApiCall = resolve; |
| 70 | + }); |
| 71 | + |
| 72 | + mockCreateApi.mockImplementation(() => apiPromise); |
| 73 | + |
| 74 | + const gridEvent = createMockStandaloneEvent() as Schema_GridEvent; |
| 75 | + const event = new OnSubmitParser(gridEvent).parse() as Schema_Event; |
| 76 | + const action = createEventSlice.actions.request(event); |
| 77 | + |
| 78 | + store.dispatch(action); |
| 79 | + |
| 80 | + // Get the optimistic ID immediately after dispatch |
| 81 | + const stateAfterDispatch = store.getState(); |
| 82 | + const eventEntitiesAfterDispatch = |
| 83 | + stateAfterDispatch.events.entities.value || {}; |
| 84 | + const optimisticIds = Object.keys(eventEntitiesAfterDispatch); |
| 85 | + expect(optimisticIds).toHaveLength(1); |
| 86 | + const optimisticId = optimisticIds[0]; |
| 87 | + |
| 88 | + // Verify event is still in state while API call is pending |
| 89 | + const stateDuringApiCall = store.getState(); |
| 90 | + const eventDuringApiCall = selectEventById( |
| 91 | + stateDuringApiCall as RootState, |
| 92 | + optimisticId, |
| 93 | + ); |
| 94 | + |
| 95 | + expect(eventDuringApiCall).not.toBeNull(); |
| 96 | + expect(eventDuringApiCall?._id).toBe(optimisticId); |
| 97 | + expect(isOptimisticEvent(eventDuringApiCall!)).toBe(true); |
| 98 | + |
| 99 | + // Verify event is still in week and day lists |
| 100 | + const weekEventIdsDuringCall = |
| 101 | + stateDuringApiCall.events.getWeekEvents.value?.data || []; |
| 102 | + const dayEventIdsDuringCall = |
| 103 | + stateDuringApiCall.events.getDayEvents.value?.data || []; |
| 104 | + |
| 105 | + expect(weekEventIdsDuringCall).toContain(optimisticId); |
| 106 | + expect(dayEventIdsDuringCall).toContain(optimisticId); |
| 107 | + |
| 108 | + // Resolve the API call |
| 109 | + resolveApiCall!({ |
| 110 | + status: 200, |
| 111 | + } as AxiosResponse<void>); |
| 112 | + |
| 113 | + // Wait for saga to complete |
| 114 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 115 | + |
| 116 | + // Verify event is still in state after API call completes |
| 117 | + // The optimistic ID should be replaced with real ID |
| 118 | + const realEventId = optimisticId.replace(`${ID_OPTIMISTIC_PREFIX}-`, ""); |
| 119 | + const stateAfterApiCall = store.getState(); |
| 120 | + const eventAfterApiCall = selectEventById( |
| 121 | + stateAfterApiCall as RootState, |
| 122 | + realEventId, |
| 123 | + ); |
| 124 | + |
| 125 | + // Event should still exist with real ID |
| 126 | + expect(eventAfterApiCall).not.toBeNull(); |
| 127 | + expect(eventAfterApiCall?._id).toBe(realEventId); |
| 128 | + }); |
| 129 | + |
| 130 | + it("should replace optimistic ID with real ID after successful API call", async () => { |
| 131 | + const gridEvent = createMockStandaloneEvent() as Schema_GridEvent; |
| 132 | + const event = new OnSubmitParser(gridEvent).parse() as Schema_Event; |
| 133 | + |
| 134 | + // Mock API to return success |
| 135 | + mockCreateApi.mockResolvedValue({ |
| 136 | + status: 200, |
| 137 | + } as AxiosResponse<void>); |
| 138 | + |
| 139 | + const action = createEventSlice.actions.request(event); |
| 140 | + store.dispatch(action); |
| 141 | + |
| 142 | + // Get optimistic ID immediately |
| 143 | + const initialState = store.getState(); |
| 144 | + const initialEventEntities = initialState.events.entities.value || {}; |
| 145 | + const optimisticIds = Object.keys(initialEventEntities); |
| 146 | + expect(optimisticIds).toHaveLength(1); |
| 147 | + const optimisticId = optimisticIds[0]; |
| 148 | + |
| 149 | + // The real ID is the optimistic ID without the prefix |
| 150 | + const realEventId = optimisticId.replace(`${ID_OPTIMISTIC_PREFIX}-`, ""); |
| 151 | + |
| 152 | + // Wait for saga to complete |
| 153 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 154 | + |
| 155 | + const finalState = store.getState(); |
| 156 | + const eventEntities = finalState.events.entities.value || {}; |
| 157 | + const eventIds = Object.keys(eventEntities); |
| 158 | + |
| 159 | + // Should still have exactly one event |
| 160 | + expect(eventIds).toHaveLength(1); |
| 161 | + |
| 162 | + // The event should have the real ID (without optimistic prefix) |
| 163 | + expect(eventIds[0]).toBe(realEventId); |
| 164 | + expect(eventIds[0]).not.toMatch(new RegExp(`^${ID_OPTIMISTIC_PREFIX}-`)); |
| 165 | + |
| 166 | + // Verify the event is accessible by real ID |
| 167 | + const finalEvent = selectEventById(finalState as RootState, realEventId); |
| 168 | + expect(finalEvent).not.toBeNull(); |
| 169 | + expect(finalEvent?._id).toBe(realEventId); |
| 170 | + expect(isOptimisticEvent(finalEvent!)).toBe(false); |
| 171 | + }); |
| 172 | + |
| 173 | + it("should never remove event from state after being added", async () => { |
| 174 | + const gridEvent = createMockStandaloneEvent() as Schema_GridEvent; |
| 175 | + const event = new OnSubmitParser(gridEvent).parse() as Schema_Event; |
| 176 | + const action = createEventSlice.actions.request(event); |
| 177 | + |
| 178 | + store.dispatch(action); |
| 179 | + |
| 180 | + // Get optimistic ID immediately |
| 181 | + const initialState = store.getState(); |
| 182 | + const initialEventEntities = initialState.events.entities.value || {}; |
| 183 | + const optimisticIds = Object.keys(initialEventEntities); |
| 184 | + expect(optimisticIds).toHaveLength(1); |
| 185 | + const optimisticId = optimisticIds[0]; |
| 186 | + const realEventId = optimisticId.replace(`${ID_OPTIMISTIC_PREFIX}-`, ""); |
| 187 | + |
| 188 | + // Check 1: Immediately after dispatch (should have optimistic ID) |
| 189 | + const check1 = store.getState(); |
| 190 | + const event1 = selectEventById(check1 as RootState, optimisticId); |
| 191 | + expect(event1).not.toBeNull(); |
| 192 | + expect(event1?._id).toBe(optimisticId); |
| 193 | + |
| 194 | + // Wait for API call to complete |
| 195 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 196 | + |
| 197 | + // Check 2: After API call completes (should have real ID, not optimistic) |
| 198 | + const check2 = store.getState(); |
| 199 | + const event2Optimistic = selectEventById(check2 as RootState, optimisticId); |
| 200 | + const event2Real = selectEventById(check2 as RootState, realEventId); |
| 201 | + |
| 202 | + // Event should no longer be accessible by optimistic ID |
| 203 | + expect(event2Optimistic).toBeNull(); |
| 204 | + // But should be accessible by real ID |
| 205 | + expect(event2Real).not.toBeNull(); |
| 206 | + expect(event2Real?._id).toBe(realEventId); |
| 207 | + |
| 208 | + // Final verification: event should exist with real ID |
| 209 | + const finalState = store.getState(); |
| 210 | + const finalEventEntities = finalState.events.entities.value || {}; |
| 211 | + const finalEventCount = Object.keys(finalEventEntities).length; |
| 212 | + |
| 213 | + // Should have exactly one event |
| 214 | + expect(finalEventCount).toBe(1); |
| 215 | + expect(finalEventEntities[realEventId]).toBeDefined(); |
| 216 | + |
| 217 | + // Verify event count never dropped to zero |
| 218 | + // The event should transition from optimistic ID to real ID without disappearing |
| 219 | + expect(finalEventCount).toBeGreaterThanOrEqual(1); |
| 220 | + }); |
| 221 | + |
| 222 | + it("should maintain event in week and day event lists throughout creation process", async () => { |
| 223 | + const gridEvent = createMockStandaloneEvent() as Schema_GridEvent; |
| 224 | + const event = new OnSubmitParser(gridEvent).parse() as Schema_Event; |
| 225 | + const action = createEventSlice.actions.request(event); |
| 226 | + |
| 227 | + store.dispatch(action); |
| 228 | + |
| 229 | + // Get optimistic ID |
| 230 | + const initialState = store.getState(); |
| 231 | + const initialEventEntities = initialState.events.entities.value || {}; |
| 232 | + const optimisticIds = Object.keys(initialEventEntities); |
| 233 | + expect(optimisticIds).toHaveLength(1); |
| 234 | + const optimisticId = optimisticIds[0]; |
| 235 | + const realEventId = optimisticId.replace(`${ID_OPTIMISTIC_PREFIX}-`, ""); |
| 236 | + |
| 237 | + // Verify in lists immediately with optimistic ID |
| 238 | + const initialWeekIds = initialState.events.getWeekEvents.value?.data || []; |
| 239 | + const initialDayIds = initialState.events.getDayEvents.value?.data || []; |
| 240 | + expect(initialWeekIds).toContain(optimisticId); |
| 241 | + expect(initialDayIds).toContain(optimisticId); |
| 242 | + |
| 243 | + // Wait for API call to complete |
| 244 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 245 | + |
| 246 | + // Verify still in lists but with real ID (replaced) |
| 247 | + const finalState = store.getState(); |
| 248 | + const finalWeekIds = finalState.events.getWeekEvents.value?.data || []; |
| 249 | + const finalDayIds = finalState.events.getDayEvents.value?.data || []; |
| 250 | + |
| 251 | + // Should no longer have optimistic ID |
| 252 | + expect(finalWeekIds).not.toContain(optimisticId); |
| 253 | + expect(finalDayIds).not.toContain(optimisticId); |
| 254 | + |
| 255 | + // Should have real ID in both lists |
| 256 | + expect(finalWeekIds).toContain(realEventId); |
| 257 | + expect(finalDayIds).toContain(realEventId); |
| 258 | + |
| 259 | + // Should have exactly one event in each list |
| 260 | + expect(finalWeekIds).toHaveLength(1); |
| 261 | + expect(finalDayIds).toHaveLength(1); |
| 262 | + }); |
| 263 | +}); |
0 commit comments