|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { mount } from '@vue/test-utils' |
| 7 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | + |
| 9 | +const confirmPassword = vi.hoisted(() => vi.fn()) |
| 10 | +vi.mock('@nextcloud/password-confirmation', () => ({ confirmPassword })) |
| 11 | +vi.mock('@nextcloud/dialogs', () => ({ showError: vi.fn(), showSuccess: vi.fn() })) |
| 12 | + |
| 13 | +// Decouple the dialog test from form-data diffing internals: always report a |
| 14 | +// non-empty change set so save() proceeds past its early return. Other exports |
| 15 | +// (used transitively by the form sub-components) are kept real. |
| 16 | +vi.mock('./userFormUtils.ts', async (importActual) => ({ |
| 17 | + ...(await importActual()), |
| 18 | + userToFormData: () => ({ |
| 19 | + username: 'bob', |
| 20 | + displayName: 'Bob', |
| 21 | + password: '', |
| 22 | + email: '', |
| 23 | + groups: [], |
| 24 | + subadminGroups: [], |
| 25 | + quota: { id: 'default' }, |
| 26 | + language: { code: 'en' }, |
| 27 | + manager: { id: '' }, |
| 28 | + }), |
| 29 | + diffPayload: () => ({ displayName: 'Bobby' }), |
| 30 | +})) |
| 31 | + |
| 32 | +import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon' |
| 33 | +import EditUserDialog from './EditUserDialog.vue' |
| 34 | +import { flushPromises, NcButtonStub, NcDialogStub, UserFormFieldsStub } from './dialogTestHelpers.ts' |
| 35 | + |
| 36 | +function mountDialog({ dispatch = vi.fn() } = {}) { |
| 37 | + return mount(EditUserDialog, { |
| 38 | + propsData: { |
| 39 | + user: { id: 'bob', backendCapabilities: { setPassword: true } }, |
| 40 | + quotaOptions: [], |
| 41 | + }, |
| 42 | + mocks: { |
| 43 | + t: (_app: string, text: string) => text, |
| 44 | + $store: { |
| 45 | + dispatch, |
| 46 | + getters: { |
| 47 | + getGroups: [], |
| 48 | + getServerData: { languages: [], canChangePassword: true }, |
| 49 | + getPasswordPolicyMinLength: 8, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + stubs: { |
| 54 | + NcDialog: NcDialogStub, |
| 55 | + NcButton: NcButtonStub, |
| 56 | + UserFormFields: UserFormFieldsStub, |
| 57 | + }, |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +describe('EditUserDialog loading feedback', () => { |
| 62 | + beforeEach(() => { |
| 63 | + vi.clearAllMocks() |
| 64 | + confirmPassword.mockResolvedValue(undefined) |
| 65 | + }) |
| 66 | + |
| 67 | + it('does not dispatch a second save request while one is in flight', async () => { |
| 68 | + const dispatch = vi.fn().mockReturnValue(new Promise(() => {})) |
| 69 | + const wrapper = mountDialog({ dispatch }) |
| 70 | + |
| 71 | + await wrapper.find('form').trigger('submit') |
| 72 | + await flushPromises() |
| 73 | + await wrapper.find('form').trigger('submit') |
| 74 | + await flushPromises() |
| 75 | + |
| 76 | + const saveCalls = dispatch.mock.calls.filter(([action]) => action === 'editUserMultiField') |
| 77 | + expect(saveCalls).toHaveLength(1) |
| 78 | + }) |
| 79 | + |
| 80 | + it('marks the form as busy and inert while saving', async () => { |
| 81 | + confirmPassword.mockReturnValue(new Promise(() => {})) |
| 82 | + const wrapper = mountDialog() |
| 83 | + |
| 84 | + await wrapper.find('form').trigger('submit') |
| 85 | + |
| 86 | + const form = wrapper.find('form') |
| 87 | + expect(form.attributes('aria-busy')).toBe('true') |
| 88 | + expect(form.attributes('inert')).toBeDefined() |
| 89 | + }) |
| 90 | + |
| 91 | + it('shows a spinner and busy label on the submit button while saving', async () => { |
| 92 | + confirmPassword.mockReturnValue(new Promise(() => {})) |
| 93 | + const wrapper = mountDialog() |
| 94 | + |
| 95 | + await wrapper.find('form').trigger('submit') |
| 96 | + |
| 97 | + expect(wrapper.findComponent(NcLoadingIcon).exists()).toBe(true) |
| 98 | + expect(wrapper.find('[data-test="submit"]').text()).toContain('Saving') |
| 99 | + }) |
| 100 | + |
| 101 | + it('sets aria-disabled (not disabled) on the submit button while saving', async () => { |
| 102 | + confirmPassword.mockReturnValue(new Promise(() => {})) |
| 103 | + const wrapper = mountDialog() |
| 104 | + const submit = wrapper.find('[data-test="submit"]') |
| 105 | + |
| 106 | + expect(submit.attributes('aria-disabled')).toBe('false') |
| 107 | + expect(submit.attributes('disabled')).toBeUndefined() |
| 108 | + |
| 109 | + await wrapper.find('form').trigger('submit') |
| 110 | + |
| 111 | + expect(submit.attributes('aria-disabled')).toBe('true') |
| 112 | + expect(submit.attributes('disabled')).toBeUndefined() |
| 113 | + }) |
| 114 | + |
| 115 | + it('prevents closing the dialog while saving', async () => { |
| 116 | + confirmPassword.mockReturnValue(new Promise(() => {})) |
| 117 | + const wrapper = mountDialog() |
| 118 | + const dialog = wrapper.findComponent(NcDialogStub) |
| 119 | + |
| 120 | + expect(dialog.props('noClose')).toBe(false) |
| 121 | + |
| 122 | + await wrapper.find('form').trigger('submit') |
| 123 | + |
| 124 | + expect(dialog.props('noClose')).toBe(true) |
| 125 | + }) |
| 126 | +}) |
0 commit comments