Skip to content

Commit f682c52

Browse files
authored
Merge pull request #1803 from yunchipang/fix/handle-non-string-uuid
Fix: Ensure UUIDTag Component Handles Non-String UUID Values Safely
2 parents c6b429e + d200917 commit f682c52

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/components/common/UUIDTag.cy.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import UUIDTag from './UUIDTag';
2+
3+
describe('UUIDTag', () => {
4+
beforeEach(() => {
5+
cy.viewport(500, 500);
6+
});
7+
8+
it('renders correctly with a valid UUID string', () => {
9+
const testId = '12345678';
10+
cy.mount(<UUIDTag uuid={testId} />);
11+
cy.contains('1234...5678').should('exist');
12+
});
13+
14+
it('handles non-string UUID values gracefully', () => {
15+
const testIdNonString = 12345678;
16+
cy.mount(<UUIDTag uuid={testIdNonString} />);
17+
cy.contains('1234...5678').should('exist');
18+
});
19+
20+
it('displays a fallback for undefined UUID', () => {
21+
cy.mount(<UUIDTag uuid={undefined} />);
22+
cy.contains('...').should('exist');
23+
});
24+
});

src/components/common/UUIDTag.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { Box, Tooltip, Typography } from '@mui/material';
22
import { useClipboard } from 'hooks/globalHooks';
33

44
function UUIDTag({ uuid, sx }) {
5-
const formattedId = `${uuid.slice(0, 4)}...${uuid.slice(
6-
uuid.length - 4,
7-
uuid.length,
5+
const id = typeof uuid === 'string' ? uuid : String(uuid || '');
6+
7+
const formattedId = `${id.slice(0, 4)}...${id.slice(
8+
id.length - 4,
9+
id.length,
810
)}`;
911

10-
const { onCopy, hasCopied } = useClipboard(uuid);
12+
const { onCopy, hasCopied } = useClipboard(id);
1113

1214
const title = (
1315
<>

0 commit comments

Comments
 (0)