import { render, screen } from '@testing-library/react'; import React from 'react'; import { MemoryRouter } from 'react-router-dom'; import { describe, expect, it, vi } from 'vitest'; import { makeAuditData, makeResult } from '../test-utils'; // Mock Headlamp lib vi.mock('@kinvolk/headlamp-plugin/lib', () => ({ ApiProxy: { request: vi.fn() }, Router: { createRouteURL: (name: string, params: Record) => `/polaris/ns/${params.namespace}`, }, })); // Mock Headlamp CommonComponents vi.mock('@kinvolk/headlamp-plugin/lib/CommonComponents', () => ({ Loader: ({ title }: { title: string }) =>
{title}
, SectionBox: ({ title, children }: { title?: string; children?: React.ReactNode }) => (
{children}
), SectionHeader: ({ title }: { title: string }) =>
{title}
, StatusLabel: ({ status, children }: { status: string; children?: React.ReactNode }) => ( {children} ), NameValueTable: ({ rows }: { rows: Array<{ name: string; value: React.ReactNode }> }) => ( {rows.map(row => ( ))}
{row.name} {row.value}
), SimpleTable: ({ columns, data, emptyMessage, }: { columns: Array<{ label: string; getter: (row: unknown) => React.ReactNode }>; data: unknown[]; emptyMessage?: string; }) => data.length === 0 ? (
{emptyMessage}
) : ( {columns.map(col => ( ))} {data.map((row, i) => ( {columns.map(col => ( ))} ))}
{col.label}
{col.getter(row)}
), })); const mockUsePolarisDataContext = vi.fn(); vi.mock('../api/PolarisDataContext', () => ({ usePolarisDataContext: () => mockUsePolarisDataContext(), })); import NamespacesListView from './NamespacesListView'; function renderWithRouter(ui: React.ReactElement) { return render({ui}); } describe('NamespacesListView', () => { it('renders loader when loading', () => { mockUsePolarisDataContext.mockReturnValue({ data: null, loading: true, error: null, }); renderWithRouter(); expect(screen.getByTestId('loader')).toHaveTextContent('Loading Polaris audit data'); }); it('renders error message when error is set', () => { mockUsePolarisDataContext.mockReturnValue({ data: null, loading: false, error: 'Polaris dashboard not reachable', }); renderWithRouter(); expect(screen.getByText('Polaris dashboard not reachable')).toBeInTheDocument(); }); it('renders "No Data" when no data and no error', () => { mockUsePolarisDataContext.mockReturnValue({ data: null, loading: false, error: null, }); renderWithRouter(); expect(screen.getByText('No Polaris audit results found.')).toBeInTheDocument(); }); it('renders namespace rows with correct scores and links', () => { const data = makeAuditData([ makeResult({ Name: 'deploy-a', Namespace: 'alpha', Results: { c1: { ID: 'c1', Message: '', Details: [], Success: true, Severity: 'warning', Category: 'X', }, }, }), makeResult({ Name: 'deploy-b', Namespace: 'beta', Results: { c2: { ID: 'c2', Message: '', Details: [], Success: false, Severity: 'danger', Category: 'X', }, }, }), ]); mockUsePolarisDataContext.mockReturnValue({ data, loading: false, error: null, }); renderWithRouter(); // Namespace links const alphaLink = screen.getByText('alpha'); expect(alphaLink.closest('a')).toHaveAttribute('href', '/polaris/ns/alpha'); const betaLink = screen.getByText('beta'); expect(betaLink.closest('a')).toHaveAttribute('href', '/polaris/ns/beta'); }); it('uses correct scoreStatus: >=80 success, >=50 warning, <50 error', () => { // Create a namespace with 100% score (1 pass) and one with 0% (1 danger) const data = makeAuditData([ makeResult({ Name: 'perfect', Namespace: 'good-ns', Results: { c1: { ID: 'c1', Message: '', Details: [], Success: true, Severity: 'warning', Category: 'X', }, }, }), makeResult({ Name: 'bad', Namespace: 'bad-ns', Results: { c2: { ID: 'c2', Message: '', Details: [], Success: false, Severity: 'danger', Category: 'X', }, }, }), ]); mockUsePolarisDataContext.mockReturnValue({ data, loading: false, error: null, }); renderWithRouter(); // Find score StatusLabels - good-ns has 100% (success), bad-ns has 0% (error) const statusLabels = screen.getAllByTestId('status-label'); const scoreLabels = statusLabels.filter(el => el.textContent?.includes('%')); const successScore = scoreLabels.find(el => el.textContent === '100%'); expect(successScore).toHaveAttribute('data-status', 'success'); const errorScore = scoreLabels.find(el => el.textContent === '0%'); expect(errorScore).toHaveAttribute('data-status', 'error'); }); });