Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Unit test coverage - HostNameTableCell #10596

Merged
merged 7 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-10596-tests-1718806691101.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Tests
---

Unit test coverage - HostNameTableCell ([#10596](https://github.com/linode/manager/pull/10596))
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import '@testing-library/jest-dom';
import { waitFor } from '@testing-library/react';
import React from 'react';

import { objectStorageKeyFactory, regionFactory } from 'src/factories';
import { makeResourcePage } from 'src/mocks/serverHandlers';
import { HttpResponse, http, server } from 'src/mocks/testServer';
import { renderWithTheme } from 'src/utilities/testHelpers';

import { HostNameTableCell } from './HostNameTableCell';

const storageKeyData = objectStorageKeyFactory.build({
regions: [
{
id: 'us-east',
s3_endpoint: 'alpha.test.com',
},
],
});

describe('HostNameTableCell', () => {
it('should render "None" when there are no regions', () => {
const { getByText } = renderWithTheme(
<HostNameTableCell
storageKeyData={{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could use the factory here as well.

access_key: 'test_key',
bucket_access: null,
id: 0,
label: 'test',
limited: false,
regions: [],
secret_key: '',
}}
setHostNames={vi.fn()}
setShowHostNamesDrawers={vi.fn()}
/>
);

expect(getByText('None')).toBeInTheDocument();
});

test('should render "Regions/S3 Hostnames" cell when there are regions', async () => {
const region = regionFactory.build({
capabilities: ['Object Storage'],
id: 'us-east',
label: 'Newark, NJ',
});

server.use(
http.get('*/v4/regions', () => {
return HttpResponse.json(makeResourcePage([region]));
})
);
const { findByText } = renderWithTheme(
<HostNameTableCell
setHostNames={vi.fn()}
setShowHostNamesDrawers={vi.fn()}
storageKeyData={storageKeyData}
/>
);

const hostname = await findByText('Newark, NJ: alpha.test.com');

await waitFor(() => expect(hostname).toBeInTheDocument());
});
test('should render all "Regions/S3 Hostnames" in the cell when there are multiple regions', async () => {
const region = regionFactory.build({
capabilities: ['Object Storage'],
id: 'us-east',
label: 'Newark, NJ',
});

server.use(
http.get('*/v4/regions', () => {
return HttpResponse.json(makeResourcePage([region]));
})
);
const { findByText } = renderWithTheme(
<HostNameTableCell
storageKeyData={{
...storageKeyData,
regions: [
{
id: 'us-east',
s3_endpoint: 'alpha.test.com',
},
{
id: 'us-south',
s3_endpoint: 'alpha.test.com',
},
],
}}
setHostNames={vi.fn()}
setShowHostNamesDrawers={vi.fn()}
/>
);
const hostname = await findByText('Newark, NJ: alpha.test.com');
const moreButton = await findByText(/and\s+1\s+more\.\.\./);
await waitFor(() => expect(hostname).toBeInTheDocument());

await expect(moreButton).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
ObjectStorageKey,
RegionS3EndpointAndID,
} from '@linode/api-v4/lib/object-storage';
import { styled } from '@mui/material/styles';
import React from 'react';

Expand All @@ -11,6 +7,11 @@ import { TableCell } from 'src/components/TableCell';
import { useRegionsQuery } from 'src/queries/regions/regions';
import { getRegionsByRegionId } from 'src/utilities/regions';

import type {
ObjectStorageKey,
RegionS3EndpointAndID,
} from '@linode/api-v4/lib/object-storage';

type Props = {
setHostNames: (hostNames: RegionS3EndpointAndID[]) => void;
setShowHostNamesDrawers: (show: boolean) => void;
Expand All @@ -31,14 +32,14 @@ export const HostNameTableCell = ({
if (!regionsLookup || !regionsData || !regions || regions.length === 0) {
return <TableCell>None</TableCell>;
}
const label = regionsLookup[storageKeyData.regions[0].id]?.label;
const s3Endpoint = storageKeyData?.regions[0]?.s3_endpoint;

return (
<TableCell>
{`${regionsLookup[storageKeyData.regions[0].id].label}: ${
storageKeyData?.regions[0]?.s3_endpoint
} `}
{`${label}: ${s3Endpoint} `}
{storageKeyData?.regions?.length === 1 && (
cpathipa marked this conversation as resolved.
Show resolved Hide resolved
<StyledCopyIcon text={storageKeyData.regions[0].s3_endpoint} />
<StyledCopyIcon text={s3Endpoint} />
)}
{storageKeyData.regions.length > 1 && (
<StyledLinkButton
Expand Down
Loading