-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuser.view.unit.tests.js
More file actions
512 lines (433 loc) · 18.2 KB
/
user.view.unit.tests.js
File metadata and controls
512 lines (433 loc) · 18.2 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { setActivePinia, createPinia } from 'pinia';
import { shallowMount } from '@vue/test-utils';
import { useOrganizationsStore } from '../../organizations/stores/organizations.store';
import { useAuthStore } from '../../auth/stores/auth.store';
import UserView from '../views/user.view.vue';
import axios from '../../../lib/services/axios';
// Mock axios
vi.mock('../../../lib/services/axios', () => ({
default: {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
},
}));
// Mock config
vi.mock('../../../lib/services/config', () => ({
default: {
api: { protocol: 'http', host: 'localhost', port: '3000', base: 'api' },
cookie: { prefix: 'devkit' },
},
}));
// Mock helpers
vi.mock('../../../lib/helpers/roleColor', () => ({ default: () => 'primary' }));
vi.mock('../../../lib/helpers/orgColor', () => ({ default: () => 'blue' }));
vi.mock('../../../lib/helpers/ability', () => ({ updateAbilities: vi.fn() }));
const sharedStubs = {
PageHeader: true,
userProfileComponent: true,
organizationsSwitcherComponent: true,
orgAvatarComponent: true,
'v-container': { template: '<div><slot /></div>' },
'v-row': { template: '<div><slot /></div>' },
'v-col': { template: '<div><slot /></div>' },
'v-card': { template: '<div><slot /></div>' },
'v-tabs': { template: '<div><slot /></div>' },
'v-tab': { template: '<div><slot /></div>' },
'v-divider': { template: '<div />' },
'v-window': { template: '<div><slot /></div>' },
'v-window-item': { template: '<div><slot /></div>' },
'v-list': { template: '<div><slot /></div>' },
'v-list-item': { template: '<div><slot /></div>' },
'v-list-item-title': { template: '<div><slot /></div>' },
'v-list-item-subtitle': { template: '<div><slot /></div>' },
'v-avatar': { template: '<div><slot /></div>' },
'v-chip': { template: '<div><slot /></div>' },
'v-btn': { template: '<div><slot /></div>' },
'v-icon': { template: '<div />' },
'v-dialog': { template: '<div><slot /></div>' },
'v-card-title': { template: '<div><slot /></div>' },
'v-card-text': { template: '<div><slot /></div>' },
'v-card-actions': { template: '<div><slot /></div>' },
'v-spacer': { template: '<div />' },
'v-text-field': { template: '<div />' },
};
const sharedMocks = ($router = { push: vi.fn() }, $route = { query: {}, hash: '' }) => ({
$router,
$route,
$t: (key) => key,
config: {
api: { protocol: 'http', host: 'localhost', port: '3000', base: 'api' },
vuetify: { theme: { flat: true, rounded: 'rounded' } },
},
});
describe('UserView – leaveOrg redirect behaviour', () => {
let organizationsStore;
let authStore;
let routerPush;
let wrapper;
beforeEach(() => {
setActivePinia(createPinia());
organizationsStore = useOrganizationsStore();
authStore = useAuthStore();
// Stub fetchOrganizations so the created() hook does not hit the network
organizationsStore.fetchOrganizations = vi.fn().mockResolvedValue([]);
routerPush = vi.fn();
wrapper = shallowMount(UserView, {
global: {
mocks: sharedMocks({ push: routerPush }),
stubs: sharedStubs,
},
});
});
it('should redirect to /organization-required when 0 orgs remain after leaving', async () => {
// Set up: user is leaving the only org they belong to
const orgId = 'org-1';
wrapper.vm.orgToLeave = { id: orgId, name: 'Only Org' };
// Stub leaveOrganization so that after call, organizations is empty
organizationsStore.leaveOrganization = vi.fn().mockImplementation(() => {
organizationsStore.organizations = [];
return Promise.resolve();
});
authStore.refreshAbilities = vi.fn().mockResolvedValue();
await wrapper.vm.leaveOrg();
expect(organizationsStore.leaveOrganization).toHaveBeenCalledWith(orgId);
expect(routerPush).toHaveBeenCalledWith('/organization-required');
});
it('should call switchOrganization on first remaining org when currentOrganization is null after leaving', async () => {
const orgId = 'org-leave';
const remainingOrg = { id: 'org-remain', name: 'Remaining Org' };
wrapper.vm.orgToLeave = { id: orgId, name: 'Leaving Org' };
// After leaving, one org remains but currentOrganization is null
organizationsStore.leaveOrganization = vi.fn().mockImplementation(() => {
organizationsStore.organizations = [remainingOrg];
organizationsStore.currentOrganization = null;
return Promise.resolve();
});
organizationsStore.switchOrganization = vi.fn().mockResolvedValue();
authStore.refreshAbilities = vi.fn().mockResolvedValue();
await wrapper.vm.leaveOrg();
expect(organizationsStore.leaveOrganization).toHaveBeenCalledWith(orgId);
expect(organizationsStore.switchOrganization).toHaveBeenCalledWith(remainingOrg.id);
expect(routerPush).not.toHaveBeenCalled();
});
});
// ── UserView – C4 decoupling: no billing / subscriptions tab ─────────────────
describe('UserView – C4 decoupling: billing tab removed', () => {
beforeEach(() => {
setActivePinia(createPinia());
const organizationsStore = useOrganizationsStore();
organizationsStore.fetchOrganizations = vi.fn().mockResolvedValue([]);
});
it('does not expose showSubscriptionsTab computed', () => {
const wrapper = shallowMount(UserView, {
global: { mocks: sharedMocks(), stubs: sharedStubs },
});
expect(wrapper.vm.showSubscriptionsTab).toBeUndefined();
});
it('does not expose hasOwnerOrAdminRole computed', () => {
const wrapper = shallowMount(UserView, {
global: { mocks: sharedMocks(), stubs: sharedStubs },
});
expect(wrapper.vm.hasOwnerOrAdminRole).toBeUndefined();
});
it('does not render a subscriptions tab in the template', () => {
const authStore = useAuthStore();
const organizationsStore = useOrganizationsStore();
authStore.serverConfig = { billing: { enabled: true } };
organizationsStore.organizations = [{ id: 'o1', role: 'owner', name: 'Acme' }];
// Use a v-tab stub that captures its slot content so we can check for absence
const tabContents = [];
const vTabStub = {
template: '<div><slot /></div>',
created() { tabContents.push(this.$slots?.default?.()?.[0]?.children || ''); },
};
shallowMount(UserView, {
global: {
mocks: sharedMocks(),
stubs: { ...sharedStubs, 'v-tab': vTabStub },
},
});
// No tab content should contain 'Subscriptions' or 'subscriptions'
const allContent = tabContents.join('').toLowerCase();
expect(allContent).not.toContain('subscriptions');
});
it('does not import or render BillingSubscriptionsComponent', () => {
// Register a sentinel stub — if user.view still imports and renders it, it would appear
const wrapper = shallowMount(UserView, {
global: {
mocks: sharedMocks(),
stubs: {
...sharedStubs,
BillingSubscriptionsComponent: { template: '<div data-billing-sentinel />' },
},
},
});
expect(wrapper.html()).not.toContain('data-billing-sentinel');
});
it('only exposes profile and organizations tabs', () => {
// After C1.2 refactor, tabs are declared via tabsConfig (consumed by PageTabs).
// v-tab elements no longer appear directly in user.view.vue, so we inspect
// tabsConfig to verify the exposed set of tabs.
const wrapper = shallowMount(UserView, {
global: {
mocks: sharedMocks(),
stubs: sharedStubs,
},
});
const tabValues = wrapper.vm.tabsConfig.map((t) => t.value);
expect(tabValues).toContain('profile');
expect(tabValues).toContain('organizations');
expect(tabValues).not.toContain('subscriptions');
});
});
// ── UserView – tab routing from query/hash ───────────────────────────────────
describe('UserView – tab routing from query/hash', () => {
let authStore;
let organizationsStore;
beforeEach(() => {
setActivePinia(createPinia());
authStore = useAuthStore();
organizationsStore = useOrganizationsStore();
organizationsStore.fetchOrganizations = vi.fn().mockResolvedValue([]);
});
/**
* @desc Mount with explicit $route — caller controls query/hash.
* @param {Object} route
* @returns {import('@vue/test-utils').VueWrapper}
*/
const mountWithRoute = (route) =>
shallowMount(UserView, {
global: {
mocks: sharedMocks({ push: vi.fn() }, route),
stubs: sharedStubs,
},
});
it('switches to organizations tab when ?tab=organizations', async () => {
authStore.serverConfig = { billing: { enabled: true } };
organizationsStore.organizations = [{ id: 'o1', role: 'owner' }];
const wrapper = mountWithRoute({ query: { tab: 'organizations' }, hash: '' });
expect(wrapper.vm.tab).toBe('organizations');
});
it('keeps default profile tab when neither query nor hash is set', async () => {
authStore.serverConfig = { billing: { enabled: true } };
organizationsStore.organizations = [{ id: 'o1', role: 'owner' }];
const wrapper = mountWithRoute({ query: {}, hash: '' });
expect(wrapper.vm.tab).toBe('profile');
});
it('ignores ?tab=subscriptions (subscriptions tab removed) and stays on profile', async () => {
// After C4, there is no subscriptions tab — the request must be silently ignored
authStore.serverConfig = { billing: { enabled: true } };
organizationsStore.organizations = [{ id: 'o1', role: 'owner' }];
const wrapper = mountWithRoute({ query: { tab: 'subscriptions' }, hash: '' });
expect(wrapper.vm.tab).toBe('profile');
});
it('switches to profile tab when ?tab=profile', async () => {
const wrapper = mountWithRoute({ query: { tab: 'profile' }, hash: '' });
expect(wrapper.vm.tab).toBe('profile');
});
});
// ── UserView – organizations refetch on auth state change ────────────────────
describe('UserView – organizations refetch on auth state change', () => {
let authStore;
let organizationsStore;
beforeEach(() => {
setActivePinia(createPinia());
authStore = useAuthStore();
organizationsStore = useOrganizationsStore();
organizationsStore.fetchOrganizations = vi.fn().mockResolvedValue([]);
});
it('calls fetchOrganizations immediately when isLoggedIn is true at mount', async () => {
authStore.cookieExpire = Date.now() + 3600000; // logged in
organizationsStore.fetchOrganizations = vi.fn().mockResolvedValue([]);
shallowMount(UserView, {
global: {
mocks: sharedMocks(),
stubs: sharedStubs,
},
});
// Flush microtasks so the immediate watcher handler resolves
await new Promise((r) => setTimeout(r, 0));
expect(organizationsStore.fetchOrganizations).toHaveBeenCalled();
});
it('does not call fetchOrganizations from watcher when isLoggedIn is false at mount', async () => {
authStore.cookieExpire = 0; // logged out
const fetchOrgsSpy = vi.fn().mockResolvedValue([]);
organizationsStore.fetchOrganizations = fetchOrgsSpy;
shallowMount(UserView, {
global: {
mocks: sharedMocks(),
stubs: sharedStubs,
},
});
await new Promise((r) => setTimeout(r, 0));
// The created() hook always calls fetchOrganizations once; the watcher must NOT call it
// a second time when not logged in. Total = 1 (from created), not 2.
expect(fetchOrgsSpy.mock.calls.length).toBeLessThanOrEqual(1);
});
it('does NOT have a billingStore.fetchSubscription call (billing decoupled)', async () => {
// Ensure user.view no longer touches any billingStore
authStore.cookieExpire = Date.now() + 3600000; // logged in
// If user.view still imports billingStore and calls fetchSubscription, this would
// require a mock; its absence means the view is cleanly decoupled.
expect(Object.keys(UserView.components || {})).not.toContain('BillingSubscriptionsComponent');
});
});
// ── UserView – C1.2: PageTabs integration ────────────────────────────────────
describe('UserView – renders PageTabs with profile + organizations entries', () => {
beforeEach(() => {
setActivePinia(createPinia());
const organizationsStore = useOrganizationsStore();
organizationsStore.fetchOrganizations = vi.fn().mockResolvedValue([]);
});
it('renders a [data-test="page-tabs"] element via PageTabs', async () => {
const PageTabsStub = {
template: '<div data-test="page-tabs"><slot /></div>',
inheritAttrs: false,
};
const wrapper = shallowMount(UserView, {
global: {
mocks: sharedMocks(),
stubs: { ...sharedStubs, PageTabs: PageTabsStub },
},
});
await wrapper.vm.$nextTick();
expect(wrapper.find('[data-test="page-tabs"]').exists()).toBe(true);
});
it('tabsConfig includes profile and organizations entries', () => {
const wrapper = shallowMount(UserView, {
global: { mocks: sharedMocks(), stubs: sharedStubs },
});
const config = wrapper.vm.tabsConfig;
expect(Array.isArray(config)).toBe(true);
const values = config.map((t) => t.value);
expect(values).toContain('profile');
expect(values).toContain('organizations');
});
it('tabsConfig profile entry has correct label', () => {
const wrapper = shallowMount(UserView, {
global: { mocks: sharedMocks(), stubs: sharedStubs },
});
const profile = wrapper.vm.tabsConfig.find((t) => t.value === 'profile');
expect(profile?.label).toBe('Profile');
});
it('tabsConfig organizations entry has correct label', () => {
const wrapper = shallowMount(UserView, {
global: { mocks: sharedMocks(), stubs: sharedStubs },
});
const orgs = wrapper.vm.tabsConfig.find((t) => t.value === 'organizations');
expect(orgs?.label).toBe('Organizations');
});
});
// ── UserView – delete account danger zone in Profile tab ─────────────────────
describe('UserView – delete account danger zone', () => {
let authStore;
let organizationsStore;
let wrapper;
beforeEach(() => {
setActivePinia(createPinia());
authStore = useAuthStore();
organizationsStore = useOrganizationsStore();
organizationsStore.fetchOrganizations = vi.fn().mockResolvedValue([]);
authStore.serverConfig = { billing: { enabled: true } };
organizationsStore.organizations = [{ id: 'o1', role: 'owner' }];
wrapper = shallowMount(UserView, {
global: {
mocks: sharedMocks(),
stubs: sharedStubs,
},
});
});
it('opens the delete account dialog when confirmDeleteAccount is set to true', async () => {
expect(wrapper.vm.confirmDeleteAccount).toBe(false);
wrapper.vm.confirmDeleteAccount = true;
await wrapper.vm.$nextTick();
expect(wrapper.vm.confirmDeleteAccount).toBe(true);
});
it('confirm button is disabled when deleteConfirmInput is not DELETE', async () => {
// Use a full-stubs setup that captures the :disabled binding from the confirm v-btn
// The confirm button has :disabled="deleteConfirmInput !== 'DELETE'"
const capturedDisabledValues = [];
const vBtnStub = {
template: '<button :disabled="disabled" v-bind="$attrs"><slot /></button>',
props: { disabled: { type: Boolean, default: false } },
created() {
// Capture only the btn that has a disabled binding (the confirm btn)
if (Object.prototype.hasOwnProperty.call(this.$props, 'disabled')) {
capturedDisabledValues.push(this.$props);
}
},
};
const w = shallowMount(UserView, {
global: {
mocks: sharedMocks(),
stubs: { ...sharedStubs, 'v-btn': vBtnStub },
},
});
w.vm.deleteConfirmInput = 'DELET';
await w.vm.$nextTick();
// The confirm button binding: :disabled="deleteConfirmInput !== 'DELETE'"
expect(w.vm.deleteConfirmInput !== 'DELETE').toBe(true);
// If stubs render a standard <button>, check DOM disabled state
const buttons = w.findAll('button[disabled]');
expect(buttons.length).toBeGreaterThanOrEqual(1);
});
it('confirm button is enabled when deleteConfirmInput equals DELETE', async () => {
const w = shallowMount(UserView, {
global: {
mocks: sharedMocks(),
stubs: {
...sharedStubs,
'v-btn': {
template: '<button :disabled="disabled" v-bind="$attrs"><slot /></button>',
props: { disabled: { type: Boolean, default: false } },
},
},
},
});
w.vm.deleteConfirmInput = 'DELETE';
await w.vm.$nextTick();
// :disabled="deleteConfirmInput !== 'DELETE'" → false when input === 'DELETE'
expect(w.vm.deleteConfirmInput === 'DELETE').toBe(true);
// No buttons should have disabled when input is DELETE
const disabledButtons = w.findAll('button[disabled]');
expect(disabledButtons.length).toBe(0);
});
it('deleteAccount method calls axios.delete and redirects to /signin on success', async () => {
const routerPush = vi.fn();
authStore.signout = vi.fn().mockResolvedValue();
axios.delete.mockResolvedValue({});
const w = shallowMount(UserView, {
global: {
mocks: sharedMocks({ push: routerPush }),
stubs: sharedStubs,
},
});
await w.vm.deleteAccount();
expect(axios.delete).toHaveBeenCalledWith(expect.stringContaining('/users'));
expect(authStore.signout).toHaveBeenCalled();
expect(routerPush).toHaveBeenCalledWith('/signin');
});
it('deleteAccount method closes dialog on error (swallows exception)', async () => {
axios.delete.mockRejectedValue(new Error('Server error'));
const w = shallowMount(UserView, {
global: {
mocks: sharedMocks(),
stubs: sharedStubs,
},
});
w.vm.confirmDeleteAccount = true;
w.vm.deleteConfirmInput = 'DELETE';
await w.vm.deleteAccount();
expect(w.vm.confirmDeleteAccount).toBe(false);
expect(w.vm.deleteConfirmInput).toBe('');
});
it('deleteAccount strings are inlined in user.view.vue', () => {
const html = wrapper.html();
expect(html).toContain('Delete account');
expect(html).toContain('Permanently delete your account, data, and organization ownership. This cannot be undone.');
expect(html).toContain('Type DELETE to confirm');
});
});