Skip to content

Commit 50b6dda

Browse files
test(DefaultPageError): add unit tests for dynamic title and error display
Cover all branches of the errors computed property: - errors[] with items: title 'An error occurred', NcNoteCard with message - no data: title 'Page not found', description shown, no NcNoteCard - empty errors[] but error.message present: fallback to singular object Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 7379bc9 commit 50b6dda

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 LibreSign contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { beforeEach, describe, expect, it, vi, type MockedFunction } from 'vitest'
7+
import { mount } from '@vue/test-utils'
8+
9+
vi.mock('@nextcloud/initial-state', () => ({
10+
loadState: vi.fn((app: string, key: string, defaultValue: unknown) => defaultValue),
11+
}))
12+
13+
vi.mock('@nextcloud/l10n', () => ({
14+
t: vi.fn((_app: string, text: string) => text),
15+
isRTL: vi.fn(() => false),
16+
getLanguage: vi.fn(() => 'en'),
17+
getLocale: vi.fn(() => 'en'),
18+
}))
19+
20+
vi.mock('@nextcloud/vue/components/NcEmptyContent', () => ({
21+
default: {
22+
name: 'NcEmptyContent',
23+
template: '<div class="nc-empty-content"><slot name="action" /><span class="title">{{ name }}</span><span class="description">{{ description }}</span></div>',
24+
props: ['name', 'description'],
25+
},
26+
}))
27+
vi.mock('@nextcloud/vue/components/NcIconSvgWrapper', () => ({
28+
default: { name: 'NcIconSvgWrapper', template: '<span />', props: ['path', 'size'] },
29+
}))
30+
vi.mock('@nextcloud/vue/components/NcNoteCard', () => ({
31+
default: { name: 'NcNoteCard', template: '<div class="nc-note-card"><slot /></div>', props: ['type'] },
32+
}))
33+
34+
import DefaultPageError from '../../views/DefaultPageError.vue'
35+
36+
describe('DefaultPageError', () => {
37+
let loadState: MockedFunction<typeof import('@nextcloud/initial-state').loadState>
38+
39+
beforeEach(async () => {
40+
const { loadState: ls } = await import('@nextcloud/initial-state')
41+
loadState = ls as MockedFunction<typeof import('@nextcloud/initial-state').loadState>
42+
loadState.mockImplementation((_app, _key, defaultValue) => defaultValue)
43+
})
44+
45+
it('shows "An error occurred" title and error message when errors are provided via initial state', () => {
46+
loadState.mockImplementation((app, key, defaultValue) => {
47+
if (app === 'libresign' && key === 'errors') {
48+
return [{ message: 'This document is not yours. Log out and use the sign link again.' }]
49+
}
50+
return defaultValue
51+
})
52+
53+
const wrapper = mount(DefaultPageError)
54+
55+
expect(wrapper.find('.title').text()).toBe('An error occurred')
56+
expect(wrapper.find('.description').text()).toBe('')
57+
expect(wrapper.find('.nc-note-card').text()).toContain('This document is not yours. Log out and use the sign link again.')
58+
})
59+
60+
it('shows "Page not found" title and description when no errors are present', () => {
61+
const wrapper = mount(DefaultPageError)
62+
63+
expect(wrapper.find('.title').text()).toBe('Page not found')
64+
expect(wrapper.find('.description').text()).toContain('Sorry but the page you are looking for')
65+
expect(wrapper.find('.nc-note-card').exists()).toBe(false)
66+
})
67+
68+
it('shows error message from "error" key when "errors" array is empty', () => {
69+
loadState.mockImplementation((app, key, defaultValue) => {
70+
if (app === 'libresign' && key === 'errors') return []
71+
if (app === 'libresign' && key === 'error') return { message: 'Something went wrong' }
72+
return defaultValue
73+
})
74+
75+
const wrapper = mount(DefaultPageError)
76+
77+
expect(wrapper.find('.title').text()).toBe('An error occurred')
78+
expect(wrapper.find('.nc-note-card').text()).toContain('Something went wrong')
79+
})
80+
})

0 commit comments

Comments
 (0)