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

Fix: Ensure UUIDTag Component Handles Non-String UUID Values Safely #1803

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions src/components/common/UUIDTag.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import UUIDTag from './UUIDTag';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, this is great


describe('UUIDTag', () => {
beforeEach(() => {
cy.viewport(500, 500);
});

it('renders correctly with a valid UUID string', () => {
const testId = '12345678';
cy.mount(<UUIDTag uuid={testId} />);
cy.contains('1234...5678').should('exist');
});

it('handles non-string UUID values gracefully', () => {
const testIdNonString = 12345678;
cy.mount(<UUIDTag uuid={testIdNonString} />);
cy.contains('1234...5678').should('exist');
});

it('displays a fallback for undefined UUID', () => {
cy.mount(<UUIDTag uuid={undefined} />);
cy.contains('...').should('exist');
});
});
10 changes: 6 additions & 4 deletions src/components/common/UUIDTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { Box, Tooltip, Typography } from '@mui/material';
import { useClipboard } from 'hooks/globalHooks';

function UUIDTag({ uuid, sx }) {
const formattedId = `${uuid.slice(0, 4)}...${uuid.slice(
uuid.length - 4,
uuid.length,
const id = typeof uuid === 'string' ? uuid : String(uuid || '');

const formattedId = `${id.slice(0, 4)}...${id.slice(
id.length - 4,
id.length,
)}`;

const { onCopy, hasCopied } = useClipboard(uuid);
const { onCopy, hasCopied } = useClipboard(id);

const title = (
<>
Expand Down
Loading