|
| 1 | +/** |
| 2 | + * Auth Component Test Coverage |
| 3 | + */ |
| 4 | + |
| 5 | +import { ref } from 'vue'; |
| 6 | +import { mount } from '@vue/test-utils'; |
| 7 | + |
| 8 | +import { describe, expect, it, vi } from 'vitest'; |
| 9 | + |
| 10 | +import Auth from '~/components/Auth.ce.vue'; |
| 11 | + |
| 12 | +// Define types for our mocks |
| 13 | +interface AuthAction { |
| 14 | + text: string; |
| 15 | + icon: string; |
| 16 | + click?: () => void; |
| 17 | + disabled?: boolean; |
| 18 | + title?: string; |
| 19 | +} |
| 20 | + |
| 21 | +interface StateData { |
| 22 | + error: boolean; |
| 23 | + heading?: string; |
| 24 | + message?: string; |
| 25 | +} |
| 26 | + |
| 27 | +// Mock vue-i18n |
| 28 | +vi.mock('vue-i18n', () => ({ |
| 29 | + useI18n: () => ({ |
| 30 | + t: (key: string) => key, |
| 31 | + }), |
| 32 | +})); |
| 33 | + |
| 34 | +// Mock the useServerStore composable |
| 35 | +const mockServerStore = { |
| 36 | + authAction: ref<AuthAction | undefined>(undefined), |
| 37 | + stateData: ref<StateData>({ error: false }), |
| 38 | +}; |
| 39 | + |
| 40 | +vi.mock('~/store/server', () => ({ |
| 41 | + useServerStore: () => mockServerStore, |
| 42 | +})); |
| 43 | + |
| 44 | +// Mock pinia's storeToRefs to simply return the store |
| 45 | +vi.mock('pinia', () => ({ |
| 46 | + storeToRefs: (store: unknown) => store, |
| 47 | +})); |
| 48 | + |
| 49 | +describe('Auth Component', () => { |
| 50 | + it('displays an authentication button when authAction is available', () => { |
| 51 | + // Configure auth action |
| 52 | + mockServerStore.authAction.value = { |
| 53 | + text: 'Sign in to Unraid', |
| 54 | + icon: 'key', |
| 55 | + click: vi.fn(), |
| 56 | + }; |
| 57 | + mockServerStore.stateData.value = { error: false }; |
| 58 | + |
| 59 | + // Mount component |
| 60 | + const wrapper = mount(Auth); |
| 61 | + |
| 62 | + // Verify button exists |
| 63 | + const button = wrapper.findComponent({ name: 'BrandButton' }); |
| 64 | + expect(button.exists()).toBe(true); |
| 65 | + // Check props passed to button |
| 66 | + expect(button.props('text')).toBe('Sign in to Unraid'); |
| 67 | + expect(button.props('icon')).toBe('key'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('displays error messages when stateData.error is true', () => { |
| 71 | + // Configure with error state |
| 72 | + mockServerStore.authAction.value = { |
| 73 | + text: 'Sign in to Unraid', |
| 74 | + icon: 'key', |
| 75 | + }; |
| 76 | + mockServerStore.stateData.value = { |
| 77 | + error: true, |
| 78 | + heading: 'Error Title', |
| 79 | + message: 'Error Message Content', |
| 80 | + }; |
| 81 | + |
| 82 | + // Mount component |
| 83 | + const wrapper = mount(Auth); |
| 84 | + |
| 85 | + // Verify error message is displayed |
| 86 | + const errorHeading = wrapper.find('h3'); |
| 87 | + |
| 88 | + expect(errorHeading.exists()).toBe(true); |
| 89 | + expect(errorHeading.text()).toBe('Error Title'); |
| 90 | + expect(wrapper.text()).toContain('Error Message Content'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('calls the click handler when button is clicked', async () => { |
| 94 | + // Create mock click handler |
| 95 | + const clickHandler = vi.fn(); |
| 96 | + |
| 97 | + // Configure with click handler |
| 98 | + mockServerStore.authAction.value = { |
| 99 | + text: 'Sign in to Unraid', |
| 100 | + icon: 'key', |
| 101 | + click: clickHandler, |
| 102 | + }; |
| 103 | + mockServerStore.stateData.value = { error: false }; |
| 104 | + |
| 105 | + // Mount component |
| 106 | + const wrapper = mount(Auth); |
| 107 | + |
| 108 | + // Click the button |
| 109 | + await wrapper.findComponent({ name: 'BrandButton' }).vm.$emit('click'); |
| 110 | + |
| 111 | + // Verify click handler was called |
| 112 | + expect(clickHandler).toHaveBeenCalledTimes(1); |
| 113 | + }); |
| 114 | + |
| 115 | + it('does not render button when authAction is undefined', () => { |
| 116 | + // Configure with undefined auth action |
| 117 | + mockServerStore.authAction.value = undefined; |
| 118 | + mockServerStore.stateData.value = { error: false }; |
| 119 | + |
| 120 | + // Mount component |
| 121 | + const wrapper = mount(Auth); |
| 122 | + |
| 123 | + // Verify button doesn't exist |
| 124 | + const button = wrapper.findComponent({ name: 'BrandButton' }); |
| 125 | + |
| 126 | + expect(button.exists()).toBe(false); |
| 127 | + }); |
| 128 | +}); |
0 commit comments