-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upcoming: [M3-7927] - Linode Create Refactor - Part 6 - Add-ons (#10319)
* initial work * add changesets and other fixes * handle permissions in more places * diable create button if user does not have permission * add more unit testing * Apply suggestions from code review Co-authored-by: Dajahi Wiley <114682940+dwiley-akamai@users.noreply.github.com> * memoize selected region --------- Co-authored-by: Banks Nussman <banks@nussman.us> Co-authored-by: Dajahi Wiley <114682940+dwiley-akamai@users.noreply.github.com>
- Loading branch information
1 parent
a5d9483
commit d4e0d0a
Showing
20 changed files
with
696 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/api-v4": Changed | ||
--- | ||
|
||
Added jsdoc style comments to `CreateLinodeRequest` based on API documentation ([#10319](https://github.com/linode/manager/pull/10319)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/api-v4": Changed | ||
--- | ||
|
||
Allows `firewall_id` to be `null` in `CreateLinodeRequest` ([#10319](https://github.com/linode/manager/pull/10319)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/manager/.changeset/pr-10319-upcoming-features-1711487110394.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/manager": Upcoming Features | ||
--- | ||
|
||
Linode Create Refactor - Part 6 - Add-ons ([#10319](https://github.com/linode/manager/pull/10319)) |
40 changes: 40 additions & 0 deletions
40
packages/manager/src/features/Linodes/LinodeCreatev2/Addons/Addons.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React from 'react'; | ||
|
||
import { regionFactory } from 'src/factories'; | ||
import { makeResourcePage } from 'src/mocks/serverHandlers'; | ||
import { HttpResponse, http, server } from 'src/mocks/testServer'; | ||
import { renderWithThemeAndHookFormContext } from 'src/utilities/testHelpers'; | ||
|
||
import { Addons } from './Addons'; | ||
|
||
describe('Linode Create v2 Addons', () => { | ||
it('should render an "Add-ons" heading', () => { | ||
const { getByText } = renderWithThemeAndHookFormContext({ | ||
component: <Addons />, | ||
}); | ||
|
||
const heading = getByText('Add-ons'); | ||
|
||
expect(heading).toBeVisible(); | ||
expect(heading.tagName).toBe('H2'); | ||
}); | ||
|
||
it('renders a warning if an edge region is selected', async () => { | ||
const region = regionFactory.build({ site_type: 'edge' }); | ||
|
||
server.use( | ||
http.get('*/v4/regions', () => { | ||
return HttpResponse.json(makeResourcePage([region])); | ||
}) | ||
); | ||
|
||
const { findByText } = renderWithThemeAndHookFormContext({ | ||
component: <Addons />, | ||
useFormOptions: { defaultValues: { region: region.id } }, | ||
}); | ||
|
||
await findByText( | ||
'Backups and Private IP are currently not available for Edge regions.' | ||
); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/manager/src/features/Linodes/LinodeCreatev2/Addons/Addons.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { useMemo } from 'react'; | ||
import { useWatch } from 'react-hook-form'; | ||
|
||
import { Divider } from 'src/components/Divider'; | ||
import { Notice } from 'src/components/Notice/Notice'; | ||
import { Paper } from 'src/components/Paper'; | ||
import { Stack } from 'src/components/Stack'; | ||
import { Typography } from 'src/components/Typography'; | ||
import { useRegionsQuery } from 'src/queries/regions/regions'; | ||
|
||
import { Backups } from './Backups'; | ||
import { PrivateIP } from './PrivateIP'; | ||
|
||
import type { CreateLinodeRequest } from '@linode/api-v4'; | ||
|
||
export const Addons = () => { | ||
const regionId = useWatch<CreateLinodeRequest, 'region'>({ name: 'region' }); | ||
|
||
const { data: regions } = useRegionsQuery(); | ||
|
||
const selectedRegion = useMemo( | ||
() => regions?.find((r) => r.id === regionId), | ||
[regions, regionId] | ||
); | ||
|
||
const isEdgeRegionSelected = selectedRegion?.site_type === 'edge'; | ||
|
||
return ( | ||
<Paper> | ||
<Stack spacing={2}> | ||
<Typography variant="h2">Add-ons</Typography> | ||
{isEdgeRegionSelected && ( | ||
<Notice | ||
text="Backups and Private IP are currently not available for Edge regions." | ||
variant="warning" | ||
/> | ||
)} | ||
<Stack divider={<Divider />} spacing={2}> | ||
<Backups /> | ||
<PrivateIP /> | ||
</Stack> | ||
</Stack> | ||
</Paper> | ||
); | ||
}; |
114 changes: 114 additions & 0 deletions
114
packages/manager/src/features/Linodes/LinodeCreatev2/Addons/Backups.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { waitFor } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import { | ||
accountSettingsFactory, | ||
profileFactory, | ||
regionFactory, | ||
} from 'src/factories'; | ||
import { grantsFactory } from 'src/factories/grants'; | ||
import { makeResourcePage } from 'src/mocks/serverHandlers'; | ||
import { HttpResponse, http, server } from 'src/mocks/testServer'; | ||
import { renderWithThemeAndHookFormContext } from 'src/utilities/testHelpers'; | ||
|
||
import { Backups } from './Backups'; | ||
|
||
import type { CreateLinodeRequest } from '@linode/api-v4'; | ||
|
||
describe('Linode Create V2 Backups Addon', () => { | ||
it('should render a label and checkbox', () => { | ||
const { getByLabelText } = renderWithThemeAndHookFormContext({ | ||
component: <Backups />, | ||
}); | ||
|
||
const checkbox = getByLabelText('Backups', { exact: false }); | ||
|
||
expect(checkbox).toBeEnabled(); | ||
expect(checkbox).not.toBeChecked(); | ||
}); | ||
|
||
it('should get its value from the form context', () => { | ||
const { | ||
getByRole, | ||
} = renderWithThemeAndHookFormContext<CreateLinodeRequest>({ | ||
component: <Backups />, | ||
useFormOptions: { defaultValues: { backups_enabled: true } }, | ||
}); | ||
|
||
const checkbox = getByRole('checkbox'); | ||
|
||
expect(checkbox).toBeEnabled(); | ||
expect(checkbox).toBeChecked(); | ||
}); | ||
|
||
it('should render special copy, be checked, and be disabled if account backups are enabled', async () => { | ||
server.use( | ||
http.get('*/v4/account/settings', () => { | ||
return HttpResponse.json( | ||
accountSettingsFactory.build({ backups_enabled: true }) | ||
); | ||
}) | ||
); | ||
|
||
const { findByText, getByRole } = renderWithThemeAndHookFormContext({ | ||
component: <Backups />, | ||
}); | ||
|
||
const checkbox = getByRole('checkbox'); | ||
|
||
await findByText('You have enabled automatic backups for your account.', { | ||
exact: false, | ||
}); | ||
|
||
expect(checkbox).toBeDisabled(); | ||
expect(checkbox).toBeChecked(); | ||
}); | ||
|
||
it('should be disabled if an edge region is selected', async () => { | ||
const region = regionFactory.build({ site_type: 'edge' }); | ||
|
||
server.use( | ||
http.get('*/v4/regions', () => { | ||
return HttpResponse.json(makeResourcePage([region])); | ||
}) | ||
); | ||
|
||
const { | ||
getByRole, | ||
} = renderWithThemeAndHookFormContext<CreateLinodeRequest>({ | ||
component: <Backups />, | ||
useFormOptions: { defaultValues: { region: region.id } }, | ||
}); | ||
|
||
const checkbox = getByRole('checkbox'); | ||
|
||
await waitFor(() => { | ||
expect(checkbox).toBeDisabled(); | ||
}); | ||
}); | ||
|
||
it('should be disabled if the user does not have permission to create a linode', async () => { | ||
server.use( | ||
http.get('*/v4/profile', () => { | ||
return HttpResponse.json(profileFactory.build({ restricted: true })); | ||
}), | ||
http.get('*/v4/profile/grants', () => { | ||
return HttpResponse.json( | ||
grantsFactory.build({ global: { add_linodes: false } }) | ||
); | ||
}) | ||
); | ||
|
||
const { | ||
getByRole, | ||
} = renderWithThemeAndHookFormContext<CreateLinodeRequest>({ | ||
component: <Backups />, | ||
}); | ||
|
||
const checkbox = getByRole('checkbox'); | ||
|
||
await waitFor(() => { | ||
expect(checkbox).toBeDisabled(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.