|
| 1 | +import { useUIStore } from '@/stores/ui.store'; |
| 2 | +import { MODAL_CANCEL, MODAL_CONFIRM, PLACEHOLDER_EMPTY_WORKFLOW_ID, VIEWS } from '@/constants'; |
| 3 | +import { useWorkflowSaving } from './useWorkflowSaving'; |
| 4 | +import router from '@/router'; |
| 5 | +import { createTestingPinia } from '@pinia/testing'; |
| 6 | +import { setActivePinia } from 'pinia'; |
| 7 | +import { useNpsSurveyStore } from '@/stores/npsSurvey.store'; |
| 8 | +import { useWorkflowsStore } from '@/stores/workflows.store'; |
| 9 | + |
| 10 | +const modalConfirmSpy = vi.fn(); |
| 11 | +const saveCurrentWorkflowSpy = vi.fn(); |
| 12 | + |
| 13 | +vi.mock('@/composables/useMessage', () => { |
| 14 | + return { |
| 15 | + useMessage: () => ({ |
| 16 | + confirm: modalConfirmSpy, |
| 17 | + }), |
| 18 | + }; |
| 19 | +}); |
| 20 | + |
| 21 | +vi.mock('@/composables/useWorkflowHelpers', () => { |
| 22 | + return { |
| 23 | + useWorkflowHelpers: () => ({ |
| 24 | + saveCurrentWorkflow: saveCurrentWorkflowSpy, |
| 25 | + }), |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +describe('promptSaveUnsavedWorkflowChanges', () => { |
| 30 | + beforeAll(() => { |
| 31 | + setActivePinia(createTestingPinia()); |
| 32 | + }); |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + vi.resetAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should prompt the user to save changes and proceed if confirmed', async () => { |
| 39 | + const { promptSaveUnsavedWorkflowChanges } = useWorkflowSaving({ router }); |
| 40 | + const next = vi.fn(); |
| 41 | + const confirm = vi.fn().mockResolvedValue(true); |
| 42 | + const cancel = vi.fn(); |
| 43 | + |
| 44 | + // Mock state |
| 45 | + const uiStore = useUIStore(); |
| 46 | + uiStore.stateIsDirty = true; |
| 47 | + |
| 48 | + const npsSurveyStore = useNpsSurveyStore(); |
| 49 | + vi.spyOn(npsSurveyStore, 'fetchPromptsData').mockResolvedValue(); |
| 50 | + |
| 51 | + saveCurrentWorkflowSpy.mockResolvedValue(true); |
| 52 | + |
| 53 | + // Mock message.confirm |
| 54 | + modalConfirmSpy.mockResolvedValue(MODAL_CONFIRM); |
| 55 | + |
| 56 | + await promptSaveUnsavedWorkflowChanges(next, { confirm, cancel }); |
| 57 | + |
| 58 | + expect(modalConfirmSpy).toHaveBeenCalled(); |
| 59 | + expect(npsSurveyStore.fetchPromptsData).toHaveBeenCalled(); |
| 60 | + expect(saveCurrentWorkflowSpy).toHaveBeenCalledWith({}, false); |
| 61 | + expect(uiStore.stateIsDirty).toEqual(false); |
| 62 | + |
| 63 | + expect(confirm).toHaveBeenCalled(); |
| 64 | + expect(next).toHaveBeenCalledWith(true); |
| 65 | + expect(cancel).not.toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not proceed if the user cancels the confirmation modal', async () => { |
| 69 | + const { promptSaveUnsavedWorkflowChanges } = useWorkflowSaving({ router }); |
| 70 | + const next = vi.fn(); |
| 71 | + const confirm = vi.fn(); |
| 72 | + const cancel = vi.fn(); |
| 73 | + |
| 74 | + // Mock state |
| 75 | + const uiStore = useUIStore(); |
| 76 | + uiStore.stateIsDirty = true; |
| 77 | + |
| 78 | + // Mock message.confirm |
| 79 | + modalConfirmSpy.mockResolvedValue(MODAL_CANCEL); |
| 80 | + |
| 81 | + await promptSaveUnsavedWorkflowChanges(next, { confirm, cancel }); |
| 82 | + |
| 83 | + expect(modalConfirmSpy).toHaveBeenCalled(); |
| 84 | + expect(saveCurrentWorkflowSpy).not.toHaveBeenCalled(); |
| 85 | + expect(uiStore.stateIsDirty).toEqual(false); |
| 86 | + |
| 87 | + expect(confirm).not.toHaveBeenCalled(); |
| 88 | + expect(cancel).toHaveBeenCalled(); |
| 89 | + expect(next).toHaveBeenCalledWith(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should restore the route if the modal is closed and the workflow is not new', async () => { |
| 93 | + const { promptSaveUnsavedWorkflowChanges } = useWorkflowSaving({ router }); |
| 94 | + const next = vi.fn(); |
| 95 | + const confirm = vi.fn(); |
| 96 | + const cancel = vi.fn(); |
| 97 | + |
| 98 | + // Mock state |
| 99 | + const uiStore = useUIStore(); |
| 100 | + uiStore.stateIsDirty = true; |
| 101 | + |
| 102 | + const workflowStore = useWorkflowsStore(); |
| 103 | + const MOCK_ID = 'existing-workflow-id'; |
| 104 | + workflowStore.workflow.id = MOCK_ID; |
| 105 | + |
| 106 | + // Mock message.confirm |
| 107 | + modalConfirmSpy.mockResolvedValue('close'); |
| 108 | + |
| 109 | + await promptSaveUnsavedWorkflowChanges(next, { confirm, cancel }); |
| 110 | + |
| 111 | + expect(modalConfirmSpy).toHaveBeenCalled(); |
| 112 | + expect(saveCurrentWorkflowSpy).not.toHaveBeenCalled(); |
| 113 | + expect(uiStore.stateIsDirty).toEqual(true); |
| 114 | + |
| 115 | + expect(confirm).not.toHaveBeenCalled(); |
| 116 | + expect(cancel).not.toHaveBeenCalled(); |
| 117 | + expect(next).toHaveBeenCalledWith( |
| 118 | + router.resolve({ |
| 119 | + name: VIEWS.WORKFLOW, |
| 120 | + params: { name: MOCK_ID }, |
| 121 | + }), |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should close modal if workflow is not new', async () => { |
| 126 | + const { promptSaveUnsavedWorkflowChanges } = useWorkflowSaving({ router }); |
| 127 | + const next = vi.fn(); |
| 128 | + const confirm = vi.fn(); |
| 129 | + const cancel = vi.fn(); |
| 130 | + |
| 131 | + // Mock state |
| 132 | + const uiStore = useUIStore(); |
| 133 | + uiStore.stateIsDirty = true; |
| 134 | + |
| 135 | + const workflowStore = useWorkflowsStore(); |
| 136 | + workflowStore.workflow.id = PLACEHOLDER_EMPTY_WORKFLOW_ID; |
| 137 | + |
| 138 | + // Mock message.confirm |
| 139 | + modalConfirmSpy.mockResolvedValue('close'); |
| 140 | + |
| 141 | + await promptSaveUnsavedWorkflowChanges(next, { confirm, cancel }); |
| 142 | + |
| 143 | + expect(modalConfirmSpy).toHaveBeenCalled(); |
| 144 | + expect(saveCurrentWorkflowSpy).not.toHaveBeenCalled(); |
| 145 | + expect(uiStore.stateIsDirty).toEqual(true); |
| 146 | + |
| 147 | + expect(confirm).not.toHaveBeenCalled(); |
| 148 | + expect(cancel).not.toHaveBeenCalled(); |
| 149 | + expect(next).not.toHaveBeenCalled(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should proceed without prompting if there are no unsaved changes', async () => { |
| 153 | + const { promptSaveUnsavedWorkflowChanges } = useWorkflowSaving({ router }); |
| 154 | + const next = vi.fn(); |
| 155 | + const confirm = vi.fn(); |
| 156 | + const cancel = vi.fn(); |
| 157 | + |
| 158 | + // Mock state |
| 159 | + const uiStore = useUIStore(); |
| 160 | + uiStore.stateIsDirty = false; |
| 161 | + |
| 162 | + await promptSaveUnsavedWorkflowChanges(next, { confirm, cancel }); |
| 163 | + |
| 164 | + expect(modalConfirmSpy).not.toHaveBeenCalled(); |
| 165 | + expect(saveCurrentWorkflowSpy).not.toHaveBeenCalled(); |
| 166 | + expect(uiStore.stateIsDirty).toEqual(false); |
| 167 | + |
| 168 | + expect(confirm).not.toHaveBeenCalled(); |
| 169 | + expect(cancel).not.toHaveBeenCalled(); |
| 170 | + expect(next).toHaveBeenCalledWith(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should handle save failure and restore the route', async () => { |
| 174 | + const { promptSaveUnsavedWorkflowChanges } = useWorkflowSaving({ router }); |
| 175 | + const next = vi.fn(); |
| 176 | + const confirm = vi.fn(); |
| 177 | + const cancel = vi.fn(); |
| 178 | + |
| 179 | + // Mock state |
| 180 | + const uiStore = useUIStore(); |
| 181 | + uiStore.stateIsDirty = true; |
| 182 | + |
| 183 | + const workflowStore = useWorkflowsStore(); |
| 184 | + const MOCK_ID = 'existing-workflow-id'; |
| 185 | + workflowStore.workflow.id = MOCK_ID; |
| 186 | + |
| 187 | + saveCurrentWorkflowSpy.mockResolvedValue(false); |
| 188 | + |
| 189 | + // Mock message.confirm |
| 190 | + modalConfirmSpy.mockResolvedValue(MODAL_CONFIRM); |
| 191 | + |
| 192 | + await promptSaveUnsavedWorkflowChanges(next, { confirm, cancel }); |
| 193 | + |
| 194 | + expect(modalConfirmSpy).toHaveBeenCalled(); |
| 195 | + expect(saveCurrentWorkflowSpy).toHaveBeenCalledWith({}, false); |
| 196 | + expect(uiStore.stateIsDirty).toEqual(true); |
| 197 | + |
| 198 | + expect(confirm).not.toHaveBeenCalled(); |
| 199 | + expect(cancel).not.toHaveBeenCalled(); |
| 200 | + expect(next).toHaveBeenCalledWith( |
| 201 | + router.resolve({ |
| 202 | + name: VIEWS.WORKFLOW, |
| 203 | + params: { name: MOCK_ID }, |
| 204 | + }), |
| 205 | + ); |
| 206 | + }); |
| 207 | +}); |
0 commit comments