Files
api/web/__test__/components/Modal.test.ts
Eli Bosley 2c62e0ad09 feat: tailwind v4 (#1522)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Streamlined Tailwind CSS integration using Vite plugin, eliminating
the need for separate Tailwind config files.
* Updated theme and color variables for improved consistency and
maintainability.

* **Style**
* Standardized spacing, sizing, and font classes across all components
using Tailwind’s default scale.
* Reduced excessive gaps, padding, and font sizes for a more compact and
cohesive UI.
* Updated gradient, border, and shadow classes to match Tailwind v4
conventions.
* Replaced custom pixel-based classes with Tailwind’s bracketed
arbitrary value syntax where needed.
* Replaced focus outline styles from `outline-none` to `outline-hidden`
for consistent focus handling.
* Updated flex shrink/grow utility classes to use newer shorthand forms.
* Converted several component templates to use self-closing tags for
cleaner markup.
  * Adjusted icon sizes and spacing for improved visual balance.

* **Chores**
* Removed legacy Tailwind/PostCSS configuration files and related
scripts.
* Updated and cleaned up package dependencies for Tailwind v4 and
related plugins.
  * Removed unused or redundant build scripts and configuration exports.
  * Updated documentation to reflect new Tailwind v4 usage.
  * Removed Prettier Tailwind plugin from formatting configurations.
* Removed Nuxt Tailwind module in favor of direct Vite plugin
integration.
  * Cleaned up ESLint config by removing Prettier integration.

* **Bug Fixes**
  * Corrected invalid or outdated Tailwind class names and syntax.
* Fixed issues with max-width and other utility classes for improved
layout consistency.

* **Tests**
* Updated test assertions to match new class names and styling
conventions.

* **Documentation**
* Revised README and internal notes to clarify Tailwind v4 adoption and
configuration changes.
* Added new development notes emphasizing Tailwind v4 usage and
documentation references.

* **UI Components**
* Enhanced BrandButton stories with detailed variant, size, and padding
showcases for better visual testing.
* Improved theme store to apply dark mode class on both `<html>` and
`<body>` elements for compatibility.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-21 09:58:02 -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-sm 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-blue-500/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);
});
});