Skip to content

Commit 21ac7ec

Browse files
test(vue3): add ReadCertificate coverage
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent e1184cc commit 21ac7ec

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 } from 'vitest'
7+
import { mount } from '@vue/test-utils'
8+
import axios from '@nextcloud/axios'
9+
10+
import ReadCertificate from '../../../views/ReadCertificate/ReadCertificate.vue'
11+
12+
const signMethodsStoreMock = {
13+
modal: {
14+
readCertificate: true,
15+
},
16+
closeModal: vi.fn(),
17+
}
18+
19+
vi.mock('@nextcloud/l10n', () => ({
20+
t: vi.fn((_app: string, text: string) => text),
21+
translate: vi.fn((_app: string, text: string) => text),
22+
translatePlural: vi.fn((_app: string, singular: string, plural: string, count: number) => (count === 1 ? singular : plural)),
23+
n: vi.fn((_app: string, singular: string, plural: string, count: number) => (count === 1 ? singular : plural)),
24+
getLanguage: vi.fn(() => 'en'),
25+
getLocale: vi.fn(() => 'en'),
26+
isRTL: vi.fn(() => false),
27+
}))
28+
29+
vi.mock('@nextcloud/axios', () => ({
30+
default: {
31+
post: vi.fn(),
32+
},
33+
}))
34+
35+
vi.mock('@nextcloud/router', () => ({
36+
generateOcsUrl: vi.fn((path: string) => path),
37+
}))
38+
39+
vi.mock('../../../store/signMethods.js', () => ({
40+
useSignMethodsStore: vi.fn(() => signMethodsStoreMock),
41+
}))
42+
43+
describe('ReadCertificate.vue', () => {
44+
beforeEach(() => {
45+
signMethodsStoreMock.modal.readCertificate = true
46+
signMethodsStoreMock.closeModal.mockReset()
47+
vi.mocked(axios.post).mockReset()
48+
})
49+
50+
function createWrapper() {
51+
return mount(ReadCertificate, {
52+
global: {
53+
stubs: {
54+
NcDialog: {
55+
name: 'NcDialog',
56+
props: ['name', 'size', 'isForm'],
57+
emits: ['submit', 'closing'],
58+
template: '<div class="dialog-stub"><slot /><slot name="actions" /></div>',
59+
},
60+
NcPasswordField: {
61+
name: 'NcPasswordField',
62+
props: ['modelValue', 'disabled', 'label', 'placeholder'],
63+
emits: ['update:modelValue'],
64+
template: '<input class="password-field" :value="modelValue" @input="$emit(\'update:modelValue\', $event.target.value)" />',
65+
},
66+
NcButton: {
67+
name: 'NcButton',
68+
props: ['disabled', 'type', 'variant'],
69+
emits: ['click'],
70+
template: '<button class="button-stub" @click="$emit(\'click\')"><slot /><slot name="icon" /></button>',
71+
},
72+
NcNoteCard: {
73+
name: 'NcNoteCard',
74+
props: ['type'],
75+
template: '<div class="note-card"><slot /></div>',
76+
},
77+
NcLoadingIcon: {
78+
name: 'NcLoadingIcon',
79+
props: ['size'],
80+
template: '<span class="loading-stub"></span>',
81+
},
82+
CertificateContent: {
83+
name: 'CertificateContent',
84+
props: ['certificate'],
85+
template: '<div class="certificate-content-stub"></div>',
86+
},
87+
},
88+
},
89+
})
90+
}
91+
92+
it('resets its local state on mount', () => {
93+
const wrapper = createWrapper()
94+
95+
expect(wrapper.vm.password).toBe('')
96+
expect(wrapper.vm.certificate).toEqual({})
97+
expect(wrapper.vm.error).toBe('')
98+
expect(wrapper.vm.size).toBe('small')
99+
})
100+
101+
it('stores certificate data and expands the dialog on successful read', async () => {
102+
vi.mocked(axios.post).mockResolvedValue({
103+
data: {
104+
ocs: {
105+
data: {
106+
subject: 'LibreSign Certificate',
107+
},
108+
},
109+
},
110+
})
111+
const wrapper = createWrapper()
112+
wrapper.vm.password = 'secret'
113+
114+
await wrapper.vm.send()
115+
116+
expect(wrapper.vm.certificate).toEqual({ subject: 'LibreSign Certificate' })
117+
expect(wrapper.vm.size).toBe('large')
118+
expect(wrapper.vm.error).toBe('')
119+
expect(wrapper.vm.hasLoading).toBe(false)
120+
})
121+
122+
it('closes the modal and resets the state', async () => {
123+
const wrapper = createWrapper()
124+
wrapper.vm.password = 'secret'
125+
wrapper.vm.certificate = { subject: 'Existing' }
126+
wrapper.vm.size = 'large'
127+
128+
wrapper.vm.onClose()
129+
130+
expect(signMethodsStoreMock.closeModal).toHaveBeenCalledWith('readCertificate')
131+
expect(wrapper.vm.password).toBe('')
132+
expect(wrapper.vm.certificate).toEqual({})
133+
expect(wrapper.vm.size).toBe('small')
134+
})
135+
})

0 commit comments

Comments
 (0)