|
| 1 | +import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest' |
| 2 | +import { mount } from '@vue/test-utils' |
| 3 | +import SyncStatusButton from './SyncStatusButton.vue' |
| 4 | +import { useIsFetching, useQueryClient } from '@tanstack/vue-query' |
| 5 | +import { useEnhancedToast } from '@/shared/composables/useEnhancedToast' |
| 6 | + |
| 7 | +// Mock TanStack Query |
| 8 | +vi.mock('@tanstack/vue-query', () => ({ |
| 9 | + useIsFetching: vi.fn(), |
| 10 | + useQueryClient: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +// Mock Enhanced Toast |
| 14 | +vi.mock('@/shared/composables/useEnhancedToast', () => ({ |
| 15 | + useEnhancedToast: vi.fn(), |
| 16 | +})) |
| 17 | + |
| 18 | +// Mock Iconify |
| 19 | +vi.mock('@iconify/vue', () => ({ |
| 20 | + Icon: { template: '<span>icon</span>' }, |
| 21 | +})) |
| 22 | + |
| 23 | +describe('SyncStatusButton', () => { |
| 24 | + let mockQueryClient: { invalidateQueries: Mock } |
| 25 | + let mockShowSuccess: Mock |
| 26 | + let mockShowError: Mock |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + vi.clearAllMocks() |
| 30 | + |
| 31 | + mockQueryClient = { |
| 32 | + invalidateQueries: vi.fn().mockResolvedValue(undefined), |
| 33 | + } |
| 34 | + ;(useQueryClient as Mock).mockReturnValue(mockQueryClient) |
| 35 | + ;(useIsFetching as Mock).mockReturnValue({ value: 0 }) |
| 36 | + |
| 37 | + mockShowSuccess = vi.fn() |
| 38 | + mockShowError = vi.fn() |
| 39 | + ;(useEnhancedToast as Mock).mockReturnValue({ |
| 40 | + showSuccess: mockShowSuccess, |
| 41 | + showError: mockShowError, |
| 42 | + }) |
| 43 | + |
| 44 | + // Silence console during tests if needed |
| 45 | + vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should call invalidateQueries and show success toast on click', async () => { |
| 49 | + vi.useFakeTimers() |
| 50 | + const wrapper = mount(SyncStatusButton, { |
| 51 | + global: { |
| 52 | + stubs: { |
| 53 | + TooltipProvider: { template: '<div><slot /></div>' }, |
| 54 | + Tooltip: { template: '<div><slot /></div>' }, |
| 55 | + TooltipTrigger: { template: '<div><slot /></div>' }, |
| 56 | + TooltipContent: { template: '<div><slot /></div>' }, |
| 57 | + Button: { |
| 58 | + template: '<button @click="$emit(\'click\')"><slot /></button>', |
| 59 | + props: ['disabled'], |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }) |
| 64 | + |
| 65 | + const button = wrapper.find('button') |
| 66 | + await button.trigger('click') |
| 67 | + |
| 68 | + // After click, it should have started invalidation |
| 69 | + expect(mockQueryClient.invalidateQueries).toHaveBeenCalled() |
| 70 | + |
| 71 | + // Wait for the minimum duration (600ms) |
| 72 | + await vi.advanceTimersByTimeAsync(600) |
| 73 | + |
| 74 | + expect(mockShowSuccess).toHaveBeenCalledWith( |
| 75 | + 'Data synchronized', |
| 76 | + 'All active data views have been updated.' |
| 77 | + ) |
| 78 | + vi.useRealTimers() |
| 79 | + }) |
| 80 | + |
| 81 | + it('should show error toast and mark error as silent on failure', async () => { |
| 82 | + vi.useFakeTimers() |
| 83 | + const error = new Error('Sync failed') |
| 84 | + mockQueryClient.invalidateQueries.mockRejectedValue(error) |
| 85 | + |
| 86 | + const wrapper = mount(SyncStatusButton, { |
| 87 | + global: { |
| 88 | + stubs: { |
| 89 | + TooltipProvider: { template: '<div><slot /></div>' }, |
| 90 | + Tooltip: { template: '<div><slot /></div>' }, |
| 91 | + TooltipTrigger: { template: '<div><slot /></div>' }, |
| 92 | + TooltipContent: { template: '<div><slot /></div>' }, |
| 93 | + Button: { |
| 94 | + template: '<button @click="$emit(\'click\')"><slot /></button>', |
| 95 | + props: ['disabled'], |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }) |
| 100 | + |
| 101 | + const button = wrapper.find('button') |
| 102 | + await button.trigger('click') |
| 103 | + |
| 104 | + await vi.advanceTimersByTimeAsync(600) |
| 105 | + |
| 106 | + expect(mockShowError).toHaveBeenCalledWith(error, 'Sync Failed') |
| 107 | + expect((error as Error & { silent?: boolean }).silent).toBe(true) |
| 108 | + vi.useRealTimers() |
| 109 | + }) |
| 110 | +}) |
0 commit comments