|
| 1 | +import { fireEvent, waitForElementToBeRemoved } from '@testing-library/react'; |
| 2 | +import * as React from 'react'; |
| 3 | + |
| 4 | +import { reservedIPsFactory } from 'src/factories/networking'; |
| 5 | +import { makeResourcePage } from 'src/mocks/serverHandlers'; |
| 6 | +import { http, HttpResponse, server } from 'src/mocks/testServer'; |
| 7 | +import { mockMatchMedia, renderWithTheme } from 'src/utilities/testHelpers'; |
| 8 | + |
| 9 | +import { ReservedIpsLanding } from './ReservedIpsLanding'; |
| 10 | +import { headers } from './ReservedIpsLandingEmptyStateData'; |
| 11 | + |
| 12 | +const queryMocks = vi.hoisted(() => ({ |
| 13 | + useProfile: vi.fn().mockReturnValue({ data: { restricted: false } }), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('@linode/queries', async () => { |
| 17 | + const actual = await vi.importActual('@linode/queries'); |
| 18 | + return { |
| 19 | + ...actual, |
| 20 | + useProfile: queryMocks.useProfile, |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +beforeAll(() => mockMatchMedia()); |
| 25 | + |
| 26 | +const loadingTestId = 'circle-progress'; |
| 27 | +const reservedIPsEndpoint = '*/networking/reserved/ips'; |
| 28 | + |
| 29 | +describe('Reserved IPs Landing', () => { |
| 30 | + it('renders loading state initially', async () => { |
| 31 | + server.use( |
| 32 | + http.get(reservedIPsEndpoint, () => { |
| 33 | + return HttpResponse.json(makeResourcePage([])); |
| 34 | + }) |
| 35 | + ); |
| 36 | + |
| 37 | + const { getByTestId } = renderWithTheme(<ReservedIpsLanding />); |
| 38 | + |
| 39 | + expect(getByTestId(loadingTestId)).toBeInTheDocument(); |
| 40 | + |
| 41 | + await waitForElementToBeRemoved(getByTestId(loadingTestId), { |
| 42 | + timeout: 3000, |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('renders the empty state when there are no reserved IPs', async () => { |
| 47 | + server.use( |
| 48 | + http.get(reservedIPsEndpoint, () => { |
| 49 | + return HttpResponse.json(makeResourcePage([])); |
| 50 | + }) |
| 51 | + ); |
| 52 | + |
| 53 | + const { getByTestId, getByText } = renderWithTheme(<ReservedIpsLanding />); |
| 54 | + |
| 55 | + await waitForElementToBeRemoved(getByTestId(loadingTestId)); |
| 56 | + |
| 57 | + expect(getByText(headers.description)).toBeInTheDocument(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('renders the table with reserved IPs', async () => { |
| 61 | + const reservedIPs = reservedIPsFactory.buildList(3, { |
| 62 | + region: 'us-east', |
| 63 | + reserved: true, |
| 64 | + }); |
| 65 | + |
| 66 | + server.use( |
| 67 | + http.get(reservedIPsEndpoint, () => { |
| 68 | + return HttpResponse.json(makeResourcePage(reservedIPs)); |
| 69 | + }) |
| 70 | + ); |
| 71 | + |
| 72 | + const { getAllByText, getByTestId, queryAllByText } = renderWithTheme( |
| 73 | + <ReservedIpsLanding /> |
| 74 | + ); |
| 75 | + |
| 76 | + await waitForElementToBeRemoved(getByTestId(loadingTestId), { |
| 77 | + timeout: 3000, |
| 78 | + }); |
| 79 | + |
| 80 | + // Table column headers |
| 81 | + getAllByText('IP Address'); |
| 82 | + getAllByText('Assigned Resource'); |
| 83 | + getAllByText('Region'); |
| 84 | + getAllByText('Tags'); |
| 85 | + |
| 86 | + // Check mocked IP addresses rendered in the table |
| 87 | + queryAllByText(reservedIPs[0].address); |
| 88 | + }); |
| 89 | + |
| 90 | + it('renders the "Reserve an IP Address" button', async () => { |
| 91 | + server.use( |
| 92 | + http.get(reservedIPsEndpoint, () => { |
| 93 | + return HttpResponse.json(makeResourcePage([])); |
| 94 | + }) |
| 95 | + ); |
| 96 | + |
| 97 | + const { container, getByTestId } = renderWithTheme(<ReservedIpsLanding />); |
| 98 | + |
| 99 | + await waitForElementToBeRemoved(getByTestId(loadingTestId)); |
| 100 | + |
| 101 | + const reserveIPButton = container.querySelector('button'); |
| 102 | + |
| 103 | + expect(reserveIPButton).toBeInTheDocument(); |
| 104 | + expect(reserveIPButton).toHaveTextContent('Reserve an IP Address'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('renders a row with action menu for each reserved IP', async () => { |
| 108 | + const reservedIPs = reservedIPsFactory.buildList(3, { |
| 109 | + assigned_entity: null, |
| 110 | + reserved: true, |
| 111 | + }); |
| 112 | + |
| 113 | + server.use( |
| 114 | + http.get(reservedIPsEndpoint, () => { |
| 115 | + return HttpResponse.json(makeResourcePage(reservedIPs)); |
| 116 | + }) |
| 117 | + ); |
| 118 | + |
| 119 | + const { getByLabelText, getByTestId } = renderWithTheme( |
| 120 | + <ReservedIpsLanding /> |
| 121 | + ); |
| 122 | + |
| 123 | + await waitForElementToBeRemoved(getByTestId(loadingTestId), { |
| 124 | + timeout: 3000, |
| 125 | + }); |
| 126 | + |
| 127 | + const actionMenu = getByLabelText( |
| 128 | + `Action menu for Reserved IP ${reservedIPs[0].address}` |
| 129 | + ); |
| 130 | + expect(actionMenu).toBeInTheDocument(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('opens the action menu with correct options', async () => { |
| 134 | + const reservedIPs = reservedIPsFactory.buildList(1, { |
| 135 | + assigned_entity: null, |
| 136 | + reserved: true, |
| 137 | + }); |
| 138 | + |
| 139 | + server.use( |
| 140 | + http.get(reservedIPsEndpoint, () => { |
| 141 | + return HttpResponse.json(makeResourcePage(reservedIPs)); |
| 142 | + }) |
| 143 | + ); |
| 144 | + |
| 145 | + const { getByLabelText, getByTestId, getByText } = renderWithTheme( |
| 146 | + <ReservedIpsLanding /> |
| 147 | + ); |
| 148 | + |
| 149 | + await waitForElementToBeRemoved(getByTestId(loadingTestId), { |
| 150 | + timeout: 3000, |
| 151 | + }); |
| 152 | + |
| 153 | + const actionMenu = getByLabelText( |
| 154 | + `Action menu for Reserved IP ${reservedIPs[0].address}` |
| 155 | + ); |
| 156 | + |
| 157 | + await fireEvent.click(actionMenu); |
| 158 | + |
| 159 | + getByText('Edit'); |
| 160 | + getByText('Unreserve'); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('Restricted users', () => { |
| 164 | + it('should have the "Reserve an IP Address" button disabled for restricted users', async () => { |
| 165 | + queryMocks.useProfile.mockReturnValue({ data: { restricted: true } }); |
| 166 | + |
| 167 | + server.use( |
| 168 | + http.get(reservedIPsEndpoint, () => { |
| 169 | + return HttpResponse.json(makeResourcePage([])); |
| 170 | + }) |
| 171 | + ); |
| 172 | + |
| 173 | + const { container, getByTestId } = renderWithTheme( |
| 174 | + <ReservedIpsLanding /> |
| 175 | + ); |
| 176 | + |
| 177 | + await waitForElementToBeRemoved(getByTestId(loadingTestId)); |
| 178 | + |
| 179 | + const reserveIPButton = container.querySelector('button'); |
| 180 | + |
| 181 | + expect(reserveIPButton).toBeInTheDocument(); |
| 182 | + expect(reserveIPButton).toHaveTextContent('Reserve an IP Address'); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should have the "Reserve an IP Address" button enabled for users with full access', async () => { |
| 186 | + queryMocks.useProfile.mockReturnValue({ data: { restricted: false } }); |
| 187 | + |
| 188 | + server.use( |
| 189 | + http.get(reservedIPsEndpoint, () => { |
| 190 | + return HttpResponse.json(makeResourcePage([])); |
| 191 | + }) |
| 192 | + ); |
| 193 | + |
| 194 | + const { container, getByTestId } = renderWithTheme( |
| 195 | + <ReservedIpsLanding /> |
| 196 | + ); |
| 197 | + |
| 198 | + await waitForElementToBeRemoved(getByTestId(loadingTestId)); |
| 199 | + |
| 200 | + const reserveIPButton = container.querySelector('button'); |
| 201 | + |
| 202 | + expect(reserveIPButton).toBeInTheDocument(); |
| 203 | + expect(reserveIPButton).toHaveTextContent('Reserve an IP Address'); |
| 204 | + expect(reserveIPButton).toBeEnabled(); |
| 205 | + }); |
| 206 | + }); |
| 207 | +}); |
0 commit comments