|
| 1 | +import { CoreUiThemeProvider } from '@scality/core-ui/dist/next'; |
| 2 | +import { coreUIAvailableThemes } from '@scality/core-ui/dist/style/theme'; |
| 3 | +import { act, render, screen, waitFor } from '@testing-library/react'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
| 5 | +import type { PropsWithChildren } from 'react'; |
| 6 | +import { QueryClient } from 'react-query'; |
| 7 | +import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom'; |
| 8 | +import { STATUS_CRITICAL, STATUS_WARNING } from '../constants'; |
| 9 | +import { QueryClientProvider } from '../QueryClientProvider'; |
| 10 | +import ActiveAlertsCounter from './ActiveAlertsCounter'; |
| 11 | + |
| 12 | +const AlertsPage = () => { |
| 13 | + const location = useLocation(); |
| 14 | + return ( |
| 15 | + <div data-testid="alerts-page"> |
| 16 | + Alerts page |
| 17 | + <span data-testid="alerts-search">{location.search}</span> |
| 18 | + </div> |
| 19 | + ); |
| 20 | +}; |
| 21 | + |
| 22 | +const defaultProps = { |
| 23 | + criticalCounter: 3, |
| 24 | + warningCounter: 5, |
| 25 | +}; |
| 26 | + |
| 27 | +const initialPath = '/nodes/node1/overview'; |
| 28 | + |
| 29 | +const wrapper = ({ children }: PropsWithChildren) => ( |
| 30 | + <QueryClientProvider client={new QueryClient()}> |
| 31 | + <CoreUiThemeProvider theme={coreUIAvailableThemes.darkRebrand}> |
| 32 | + <MemoryRouter initialEntries={[initialPath]}>{children}</MemoryRouter> |
| 33 | + </CoreUiThemeProvider> |
| 34 | + </QueryClientProvider> |
| 35 | +); |
| 36 | + |
| 37 | +const renderWithRoutes = () => |
| 38 | + render( |
| 39 | + <Routes> |
| 40 | + <Route path="/nodes/:nodeName/overview" element={<ActiveAlertsCounter {...defaultProps} />} /> |
| 41 | + <Route path="/nodes/:nodeName/alerts" element={<AlertsPage />} /> |
| 42 | + <Route path="/alerts" element={<AlertsPage />} /> |
| 43 | + </Routes>, |
| 44 | + { wrapper }, |
| 45 | + ); |
| 46 | + |
| 47 | +describe('ActiveAlertsCounter', () => { |
| 48 | + it('displays critical and warning counts', () => { |
| 49 | + renderWithRoutes(); |
| 50 | + |
| 51 | + expect(screen.getByText('Critical')).toBeInTheDocument(); |
| 52 | + expect(screen.getByText('Warning')).toBeInTheDocument(); |
| 53 | + expect(screen.getByText('3')).toBeInTheDocument(); |
| 54 | + expect(screen.getByText('5')).toBeInTheDocument(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('navigates to alerts page with severity=critical when Critical counter is clicked', async () => { |
| 58 | + const { container } = renderWithRoutes(); |
| 59 | + |
| 60 | + const criticalCounter = container.querySelector('[data-cy="critical_counter_node"]'); |
| 61 | + if (!criticalCounter) throw new Error('critical counter not found'); |
| 62 | + await act(async () => { |
| 63 | + await userEvent.click(criticalCounter); |
| 64 | + }); |
| 65 | + |
| 66 | + await waitFor(() => { |
| 67 | + expect(screen.getByTestId('alerts-page')).toBeInTheDocument(); |
| 68 | + }); |
| 69 | + expect(screen.getByText('Alerts page')).toBeInTheDocument(); |
| 70 | + expect(screen.getByTestId('alerts-search')).toHaveTextContent(`?severity=${STATUS_CRITICAL}`); |
| 71 | + }); |
| 72 | + |
| 73 | + it('navigates to alerts page with severity=warning when Warning counter is clicked', async () => { |
| 74 | + const { container } = renderWithRoutes(); |
| 75 | + |
| 76 | + const warningCounter = container.querySelector('[data-cy="warning_counter_node"]'); |
| 77 | + if (!warningCounter) throw new Error('warning counter not found'); |
| 78 | + await act(async () => { |
| 79 | + await userEvent.click(warningCounter); |
| 80 | + }); |
| 81 | + |
| 82 | + await waitFor(() => { |
| 83 | + expect(screen.getByTestId('alerts-page')).toBeInTheDocument(); |
| 84 | + }); |
| 85 | + expect(screen.getByText('Alerts page')).toBeInTheDocument(); |
| 86 | + expect(screen.getByTestId('alerts-search')).toHaveTextContent(`?severity=${STATUS_WARNING}`); |
| 87 | + }); |
| 88 | +}); |
0 commit comments