|
| 1 | +import { waitFor, waitForElementToBeRemoved } from '@testing-library/react'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { afterAll, afterEach, beforeAll, describe, it } from 'vitest'; |
| 4 | + |
| 5 | +import { |
| 6 | + firewallFactory, |
| 7 | + subnetAssignedNodebalancerDataFactory, |
| 8 | +} from 'src/factories'; |
| 9 | +import { makeResourcePage } from 'src/mocks/serverHandlers'; |
| 10 | +import { http, HttpResponse, server } from 'src/mocks/testServer'; |
| 11 | +import { |
| 12 | + mockMatchMedia, |
| 13 | + renderWithTheme, |
| 14 | + wrapWithTableBody, |
| 15 | +} from 'src/utilities/testHelpers'; |
| 16 | + |
| 17 | +import { SubnetNodeBalancerRow } from './SubnetNodebalancerRow'; |
| 18 | + |
| 19 | +const LOADING_TEST_ID = 'circle-progress'; |
| 20 | + |
| 21 | +beforeAll(() => mockMatchMedia()); |
| 22 | +afterEach(() => server.resetHandlers()); |
| 23 | +afterAll(() => server.close()); |
| 24 | + |
| 25 | +describe('SubnetNodeBalancerRow', () => { |
| 26 | + const nodebalancer = { |
| 27 | + id: 123, |
| 28 | + label: 'test-nodebalancer', |
| 29 | + }; |
| 30 | + |
| 31 | + const configs = [ |
| 32 | + { nodes_status: { up: 3, down: 1 } }, |
| 33 | + { nodes_status: { up: 2, down: 2 } }, |
| 34 | + ]; |
| 35 | + |
| 36 | + const firewalls = makeResourcePage( |
| 37 | + firewallFactory.buildList(1, { label: 'mock-firewall' }) |
| 38 | + ); |
| 39 | + |
| 40 | + const subnetNodebalancer = subnetAssignedNodebalancerDataFactory.build({ |
| 41 | + id: nodebalancer.id, |
| 42 | + ipv4_range: '192.168.99.0/30', |
| 43 | + }); |
| 44 | + |
| 45 | + it('renders loading state', async () => { |
| 46 | + const { getByTestId } = renderWithTheme( |
| 47 | + wrapWithTableBody( |
| 48 | + <SubnetNodeBalancerRow |
| 49 | + ipv4={subnetNodebalancer.ipv4_range} |
| 50 | + nodeBalancerId={subnetNodebalancer.id} |
| 51 | + /> |
| 52 | + ) |
| 53 | + ); |
| 54 | + |
| 55 | + expect(getByTestId(LOADING_TEST_ID)).toBeInTheDocument(); |
| 56 | + await waitForElementToBeRemoved(() => getByTestId(LOADING_TEST_ID)); |
| 57 | + }); |
| 58 | + |
| 59 | + it('renders nodebalancer row with data', async () => { |
| 60 | + server.use( |
| 61 | + http.get('*/nodebalancers/:id', () => { |
| 62 | + return HttpResponse.json(nodebalancer); |
| 63 | + }), |
| 64 | + http.get('*/nodebalancers/:id/configs', () => { |
| 65 | + return HttpResponse.json(configs); |
| 66 | + }), |
| 67 | + http.get('*/nodebalancers/:id/firewalls', () => { |
| 68 | + return HttpResponse.json(firewalls); |
| 69 | + }) |
| 70 | + ); |
| 71 | + |
| 72 | + const { getByText, getByRole } = renderWithTheme( |
| 73 | + wrapWithTableBody( |
| 74 | + <SubnetNodeBalancerRow |
| 75 | + ipv4={subnetNodebalancer.ipv4_range} |
| 76 | + nodeBalancerId={nodebalancer.id} |
| 77 | + /> |
| 78 | + ) |
| 79 | + ); |
| 80 | + |
| 81 | + await waitFor(() => { |
| 82 | + expect(getByText(nodebalancer.label)).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + |
| 85 | + expect(getByText(subnetNodebalancer.ipv4_range)).toBeInTheDocument(); |
| 86 | + expect(getByText('mock-firewall')).toBeInTheDocument(); |
| 87 | + |
| 88 | + const nodebalancerLink = getByRole('link', { |
| 89 | + name: nodebalancer.label, |
| 90 | + }); |
| 91 | + |
| 92 | + expect(nodebalancerLink).toHaveAttribute( |
| 93 | + 'href', |
| 94 | + `/nodebalancers/${nodebalancer.id}/summary` |
| 95 | + ); |
| 96 | + }); |
| 97 | +}); |
0 commit comments