Files
api/web/__test__/components/Auth.test.ts
Michael Datelle 32c6fe6295 test: create tests for components batch 2 (#1365)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Refactor**
- Improved component tests by integrating Pinia's testing utilities for
more reliable store mocking and state management.
- Updated test setup to streamline plugin usage and remove unnecessary
configuration.
- Enhanced test clarity by relying on store state changes and Vue's
reactivity instead of manual mock updates.
- Simplified test cases by focusing on passed props and standardized
store mocking.
- **Chores**
  - Updated test directory structure for better organization.
  - Added additional test mocks for dependencies.
- **New Features**
- Added comprehensive tests for the ColorSwitcher component, verifying
UI elements and theme store interactions.
- Introduced tests for the DevSettings component, confirming UI
rendering and interaction behavior.
- Added detailed tests for the DowngradeOs component covering store
interactions and conditional UI rendering.
- Added new test suites for DummyServerSwitcher component, ensuring
correct rendering and reactive state updates.
- Added new test suites for HeaderOsVersion component, validating
version badge rendering and error state handling.
- Added new test suite for the I18nHost component, validating
internationalization context provisioning and error handling.
- Added a comprehensive test suite for the Modal component, covering UI
behavior, styling, and event handling.
- Added new test suite for the Registration component, verifying
rendering based on server state.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: mdatelle <mike@datelle.net>
2025-04-29 12:44:31 -04:00

143 lines
3.5 KiB
TypeScript

/**
* Auth Component Test Coverage
*/
import { nextTick, ref } from 'vue';
import { mount } from '@vue/test-utils';
import { GlobeAltIcon } from '@heroicons/vue/24/solid';
import { createTestingPinia } from '@pinia/testing';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { ServerconnectPluginInstalled } from '~/types/server';
import Auth from '~/components/Auth.ce.vue';
import { useServerStore } from '~/store/server';
vi.mock('vue-i18n', () => ({
useI18n: () => ({
t: (key: string) => key,
}),
}));
vi.mock('crypto-js/aes', () => ({
default: {},
}));
vi.mock('@unraid/shared-callbacks', () => ({
useCallback: vi.fn(() => ({
send: vi.fn(),
watcher: vi.fn(),
})),
}));
const mockAccountStore = {
signIn: vi.fn(),
};
vi.mock('~/store/account', () => ({
useAccountStore: () => mockAccountStore,
}));
vi.mock('~/store/activationCode', () => ({
useActivationCodeStore: vi.fn(() => ({
code: ref(null),
partnerName: ref(null),
})),
}));
describe('Auth Component', () => {
let serverStore: ReturnType<typeof useServerStore>;
beforeEach(() => {
vi.clearAllMocks();
});
it('displays an authentication button when authAction is available', async () => {
const wrapper = mount(Auth, {
global: {
plugins: [createTestingPinia({ createSpy: vi.fn })],
},
});
// Patch the underlying state that `authAction` depends on
serverStore = useServerStore();
serverStore.$patch({
state: 'ENOKEYFILE',
registered: false,
connectPluginInstalled: 'INSTALLED' as ServerconnectPluginInstalled,
});
await nextTick();
const button = wrapper.findComponent({ name: 'BrandButton' });
expect(button.exists()).toBe(true);
expect(button.props('text')).toBe('Sign In with Unraid.net Account');
expect(button.props('icon')).toBe(GlobeAltIcon);
});
it('displays error messages when stateData.error is true', () => {
const wrapper = mount(Auth, {
global: {
plugins: [createTestingPinia({ createSpy: vi.fn })],
},
});
// Patch the underlying state that `stateData` depends on
serverStore = useServerStore();
serverStore.$patch({
state: 'EEXPIRED',
registered: false,
connectPluginInstalled: 'INSTALLED' as ServerconnectPluginInstalled,
});
const errorHeading = wrapper.find('h3');
expect(errorHeading.exists()).toBe(true);
expect(errorHeading.text()).toBe('Stale Server');
expect(wrapper.text()).toContain(
'Please refresh the page to ensure you load your latest configuration'
);
});
it('calls the click handler when button is clicked', async () => {
const wrapper = mount(Auth, {
global: {
plugins: [createTestingPinia({ createSpy: vi.fn })],
},
});
serverStore = useServerStore();
serverStore.$patch({
state: 'ENOKEYFILE',
registered: false,
connectPluginInstalled: 'INSTALLED' as ServerconnectPluginInstalled,
});
await nextTick();
await wrapper.findComponent({ name: 'BrandButton' }).vm.$emit('click');
expect(mockAccountStore.signIn).toHaveBeenCalledTimes(1);
});
it('does not render button when authAction is undefined', () => {
const wrapper = mount(Auth, {
global: {
plugins: [createTestingPinia({ createSpy: vi.fn })],
},
});
serverStore = useServerStore();
serverStore.$patch({
state: 'PRO',
registered: true,
});
const button = wrapper.findComponent({ name: 'BrandButton' });
expect(button.exists()).toBe(false);
});
});