Repository navigation
fix(tasks): propagate store errors so views gate navigation on success #4221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e2688bf
fix(tasks): propagate store errors so views gate navigation on success
PierreBrisorgueil 9f75b23
test(tasks): clean up dead mock and superfluous config in view tests
PierreBrisorgueil 764eb98
fix(tasks): await getTasks in list view to handle promise rejection
PierreBrisorgueil 5f3882f
test(tasks): prune unused mockConfig.api fields in task.view tests
PierreBrisorgueil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
| import { mount, flushPromises } from '@vue/test-utils'; | ||
| import { createPinia, setActivePinia } from 'pinia'; | ||
| import { createVuetify } from 'vuetify'; | ||
|
|
||
| /** | ||
| * Hoisted mocks — must be defined before any imports that transitively use them. | ||
| */ | ||
| const createTaskMock = vi.hoisted(() => vi.fn()); | ||
| const updateTaskMock = vi.hoisted(() => vi.fn()); | ||
| const deleteTaskMock = vi.hoisted(() => vi.fn()); | ||
| const getTaskMock = vi.hoisted(() => vi.fn()); | ||
|
|
||
| const mockTask = vi.hoisted(() => ({ | ||
| title: 'My Task', | ||
| description: 'My Description', | ||
| id: null, | ||
| })); | ||
|
|
||
| vi.mock('../stores/tasks.store', () => ({ | ||
| useTasksStore: () => ({ | ||
| get task() { | ||
| return mockTask; | ||
| }, | ||
| createTask: createTaskMock, | ||
| updateTask: updateTaskMock, | ||
| deleteTask: deleteTaskMock, | ||
| getTask: getTaskMock, | ||
| resetTask: vi.fn(), | ||
| }), | ||
| })); | ||
|
|
||
| vi.mock('../../core/components/core.pageHeader.component.vue', () => ({ | ||
| default: { template: '<div><slot name="actions" /></div>' }, | ||
| })); | ||
|
|
||
| vi.mock('../components/task.component.vue', () => ({ | ||
| default: { template: '<div />' }, | ||
| })); | ||
|
|
||
| import TaskView from '../views/task.view.vue'; | ||
|
|
||
| const mockPush = vi.fn(); | ||
|
|
||
| /** | ||
| * Mount the task view with optional route params. | ||
| * @param {Object} [options] | ||
| * @param {string|null} [options.id=null] - Route param id | ||
| * @returns {import('@vue/test-utils').VueWrapper} | ||
| */ | ||
| const mockConfig = { | ||
| vuetify: { theme: { flat: true, rounded: 'rounded-lg' } }, | ||
| }; | ||
|
|
||
| const mountView = ({ id = null } = {}) => | ||
| mount(TaskView, { | ||
| global: { | ||
| plugins: [createVuetify(), createPinia()], | ||
| mocks: { | ||
| config: mockConfig, | ||
| $route: { params: { id } }, | ||
| $router: { push: mockPush }, | ||
| }, | ||
| stubs: { RouterLink: true }, | ||
| }, | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| describe('task.view — create()', () => { | ||
| beforeEach(() => { | ||
| setActivePinia(createPinia()); | ||
| vi.clearAllMocks(); | ||
| mockTask.id = null; | ||
| mockTask.title = 'My Task'; | ||
| mockTask.description = 'My Description'; | ||
| }); | ||
|
|
||
| it('navigates to /tasks after a successful create', async () => { | ||
| createTaskMock.mockResolvedValueOnce({ id: 'new-id', title: 'My Task', description: 'My Description' }); | ||
|
|
||
| const wrapper = mountView(); | ||
| await flushPromises(); | ||
|
|
||
| await wrapper.vm.create(); | ||
|
|
||
| expect(createTaskMock).toHaveBeenCalledTimes(1); | ||
| expect(mockPush).toHaveBeenCalledWith('/tasks'); | ||
| expect(wrapper.vm.save).toBe(false); | ||
| }); | ||
|
|
||
| it('does NOT navigate when createTask rejects', async () => { | ||
| createTaskMock.mockRejectedValueOnce(new Error('Server error')); | ||
|
|
||
| const wrapper = mountView(); | ||
| await flushPromises(); | ||
|
|
||
| await wrapper.vm.create(); | ||
|
|
||
| expect(createTaskMock).toHaveBeenCalledTimes(1); | ||
| expect(mockPush).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does NOT clear save flag when createTask rejects', async () => { | ||
| createTaskMock.mockRejectedValueOnce(new Error('Server error')); | ||
|
|
||
| const wrapper = mountView(); | ||
| await flushPromises(); | ||
|
|
||
| wrapper.vm.save = true; | ||
| await wrapper.vm.create(); | ||
|
|
||
| // save should remain true (not cleared to false) on failure | ||
| expect(wrapper.vm.save).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('task.view — update()', () => { | ||
| beforeEach(() => { | ||
| setActivePinia(createPinia()); | ||
| vi.clearAllMocks(); | ||
| mockTask.id = 'task-123'; | ||
| mockTask.title = 'My Task'; | ||
| mockTask.description = 'My Description'; | ||
| getTaskMock.mockResolvedValue({ id: 'task-123', title: 'My Task', description: 'My Description' }); | ||
| }); | ||
|
|
||
| it('navigates to /tasks after a successful update', async () => { | ||
| updateTaskMock.mockResolvedValueOnce({ id: 'task-123', title: 'My Task', description: 'My Description' }); | ||
|
|
||
| const wrapper = mountView({ id: 'task-123' }); | ||
| await flushPromises(); | ||
|
|
||
| await wrapper.vm.update(); | ||
|
|
||
| expect(updateTaskMock).toHaveBeenCalledTimes(1); | ||
| expect(mockPush).toHaveBeenCalledWith('/tasks'); | ||
| expect(wrapper.vm.save).toBe(false); | ||
| }); | ||
|
|
||
| it('does NOT navigate when updateTask rejects', async () => { | ||
| updateTaskMock.mockRejectedValueOnce(new Error('Update failed')); | ||
|
|
||
| const wrapper = mountView({ id: 'task-123' }); | ||
| await flushPromises(); | ||
|
|
||
| await wrapper.vm.update(); | ||
|
|
||
| expect(updateTaskMock).toHaveBeenCalledTimes(1); | ||
| expect(mockPush).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does NOT clear save flag when updateTask rejects', async () => { | ||
| updateTaskMock.mockRejectedValueOnce(new Error('Update failed')); | ||
|
|
||
| const wrapper = mountView({ id: 'task-123' }); | ||
| await flushPromises(); | ||
|
|
||
| wrapper.vm.save = true; | ||
| await wrapper.vm.update(); | ||
|
|
||
| expect(wrapper.vm.save).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('task.view — remove()', () => { | ||
| beforeEach(() => { | ||
| setActivePinia(createPinia()); | ||
| vi.clearAllMocks(); | ||
| mockTask.id = 'task-123'; | ||
| mockTask.title = 'My Task'; | ||
| mockTask.description = 'My Description'; | ||
| getTaskMock.mockResolvedValue({ id: 'task-123', title: 'My Task', description: 'My Description' }); | ||
| }); | ||
|
|
||
| it('navigates to /tasks after a successful delete', async () => { | ||
| deleteTaskMock.mockResolvedValueOnce(undefined); | ||
|
|
||
| const wrapper = mountView({ id: 'task-123' }); | ||
| await flushPromises(); | ||
|
|
||
| await wrapper.vm.remove(); | ||
|
|
||
| expect(deleteTaskMock).toHaveBeenCalledTimes(1); | ||
| expect(mockPush).toHaveBeenCalledWith('/tasks'); | ||
| }); | ||
|
|
||
| it('does NOT navigate when deleteTask rejects', async () => { | ||
| deleteTaskMock.mockRejectedValueOnce(new Error('Delete failed')); | ||
|
|
||
| const wrapper = mountView({ id: 'task-123' }); | ||
| await flushPromises(); | ||
|
|
||
| await wrapper.vm.remove(); | ||
|
|
||
| expect(deleteTaskMock).toHaveBeenCalledTimes(1); | ||
| expect(mockPush).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
PierreBrisorgueil marked this conversation as resolved.
Resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.