|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 LibreSign contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { mount } from '@vue/test-utils' |
| 8 | + |
| 9 | +const loadStateMock = vi.fn() |
| 10 | + |
| 11 | +vi.mock('@nextcloud/initial-state', () => ({ |
| 12 | + loadState: (...args: unknown[]) => loadStateMock(...args), |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('@nextcloud/l10n', () => ({ |
| 16 | + t: vi.fn((_app: string, text: string) => text), |
| 17 | + translate: vi.fn((_app: string, text: string) => text), |
| 18 | + translatePlural: vi.fn((_app: string, singular: string, plural: string, count: number) => (count === 1 ? singular : plural)), |
| 19 | + n: vi.fn((_app: string, singular: string, plural: string, count: number) => (count === 1 ? singular : plural)), |
| 20 | + isRTL: vi.fn(() => false), |
| 21 | + getLanguage: vi.fn(() => 'en'), |
| 22 | + getLocale: vi.fn(() => 'en'), |
| 23 | +})) |
| 24 | + |
| 25 | +let Confetti: unknown |
| 26 | + |
| 27 | +beforeAll(async () => { |
| 28 | + ;({ default: Confetti } = await import('../../../views/Settings/Confetti.vue')) |
| 29 | +}) |
| 30 | + |
| 31 | +describe('Confetti', () => { |
| 32 | + beforeEach(() => { |
| 33 | + loadStateMock.mockReset() |
| 34 | + }) |
| 35 | + |
| 36 | + it('defaults to true when state is not set', async () => { |
| 37 | + loadStateMock.mockImplementation((_app: string, key: string, fallback: unknown) => fallback) |
| 38 | + |
| 39 | + const wrapper = mount(Confetti as never, { |
| 40 | + global: { |
| 41 | + stubs: { |
| 42 | + NcSettingsSection: { template: '<div><slot /></div>' }, |
| 43 | + NcCheckboxRadioSwitch: { template: '<div><slot /></div>' }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }) |
| 47 | + |
| 48 | + expect(wrapper.vm.showConfetti).toBe(true) |
| 49 | + }) |
| 50 | + |
| 51 | + it('reads show_confetti_after_signing from initial state', async () => { |
| 52 | + loadStateMock.mockImplementation((_app: string, key: string, fallback: unknown) => { |
| 53 | + if (key === 'show_confetti_after_signing') return true |
| 54 | + return fallback |
| 55 | + }) |
| 56 | + |
| 57 | + const wrapper = mount(Confetti as never, { |
| 58 | + global: { |
| 59 | + stubs: { |
| 60 | + NcSettingsSection: { template: '<div><slot /></div>' }, |
| 61 | + NcCheckboxRadioSwitch: { template: '<div><slot /></div>' }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }) |
| 65 | + |
| 66 | + expect(wrapper.vm.showConfetti).toBe(true) |
| 67 | + }) |
| 68 | + |
| 69 | + it('calls OCP.AppConfig.setValue with "1" when enabled', async () => { |
| 70 | + loadStateMock.mockImplementation((_app: string, key: string, fallback: unknown) => fallback) |
| 71 | + const setValueMock = vi.fn() |
| 72 | + vi.stubGlobal('OCP', { AppConfig: { setValue: setValueMock } }) |
| 73 | + |
| 74 | + const wrapper = mount(Confetti as never, { |
| 75 | + global: { |
| 76 | + stubs: { |
| 77 | + NcSettingsSection: { template: '<div><slot /></div>' }, |
| 78 | + NcCheckboxRadioSwitch: { template: '<div><slot /></div>' }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }) |
| 82 | + |
| 83 | + await wrapper.setData({ showConfetti: true }) |
| 84 | + wrapper.vm.saveShowConfetti() |
| 85 | + |
| 86 | + expect(setValueMock).toHaveBeenCalledWith('libresign', 'show_confetti_after_signing', '1') |
| 87 | + }) |
| 88 | + |
| 89 | + it('calls OCP.AppConfig.setValue with "0" when disabled', async () => { |
| 90 | + loadStateMock.mockImplementation((_app: string, key: string, fallback: unknown) => { |
| 91 | + if (key === 'show_confetti_after_signing') return true |
| 92 | + return fallback |
| 93 | + }) |
| 94 | + const setValueMock = vi.fn() |
| 95 | + vi.stubGlobal('OCP', { AppConfig: { setValue: setValueMock } }) |
| 96 | + |
| 97 | + const wrapper = mount(Confetti as never, { |
| 98 | + global: { |
| 99 | + stubs: { |
| 100 | + NcSettingsSection: { template: '<div><slot /></div>' }, |
| 101 | + NcCheckboxRadioSwitch: { template: '<div><slot /></div>' }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }) |
| 105 | + |
| 106 | + await wrapper.setData({ showConfetti: false }) |
| 107 | + wrapper.vm.saveShowConfetti() |
| 108 | + |
| 109 | + expect(setValueMock).toHaveBeenCalledWith('libresign', 'show_confetti_after_signing', '0') |
| 110 | + }) |
| 111 | +}) |
0 commit comments