Skip to content
Open
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
8 changes: 7 additions & 1 deletion src/components/BlockSwitcher/BlockSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ export const BlockSwitcher = ({ children }: BlockSwitcherProps) => {
* convert names with spaces to valid ID attribute values
*/
const convertNameToValue = (name: string) => {
return `${name.split(' ').join('-').toLowerCase()}-${uniqueId}`;
return `${name
.replace(/\p{Emoji}/gu, '')
.split(' ')
// filter out leftover whitespace from emoji replacement
.filter(Boolean)
.join('-')
.toLowerCase()}-${uniqueId}`;
};

return (
Expand Down
31 changes: 31 additions & 0 deletions src/components/BlockSwitcher/__tests__/BlockSwitcher.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,41 @@ describe('BlockSwitcher', () => {

it('should throw an error if only a single block exists', () => {
const singleBlock = (
// @ts-expect-error we're testing for an error
<BlockSwitcher>
<Block name="JavaScript">{blockAContent}</Block>
</BlockSwitcher>
);
expect(() => render(singleBlock)).toThrow(BlockSwitcherErrorMessage);
});

it('should convert children names with spaces to values with dashes', () => {
const component = (
<BlockSwitcher>
<Block name="the first tab">sample content</Block>
<Block name="the second tab">sample content</Block>
</BlockSwitcher>
);
render(component);
const tabs = screen.getAllByRole('tab');
expect(tabs[0]).toHaveAttribute(
'id',
expect.stringMatching(/^the-first-tab\-.*/)
);
});

it('should convert children names with emojis to values without emojis', () => {
const component = (
<BlockSwitcher>
<Block name="🚀 rocket">sample content</Block>
<Block name="a second tab">sample content</Block>
</BlockSwitcher>
);
render(component);
const tabs = screen.getAllByRole('tab');
expect(tabs[0]).toHaveAttribute(
'id',
expect.stringMatching(/^rocket\-.*\-tab$/)
);
});
});