diff --git a/apps/systemtags/src/components/SystemTags.spec.ts b/apps/systemtags/src/components/SystemTags.spec.ts new file mode 100644 index 0000000000000..e79f60ae60e74 --- /dev/null +++ b/apps/systemtags/src/components/SystemTags.spec.ts @@ -0,0 +1,88 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import type { VueWrapper } from '@vue/test-utils' +import type { Tag } from '../types.ts' + +import { showError } from '@nextcloud/dialogs' +import { loadState } from '@nextcloud/initial-state' +import { flushPromises, mount } from '@vue/test-utils' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { createTagForFile } from '../services/files.ts' +import { defaultBaseTag } from '../utils.ts' +import SystemTags from './SystemTags.vue' + +vi.mock('@nextcloud/dialogs', () => ({ + showError: vi.fn(), +})) + +vi.mock('@nextcloud/initial-state', () => ({ + loadState: vi.fn(), +})) + +vi.mock('@nextcloud/files', () => ({ + getSidebar: vi.fn(() => ({})), +})) + +vi.mock('../services/api.ts', () => ({ + fetchTags: vi.fn(async () => []), + fetchLastUsedTagIds: vi.fn(async () => []), +})) + +vi.mock('../services/files.ts', () => ({ + createTagForFile: vi.fn(), + deleteTagForFile: vi.fn(async () => {}), + fetchTagsForFile: vi.fn(async () => []), + setTagForFile: vi.fn(async () => {}), +})) + +vi.mock('../services/davClient.ts', () => ({ + fetchNode: vi.fn(async () => null), +})) + +const newTag: Tag = { + ...defaultBaseTag, + displayName: 'new tag', +} + +describe('SystemTags tag creation', () => { + let component: VueWrapper + + beforeEach(() => { + vi.clearAllMocks() + vi.mocked(loadState).mockReturnValue(false) + }) + + afterEach(() => { + component?.unmount() + }) + + async function mountAndFailToCreate() { + vi.mocked(createTagForFile).mockRejectedValue(new Error('Failed to create tag')) + component = mount(SystemTags, { props: { fileId: 1 } }) + await flushPromises() + component.findComponent({ name: 'NcSelectTags' }).vm.$emit('option:created', newTag) + await flushPromises() + return component.findComponent({ name: 'NcSelectTags' }) + } + + it('shows the admin restriction message when tag creation is restricted', async () => { + vi.mocked(loadState).mockReturnValue(true) + + const select = await mountAndFailToCreate() + + expect(loadState).toHaveBeenCalledWith('systemtags', 'restrictSystemTagsCreationToAdmin', false) + expect(showError).toHaveBeenCalledWith('System admin disabled tag creation. You can only use existing ones.') + expect(select.props('loading')).toBe(false) + }) + + it('shows the generic message and resets loading when tag creation is not restricted', async () => { + const select = await mountAndFailToCreate() + + expect(loadState).toHaveBeenCalledWith('systemtags', 'restrictSystemTagsCreationToAdmin', false) + expect(showError).toHaveBeenCalledWith('Failed to create tag') + expect(select.props('loading')).toBe(false) + }) +}) diff --git a/apps/systemtags/src/components/SystemTags.vue b/apps/systemtags/src/components/SystemTags.vue index a5f98671f5658..2292ecb248182 100644 --- a/apps/systemtags/src/components/SystemTags.vue +++ b/apps/systemtags/src/components/SystemTags.vue @@ -161,13 +161,13 @@ async function handleCreate(tag: Tag) { sortedTags.value.unshift(createdTag) selectedTags.value.push(createdTag) } catch (error) { - const systemTagsCreationRestrictedToAdmin = loadState('settings', 'restrictSystemTagsCreationToAdmin', false) === true + const systemTagsCreationRestrictedToAdmin = loadState('systemtags', 'restrictSystemTagsCreationToAdmin', false) === true logger.error('Failed to create tag', { error }) if (systemTagsCreationRestrictedToAdmin) { showError(t('systemtags', 'System admin disabled tag creation. You can only use existing ones.')) - return + } else { + showError(t('systemtags', 'Failed to create tag')) } - showError(t('systemtags', 'Failed to create tag')) } loading.value = false