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

333 lines
10 KiB
TypeScript

import { ref } from 'vue';
import { setActivePinia } from 'pinia';
import { mount } from '@vue/test-utils';
import { createTestingPinia } from '@pinia/testing';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { VueWrapper } from '@vue/test-utils';
import type { Server, ServerconnectPluginInstalled, ServerState } from '~/types/server';
import type { Pinia } from 'pinia';
import type { MaybeRef } from '@vueuse/core';
import UserProfile from '~/components/UserProfile.ce.vue';
import { useServerStore } from '~/store/server';
import { useThemeStore } from '~/store/theme';
const mockCopy = vi.fn();
const mockCopied = ref(false);
const mockIsSupported = ref(true);
vi.mock('@vueuse/core', () => ({
useClipboard: ({ _source }: { _source: MaybeRef<string> }) => {
const actualCopy = (text: string) => {
if (mockIsSupported.value) {
mockCopy(text);
mockCopied.value = true;
} else {
mockCopied.value = false;
}
};
return {
copy: actualCopy,
copied: mockCopied,
isSupported: mockIsSupported,
};
},
}));
const mockWatcher = vi.fn();
vi.mock('~/store/callbackActions', () => ({
useCallbackActionsStore: vi.fn(() => ({
watcher: mockWatcher,
callbackData: ref(null),
})),
}));
const t = (key: string, args?: unknown[]) => (args ? `${key} ${JSON.stringify(args)}` : key);
vi.mock('vue-i18n', () => ({
useI18n: () => ({ t }),
}));
// Test Data
const initialServerData: Server = {
name: 'TestServer',
description: 'Test Description',
guid: 'TEST-GUID',
keyfile: 'keyfile.key',
lanIp: '192.168.1.100',
connectPluginInstalled: 'dynamix.unraid.net.plg' as ServerconnectPluginInstalled,
state: 'PRO' as ServerState,
dateTimeFormat: { date: 'YYYY-MM-DD', time: 'HH:mm' },
deviceCount: 5,
flashProduct: 'TestFlash',
flashVendor: 'TestVendor',
regGuid: 'REG-GUID',
regTm: 1678886400,
regTo: 'Test User',
regTy: 'Pro',
regExp: undefined,
regUpdatesExpired: false,
registered: true,
wanIp: '8.8.8.8',
};
// Component stubs for mount options
const stubs = {
UpcUptimeExpire: { template: '<div data-testid="uptime-expire"></div>', props: ['t'] },
UpcServerState: { template: '<div data-testid="server-state"></div>', props: ['t'] },
NotificationsSidebar: { template: '<div data-testid="notifications-sidebar"></div>' },
DropdownMenu: {
template: '<div data-testid="dropdown-menu"><slot name="trigger" /><slot /></div>',
},
UpcDropdownContent: { template: '<div data-testid="dropdown-content"></div>', props: ['t'] },
UpcDropdownTrigger: { template: '<button data-testid="dropdown-trigger"></button>', props: ['t'] },
};
describe('UserProfile.ce.vue', () => {
let wrapper: VueWrapper<InstanceType<typeof UserProfile>>;
let pinia: Pinia;
let serverStore: ReturnType<typeof useServerStore>;
let themeStore: ReturnType<typeof useThemeStore>;
let consoleSpies: Array<ReturnType<typeof vi.spyOn>> = [];
beforeEach(() => {
// Suppress all console outputs
consoleSpies = [
vi.spyOn(console, 'log').mockImplementation(() => {}),
vi.spyOn(console, 'warn').mockImplementation(() => {}),
vi.spyOn(console, 'debug').mockImplementation(() => {}),
vi.spyOn(console, 'info').mockImplementation(() => {}),
vi.spyOn(console, 'error').mockImplementation(() => {}),
];
// Reset refs used by mocks
mockCopied.value = false;
mockIsSupported.value = true;
// Define simple mock Event objects instead of classes with only constructors
const MockEvent = vi.fn();
const MockMouseEvent = vi.fn();
// Set up window mocks
vi.stubGlobal('window', {
...global.window,
location: {
...global.window.location,
protocol: 'https:',
},
document: {
...global.window.document,
cookie: '',
},
Event: typeof global.window.Event === 'function' ? global.window.Event : MockEvent,
MouseEvent:
typeof global.window.MouseEvent === 'function' ? global.window.MouseEvent : MockMouseEvent,
});
pinia = createTestingPinia({
createSpy: vi.fn,
initialState: {
server: { ...initialServerData },
theme: {
theme: {
name: 'default',
banner: true,
bannerGradient: true,
descriptionShow: true,
textColor: '',
metaColor: '',
bgColor: '',
},
bannerGradient: 'linear-gradient(to right, #ff0000, #0000ff)',
},
},
stubActions: false,
});
setActivePinia(pinia);
serverStore = useServerStore();
themeStore = useThemeStore();
// Override the setServer method to prevent console logging
vi.spyOn(serverStore, 'setServer').mockImplementation((server) => {
Object.assign(serverStore, server);
return server;
});
vi.clearAllMocks();
// Mount the component
wrapper = mount(UserProfile, {
props: {
server: JSON.stringify(initialServerData),
},
global: {
plugins: [pinia],
stubs,
},
});
});
afterEach(() => {
wrapper?.unmount();
consoleSpies.forEach((spy) => spy.mockRestore());
vi.restoreAllMocks();
vi.unstubAllGlobals?.();
});
it('renders initial state correctly based on props and store', async () => {
expect(serverStore.description).toBe(initialServerData.description);
expect(themeStore.theme?.descriptionShow).toBe(true);
await wrapper.vm.$nextTick();
const heading = wrapper.find('h1');
expect(heading.text()).toContain(initialServerData.name);
expect(wrapper.find('[data-testid="uptime-expire"]').exists()).toBe(true);
expect(wrapper.find('[data-testid="server-state"]').exists()).toBe(true);
expect(wrapper.find('[data-testid="notifications-sidebar"]').exists()).toBe(true);
expect(wrapper.find('[data-testid="dropdown-menu"]').exists()).toBe(true);
expect(wrapper.find('[data-testid="dropdown-trigger"]').exists()).toBe(true);
});
it('calls server store setServer and callback watcher on mount', () => {
expect(serverStore.setServer).toHaveBeenCalledTimes(1);
expect(serverStore.setServer).toHaveBeenCalledWith(JSON.parse(JSON.stringify(initialServerData)));
expect(mockWatcher).toHaveBeenCalledTimes(1);
});
it('handles server prop as object', () => {
const wrapperObjectProp = mount(UserProfile, {
props: {
server: initialServerData,
},
global: {
plugins: [pinia],
stubs,
},
});
expect(serverStore.setServer).toHaveBeenCalledTimes(2);
expect(serverStore.setServer).toHaveBeenLastCalledWith(initialServerData);
expect(wrapperObjectProp.find('h1').text()).toContain(initialServerData.name);
wrapperObjectProp.unmount();
});
it('handles server prop not being present', () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
expect(() =>
mount(UserProfile, {
props: {},
global: {
plugins: [pinia],
stubs,
},
})
).toThrow('Server data not present');
consoleErrorSpy.mockRestore();
});
it('triggers clipboard copy when server name is clicked', async () => {
const copyLanIpSpy = vi.spyOn(wrapper.vm as unknown as { copyLanIp: () => void }, 'copyLanIp');
mockIsSupported.value = true;
const serverNameButton = wrapper.find('h1 > button');
await serverNameButton.trigger('click');
await wrapper.vm.$nextTick();
expect(copyLanIpSpy).toHaveBeenCalledTimes(1);
expect(mockCopy).toHaveBeenCalledTimes(1);
expect(mockCopy).toHaveBeenCalledWith(initialServerData.lanIp);
const copiedMessage = wrapper.find('.text-white.text-xs');
expect(copiedMessage.exists()).toBe(true);
expect(copiedMessage.text()).toContain(t('LAN IP Copied'));
copyLanIpSpy.mockRestore();
});
it('shows copy not supported message correctly', async () => {
const copyLanIpSpy = vi.spyOn(wrapper.vm as unknown as { copyLanIp: () => void }, 'copyLanIp');
mockIsSupported.value = false;
const serverNameButton = wrapper.find('h1 > button');
await serverNameButton.trigger('click');
await wrapper.vm.$nextTick();
expect(copyLanIpSpy).toHaveBeenCalledTimes(1);
expect(mockCopy).not.toHaveBeenCalled();
const notSupportedMessage = wrapper.find('.text-white.text-xs');
expect(notSupportedMessage.exists()).toBe(true);
expect(notSupportedMessage.text()).toContain(t('LAN IP {0}', [initialServerData.lanIp]));
copyLanIpSpy.mockRestore();
});
it('conditionally renders description based on theme store', async () => {
expect(serverStore.description).toBe(initialServerData.description);
expect(themeStore.theme?.descriptionShow).toBe(true);
serverStore.description = initialServerData.description!;
themeStore.theme!.descriptionShow = true;
await wrapper.vm.$nextTick();
const heading = wrapper.find('h1');
expect(heading.html()).toContain(initialServerData.description);
themeStore.theme!.descriptionShow = false;
await wrapper.vm.$nextTick();
expect(heading.html()).not.toContain(initialServerData.description);
themeStore.theme!.descriptionShow = true;
await wrapper.vm.$nextTick();
expect(heading.html()).toContain(initialServerData.description);
});
it('always renders notifications sidebar, regardless of connectPluginInstalled', async () => {
expect(wrapper.find('[data-testid="notifications-sidebar"]').exists()).toBe(true);
serverStore.connectPluginInstalled = '';
await wrapper.vm.$nextTick();
expect(wrapper.find('[data-testid="notifications-sidebar"]').exists()).toBe(true);
});
it('conditionally renders banner based on theme store', async () => {
const bannerSelector = 'div.absolute.z-0';
themeStore.theme = {
...themeStore.theme!,
banner: true,
bannerGradient: true,
};
await wrapper.vm.$nextTick();
expect(themeStore.bannerGradient).toContain('background-image: linear-gradient');
expect(wrapper.find(bannerSelector).exists()).toBe(true);
themeStore.theme!.bannerGradient = false;
await wrapper.vm.$nextTick();
expect(themeStore.bannerGradient).toBeUndefined();
expect(wrapper.find(bannerSelector).exists()).toBe(false);
themeStore.theme!.bannerGradient = true;
await wrapper.vm.$nextTick();
expect(themeStore.bannerGradient).toContain('background-image: linear-gradient');
expect(wrapper.find(bannerSelector).exists()).toBe(true);
});
});