import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import React from 'react'; import { describe, expect, it, vi } from 'vitest'; // Mock Headlamp lib vi.mock('@kinvolk/headlamp-plugin/lib', () => ({ ApiProxy: { request: vi.fn() }, })); vi.mock('@mui/material/styles', () => ({ useTheme: () => ({ palette: { primary: { main: '#1976d2', contrastText: '#fff' }, text: { primary: '#000', secondary: '#666' }, action: { disabledBackground: '#e0e0e0', disabled: '#9e9e9e' }, divider: '#e0e0e0', background: { paper: '#fff' }, }, }), })); // Mock Headlamp CommonComponents vi.mock('@kinvolk/headlamp-plugin/lib/CommonComponents', () => ({ SectionBox: ({ title, children }: { title?: string; children?: React.ReactNode }) => (
{children}
), NameValueTable: ({ rows }: { rows: Array<{ name: string; value: React.ReactNode }> }) => (
{rows.map(row => (
{row.name} {row.value}
))}
), })); import PolarisSettings from './PolarisSettings'; describe('PolarisSettings', () => { it('renders with interval from props.data', () => { render(); const select = screen.getByRole('combobox'); expect(select).toHaveValue('60'); }); it('falls back to getRefreshInterval when no prop data', () => { // Default is 300 (5 minutes) render(); const select = screen.getByRole('combobox'); expect(select).toHaveValue('300'); }); it('renders all interval options', () => { render(); const options = screen.getAllByRole('option'); expect(options).toHaveLength(4); expect(options[0]).toHaveTextContent('1 minute'); expect(options[1]).toHaveTextContent('5 minutes'); expect(options[2]).toHaveTextContent('10 minutes'); expect(options[3]).toHaveTextContent('30 minutes'); }); it('calls setRefreshInterval and onDataChange when selection changes', async () => { const onDataChange = vi.fn(); render(); const select = screen.getByRole('combobox'); await userEvent.selectOptions(select, '1800'); // Check localStorage was updated expect(localStorage.getItem('polaris-plugin-refresh-interval')).toBe('1800'); // Check callback was called with merged data expect(onDataChange).toHaveBeenCalledWith({ refreshInterval: 1800 }); }); it('works without onDataChange callback', async () => { render(); const select = screen.getByRole('combobox'); // Should not throw even without onDataChange await userEvent.selectOptions(select, '60'); expect(localStorage.getItem('polaris-plugin-refresh-interval')).toBe('60'); }); });