Files
api/web/__test__/components/Modal.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

230 lines
5.7 KiB
TypeScript

/**
* Modal Component Test Coverage
*/
import { nextTick } from 'vue';
import { mount } from '@vue/test-utils';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { MountingOptions, VueWrapper } from '@vue/test-utils';
import type { Props as ModalProps } from '~/components/Modal.vue';
import Modal from '~/components/Modal.vue';
vi.mock('@unraid/ui', () => ({
cn: (...args: unknown[]) => args.filter(Boolean).join(' '),
}));
const mockSetProperty = vi.fn();
const mockRemoveProperty = vi.fn();
Object.defineProperty(document.body.style, 'setProperty', {
value: mockSetProperty,
writable: true,
});
Object.defineProperty(document.body.style, 'removeProperty', {
value: mockRemoveProperty,
writable: true,
});
const t = (key: string) => key;
describe('Modal', () => {
let wrapper: VueWrapper<unknown>;
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
wrapper?.unmount();
document.body.style.removeProperty('overflow');
vi.restoreAllMocks();
});
const mountModal = (options: MountingOptions<ModalProps> = {}) => {
const { slots, ...restOptions } = options;
return mount(Modal, {
props: {
t,
open: true,
...(restOptions.props || {}),
},
slots: slots as Record<string, string>,
global: {
stubs: {
TransitionRoot: {
template: '<div v-show="show"><slot /></div>',
props: ['show'],
},
TransitionChild: {
template: '<div><slot /></div>',
},
...(restOptions.global?.stubs || {}),
},
...(restOptions.global || {}),
},
attachTo: restOptions.attachTo,
});
};
it('applies and removes body scroll lock based on open prop', async () => {
wrapper = mount(Modal, {
props: {
t,
open: false,
},
});
// Initially hidden
expect(mockSetProperty).not.toHaveBeenCalled();
await wrapper.setProps({ open: true });
await nextTick();
expect(mockSetProperty).toHaveBeenCalledWith('overflow', 'hidden');
mockSetProperty.mockClear();
mockRemoveProperty.mockClear();
await wrapper.setProps({ open: false });
await nextTick();
expect(mockRemoveProperty).toHaveBeenCalledWith('overflow');
expect(mockSetProperty).not.toHaveBeenCalled();
});
it('renders description in main content', async () => {
const testDescription = 'This is the modal description.';
wrapper = mountModal({ props: { t, description: testDescription } });
const main = wrapper.find('[class*="max-h-"]');
expect(main.find('h2').exists()).toBe(true);
expect(main.text()).toContain(testDescription);
});
it('does not emit close event on overlay click when disableOverlayClose is true', async () => {
wrapper = mountModal({ props: { t, disableOverlayClose: true } });
const overlay = wrapper.find('[class*="fixed inset-0 z-0"]');
await overlay.trigger('click');
expect(wrapper.emitted('close')).toBeUndefined();
});
it('emits close event when Escape key is pressed', async () => {
wrapper = mountModal({ attachTo: document.body });
await wrapper.find('[role="dialog"]').trigger('keyup.esc');
expect(wrapper.emitted('close')).toHaveLength(1);
});
it('applies maxWidth class correctly', async () => {
const maxWidth = 'sm:max-w-2xl';
wrapper = mount(Modal, {
props: {
t,
open: true,
maxWidth,
},
});
await nextTick();
expect(wrapper.find('[class*="sm:max-w-"]').classes()).toContain(maxWidth);
});
it('applies error and success classes correctly', async () => {
wrapper = mount(Modal, {
props: {
t,
open: true,
error: true,
},
});
await nextTick();
let modalDiv = wrapper.find('[class*="text-left relative z-10"]');
expect(modalDiv.classes()).toContain('shadow-unraid-red/30');
expect(modalDiv.classes()).toContain('border-unraid-red/10');
wrapper.setProps({ error: false, success: true });
await nextTick();
modalDiv = wrapper.find('[class*="text-left relative z-10"]');
expect(modalDiv.classes()).toContain('shadow-green-600/30');
expect(modalDiv.classes()).toContain('border-green-600/10');
});
it('disables shadow when disableShadow is true', async () => {
wrapper = mount(Modal, {
props: {
t,
open: true,
disableShadow: true,
},
});
await nextTick();
const modalDiv = wrapper.find('[class*="text-left relative z-10"]');
expect(modalDiv.classes()).toContain('shadow-none');
expect(modalDiv.classes()).toContain('border-none');
});
it('applies header justification class based on headerJustifyCenter prop', async () => {
wrapper = mount(Modal, {
props: {
t,
open: true,
headerJustifyCenter: false,
},
});
await nextTick();
expect(wrapper.find('header').classes()).toContain('justify-between');
expect(wrapper.find('header').classes()).not.toContain('justify-center');
wrapper.setProps({ headerJustifyCenter: true });
await nextTick();
expect(wrapper.find('header').classes()).toContain('justify-center');
expect(wrapper.find('header').classes()).not.toContain('justify-between');
});
it('applies overlay color and opacity classes', async () => {
const overlayColor = 'bg-blue-500';
const overlayOpacity = 'bg-opacity-50';
wrapper = mount(Modal, {
props: {
t,
open: true,
overlayColor,
overlayOpacity,
},
});
await nextTick();
const overlay = wrapper.find('[class*="fixed inset-0 z-0"]');
expect(overlay.classes()).toContain(overlayColor);
expect(overlay.classes()).toContain(overlayOpacity);
});
});