Skip to content

Commit 66a4729

Browse files
committed
test: fix broken tests from chrome motion PR
- Make matchMedia mock unconditional in setup.js (was skipped when JSDOM already defined it) - Stub matchMedia for mobile breakpoint in AlgorithmInsightPanel tests (useIsBelowLg uses matchMedia, not innerWidth) - Add visualizationsRemaining to ControlPanel mock in VisualizerApp tests
1 parent f49d1c3 commit 66a4729

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

src/components/AlgorithmInsightPanel.test.jsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,28 @@ import { fireEvent, renderWithI18n, screen, waitFor } from '../test/testUtils';
99
import AlgorithmInsightPanel from './AlgorithmInsightPanel';
1010
import i18n from '../i18n';
1111
import { ALGORITHM_TYPES } from '@/constants';
12+
import { BELOW_LG_MEDIA_QUERY } from '../hooks/useIsBelowLg';
13+
14+
function stubMatchMedia(matchesMobile) {
15+
vi.stubGlobal(
16+
'matchMedia',
17+
vi.fn(query => ({
18+
matches: query === BELOW_LG_MEDIA_QUERY ? matchesMobile : false,
19+
media: query,
20+
onchange: null,
21+
addEventListener: vi.fn(),
22+
removeEventListener: vi.fn(),
23+
addListener: vi.fn(),
24+
removeListener: vi.fn(),
25+
dispatchEvent: vi.fn(() => false),
26+
}))
27+
);
28+
}
1229

1330
describe('AlgorithmInsightPanel', () => {
1431
beforeEach(async () => {
1532
await i18n.changeLanguage('en');
33+
stubMatchMedia(false);
1634
});
1735

1836
it('renders insight content and icon placeholders when open', () => {
@@ -39,11 +57,7 @@ describe('AlgorithmInsightPanel', () => {
3957
});
4058

4159
it('calls onClose when the mobile close button is clicked', () => {
42-
Object.defineProperty(window, 'innerWidth', {
43-
writable: true,
44-
configurable: true,
45-
value: 375,
46-
});
60+
stubMatchMedia(true);
4761
const onClose = vi.fn();
4862
renderWithI18n(
4963
<AlgorithmInsightPanel
@@ -63,11 +77,7 @@ describe('AlgorithmInsightPanel', () => {
6377
});
6478

6579
it('calls onClose when backdrop is clicked on desktop', () => {
66-
Object.defineProperty(window, 'innerWidth', {
67-
writable: true,
68-
configurable: true,
69-
value: 1024,
70-
});
80+
stubMatchMedia(false);
7181
const onClose = vi.fn();
7282
const { container } = renderWithI18n(
7383
<AlgorithmInsightPanel

src/pages/VisualizerApp.test.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ vi.mock('../components/ControlPanel', () => ({
299299
sortOrder,
300300
onSortOrderChange,
301301
onToggleFullScreen,
302+
visualizationsRemaining,
302303
}) => (
303304
<div data-testid="control-panel">
304305
<span data-testid="control-total-steps">{String(totalSteps)}</span>
@@ -316,6 +317,12 @@ vi.mock('../components/ControlPanel', () => ({
316317
<button type="button" onClick={onToggleFullScreen}>
317318
toggle-fullscreen
318319
</button>
320+
{visualizationsRemaining != null &&
321+
Number.isFinite(visualizationsRemaining) ? (
322+
<p role="status">
323+
{`${visualizationsRemaining} visualizations remaining`}
324+
</p>
325+
) : null}
319326
{algorithmType === 'sorting' && (
320327
<button
321328
type="button"

src/test/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ vi.mock('framer-motion', async () => {
216216
return createFramerMotionMock();
217217
});
218218

219-
if (typeof window !== 'undefined' && typeof window.matchMedia !== 'function') {
219+
if (typeof window !== 'undefined') {
220220
Object.defineProperty(window, 'matchMedia', {
221221
writable: true,
222222
configurable: true,

0 commit comments

Comments
 (0)