-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtask.view.unit.tests.js
More file actions
247 lines (193 loc) · 6.98 KB
/
Copy pathtask.view.unit.tests.js
File metadata and controls
247 lines (193 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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 },
},
});
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();
});
});
describe('task.view — save flag on user action (T6)', () => {
beforeEach(() => {
setActivePinia(createPinia());
vi.clearAllMocks();
mockTask.id = null;
mockTask.title = 'My Task';
mockTask.description = 'My Description';
});
it('sets save=true when title computed setter is called', async () => {
const wrapper = mountView();
await flushPromises();
// Simulate user editing the title
wrapper.vm.title = 'Updated Title';
expect(wrapper.vm.save).toBe(true);
});
it('sets save=true when description computed setter is called', async () => {
const wrapper = mountView();
await flushPromises();
// Simulate user editing the description
wrapper.vm.description = 'Updated Description';
expect(wrapper.vm.save).toBe(true);
});
it('does not have a watch block on task (removed in T6)', () => {
const wrapper = mountView();
// The $options.watch should be absent or not contain a 'task' watcher
const watch = wrapper.vm.$options.watch;
if (watch) {
expect(watch).not.toHaveProperty('task');
} else {
// No watch block at all — task watcher is gone (expected outcome)
expect(watch).toBeUndefined();
}
});
it('save starts as null (not pre-triggered on mount for new task)', async () => {
const wrapper = mountView();
await flushPromises();
// For a new task (no id), save should remain null until user edits
expect(wrapper.vm.save).toBeNull();
});
});