Skip to content

Commit a475b6f

Browse files
committed
test(react-list): fix failing rit test
1 parent 19f8a64 commit a475b6f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

packages/react-components/react-list/library/src/components/List/List.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import { ListProps } from './List.types';
66
import { ListItem } from '../ListItem/ListItem';
77
import { ListItemActionEventData } from '../ListItem/ListItem.types';
88
import { EventHandler } from '@fluentui/react-utilities';
9+
import { resetIdsForTests } from '@fluentui/react-utilities';
10+
11+
// Mock useId from @fluentui/react-utilities to return CSS-safe IDs without special characters.
12+
// React's useId generates IDs with commas that create invalid CSS selectors in JSDOM 25+,
13+
// causing console.error logs that React 19's act() collects as AggregateError.
14+
jest.mock('@fluentui/react-utilities', () => ({
15+
...jest.requireActual('@fluentui/react-utilities'),
16+
...jest.requireActual('../../testing/createUseIdMock').createUseIdMock(),
17+
}));
918

1019
function expectListboxItemSelected(item: HTMLElement, selected: boolean) {
1120
expect(item.getAttribute('aria-selected')).toBe(selected.toString());
@@ -58,8 +67,13 @@ describe('List', () => {
5867
// and false warnings about the mismatched roles because of tabster not working reliably in tests.
5968
const consoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => jest.fn());
6069

70+
afterEach(() => {
71+
resetIdsForTests();
72+
});
73+
6174
afterAll(() => {
6275
consoleWarn.mockRestore();
76+
jest.clearAllMocks();
6377
});
6478

6579
describe('rendering', () => {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use client';
2+
3+
import * as React from 'react';
4+
5+
export function createUseIdMock(): { useId: (prefix: string) => string; resetIdsForTests: () => void } {
6+
let idCounter = 0;
7+
8+
return {
9+
useId: jest.fn().mockImplementation((prefix: string) => {
10+
return React.useMemo(() => {
11+
idCounter += 1;
12+
return `${prefix}${idCounter}`;
13+
}, [prefix]);
14+
}),
15+
resetIdsForTests: () => {
16+
idCounter = 0;
17+
},
18+
};
19+
}

0 commit comments

Comments
 (0)