import { screen, fireEvent, waitFor } from '@testing-library/react'; import { ComponentKind, NudgeStats } from '../../../../types'; import { mockNamespaceHooks } from '../../../../unit-test-utils/mock-namespace'; import { routerRenderer } from '../../../../utils/test-utils'; import ComponentNudgesDependencies from '../ComponentNudgesDependencies'; const mockComponent = { metadata: { name: 'component' }, spec: { application: 'application' }, } as ComponentKind; const mockAllComponents = [ { metadata: { name: 'cmp1' }, spec: { application: 'app1' } }, { metadata: { name: 'cmp2' }, spec: { application: 'app2' } }, { metadata: { name: 'cmp3' }, spec: { application: 'app3' } }, { metadata: { name: 'cmp4' }, spec: { application: 'app4' } }, { metadata: { name: 'cmp5' }, spec: { application: 'app5' } }, ] as ComponentKind[]; jest.mock('../../../../hooks/useComponents', () => ({ useAllComponents: jest.fn(() => [mockAllComponents, true, undefined]), })); mockNamespaceHooks('useNamespace', 'test-ns'); describe('ComponentNudgesDependencies', () => { it('should render empty state when no dependencies', () => { routerRenderer(); screen.queryByTestId('nudges-empty-state'); }); it('should render 3 nudges dependencies', async () => { const cmp = { ...mockComponent, spec: { [NudgeStats.NUDGES]: ['cmp1', 'cmp2', 'cmp3'] }, } as ComponentKind; routerRenderer(); fireEvent.click(screen.getByTestId('nudges-radio')); await waitFor(() => { expect(screen.getByText('cmp1')).toBeInTheDocument(); expect(screen.getByText('cmp2')).toBeInTheDocument(); expect(screen.getByText('cmp3')).toBeInTheDocument(); }); }); it('should render 3 connectors', () => { const cmp = { ...mockComponent, spec: { [NudgeStats.NUDGES]: ['cmp1', 'cmp2', 'cmp3'] }, } as ComponentKind; routerRenderer(); fireEvent.click(screen.getByTestId('nudges-radio')); const connectors = screen.queryAllByTestId('nudges-connector'); expect(connectors.length).toBe(3); }); it('should render correct links', () => { const cmp = { ...mockComponent, metadata: { name: 'test' }, spec: { application: 'testApp', [NudgeStats.NUDGES]: ['cmp1', 'cmp2', 'cmp3'] }, } as ComponentKind; routerRenderer(); fireEvent.click(screen.getByTestId('nudges-radio')); const links = screen.queryAllByTestId('nudges-cmp-link'); expect(links.length).toBe(3); expect(links[0].getAttribute('href')).toBe('/ns/test-ns/applications/app1/components/cmp1'); }); }); describe('ComponentNudgesDependencies nudged by', () => { it('should render 4 nudged-by dependencies', async () => { const cmp = { ...mockComponent, status: { [NudgeStats.NUDGED_BY]: ['cmp1', 'cmp2', 'cmp3', 'cmp4'] }, } as ComponentKind; routerRenderer(); fireEvent.click(screen.getByTestId('nudged-by-radio')); await waitFor(() => { expect(screen.getByText('cmp1')).toBeInTheDocument(); expect(screen.getByText('cmp2')).toBeInTheDocument(); expect(screen.getByText('cmp3')).toBeInTheDocument(); expect(screen.getByText('cmp4')).toBeInTheDocument(); }); }); it('should render 4 connectors', () => { const cmp = { ...mockComponent, status: { [NudgeStats.NUDGED_BY]: ['cmp1', 'cmp2', 'cmp3', 'cmp4'] }, spec: { application: 'testApp' }, } as ComponentKind; routerRenderer(); fireEvent.click(screen.getByTestId('nudged-by-radio')); const connectors = screen.queryAllByTestId('nudges-connector'); expect(connectors.length).toBe(4); }); it('should render correct links', () => { const cmp = { ...mockComponent, metadata: { name: 'test' }, status: { [NudgeStats.NUDGED_BY]: ['cmp1', 'cmp2'] }, spec: { application: 'testApp' }, } as ComponentKind; routerRenderer(); fireEvent.click(screen.getByTestId('nudged-by-radio')); const links = screen.queryAllByTestId('nudged-by-cmp-link'); expect(links.length).toBe(2); expect(links[1].getAttribute('href')).toBe('/ns/test-ns/applications/app2/components/cmp2'); }); });