|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode 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 { setActivePinia } from 'pinia' |
| 9 | +import { createTestingPinia } from '@pinia/testing' |
| 10 | + |
| 11 | +import FileListFilter from '../../../../views/FilesList/FileListFilter/FileListFilter.vue' |
| 12 | + |
| 13 | +vi.mock('@nextcloud/l10n', () => ({ |
| 14 | + t: vi.fn((_app: string, text: string) => text), |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@nextcloud/logger', () => ({ |
| 18 | + getLogger: vi.fn(() => ({ |
| 19 | + error: vi.fn(), |
| 20 | + warn: vi.fn(), |
| 21 | + info: vi.fn(), |
| 22 | + debug: vi.fn(), |
| 23 | + })), |
| 24 | + getLoggerBuilder: vi.fn(() => ({ |
| 25 | + setApp: vi.fn().mockReturnThis(), |
| 26 | + detectUser: vi.fn().mockReturnThis(), |
| 27 | + build: vi.fn(() => ({ |
| 28 | + error: vi.fn(), |
| 29 | + warn: vi.fn(), |
| 30 | + info: vi.fn(), |
| 31 | + debug: vi.fn(), |
| 32 | + })), |
| 33 | + })), |
| 34 | +})) |
| 35 | + |
| 36 | +vi.mock('@nextcloud/vue/components/NcButton', () => ({ |
| 37 | + default: { |
| 38 | + name: 'NcButton', |
| 39 | + props: ['variant', 'alignment', 'wide'], |
| 40 | + emits: ['click'], |
| 41 | + template: '<button class="nc-button-stub" :data-variant="variant" @click="$emit(\'click\')"><slot /><slot name="icon" /></button>', |
| 42 | + }, |
| 43 | +})) |
| 44 | + |
| 45 | +vi.mock('@nextcloud/vue/components/NcPopover', () => ({ |
| 46 | + default: { |
| 47 | + name: 'NcPopover', |
| 48 | + template: '<div class="nc-popover-stub"><slot name="trigger" /><slot /></div>', |
| 49 | + }, |
| 50 | +})) |
| 51 | + |
| 52 | +describe('FileListFilter.vue', () => { |
| 53 | + beforeEach(() => { |
| 54 | + setActivePinia(createTestingPinia({ createSpy: vi.fn })) |
| 55 | + }) |
| 56 | + |
| 57 | + function mountComponent(props: Record<string, unknown> = {}) { |
| 58 | + return mount(FileListFilter, { |
| 59 | + props: { |
| 60 | + isActive: false, |
| 61 | + filterName: 'Test Filter', |
| 62 | + ...props, |
| 63 | + }, |
| 64 | + slots: { |
| 65 | + default: '<div class="filter-content-stub" />', |
| 66 | + icon: '<i class="filter-icon-stub" />', |
| 67 | + }, |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + /** Finds a button stub by its visible text label */ |
| 72 | + function findButton(wrapper: ReturnType<typeof mountComponent>, label: string) { |
| 73 | + return wrapper.findAll('.nc-button-stub').find((b) => b.text().includes(label)) |
| 74 | + } |
| 75 | + |
| 76 | + it('renders filterName in the trigger button', () => { |
| 77 | + const wrapper = mountComponent() |
| 78 | + expect(wrapper.text()).toContain('Test Filter') |
| 79 | + }) |
| 80 | + |
| 81 | + it('uses tertiary variant on the trigger button when isActive is false', () => { |
| 82 | + const wrapper = mountComponent({ isActive: false }) |
| 83 | + expect(findButton(wrapper, 'Test Filter')?.attributes('data-variant')).toBe('tertiary') |
| 84 | + }) |
| 85 | + |
| 86 | + it('uses secondary variant on the trigger button when isActive is true', () => { |
| 87 | + const wrapper = mountComponent({ isActive: true }) |
| 88 | + expect(findButton(wrapper, 'Test Filter')?.attributes('data-variant')).toBe('secondary') |
| 89 | + }) |
| 90 | + |
| 91 | + it('does not render the Clear filter button when isActive is false', () => { |
| 92 | + const wrapper = mountComponent({ isActive: false }) |
| 93 | + expect(findButton(wrapper, 'Clear filter')).toBeUndefined() |
| 94 | + }) |
| 95 | + |
| 96 | + it('renders the Clear filter button when isActive is true', () => { |
| 97 | + const wrapper = mountComponent({ isActive: true }) |
| 98 | + expect(findButton(wrapper, 'Clear filter')).toBeDefined() |
| 99 | + }) |
| 100 | + |
| 101 | + it('renders the slot content inside the popover', () => { |
| 102 | + const wrapper = mountComponent() |
| 103 | + const popover = wrapper.find('.nc-popover-stub') |
| 104 | + expect(popover.find('.filter-content-stub').exists()).toBe(true) |
| 105 | + }) |
| 106 | + |
| 107 | + it('emits reset-filter when the Clear filter button is clicked', async () => { |
| 108 | + const wrapper = mountComponent({ isActive: true }) |
| 109 | + await findButton(wrapper, 'Clear filter')!.trigger('click') |
| 110 | + expect(wrapper.emitted('reset-filter')).toHaveLength(1) |
| 111 | + }) |
| 112 | +}) |
0 commit comments