Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions apps/systemtags/src/components/SystemTags.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
6 changes: 3 additions & 3 deletions apps/systemtags/src/components/SystemTags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ async function handleCreate(tag: Tag) {
sortedTags.value.unshift(createdTag)
selectedTags.value.push(createdTag)
} catch (error) {
const systemTagsCreationRestrictedToAdmin = loadState<true | false>('settings', 'restrictSystemTagsCreationToAdmin', false) === true
const systemTagsCreationRestrictedToAdmin = loadState<true | false>('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

Expand Down