Skip to content

Commit 94c9a12

Browse files
committed
fix(modal): use try-finally blocks in focusModalChildElement tests
1 parent 63ba35d commit 94c9a12

File tree

1 file changed

+85
-70
lines changed

1 file changed

+85
-70
lines changed

packages/modal/src/utils/focusModalChildElement.spec.ts

Lines changed: 85 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ describe('focusModalChildElement', () => {
2222
beforeEach(() => {
2323
mockFocus = jest.fn();
2424
mockQuerySelector = jest.fn();
25+
mockQueryFirstFocusableElement.mockClear();
2526

2627
// Create a mock dialog element
2728
mockDialogElement = {
2829
querySelector: mockQuerySelector,
2930
} as unknown as HTMLDialogElement;
30-
31-
// Reset mocks
32-
jest.clearAllMocks();
3331
});
3432

3533
describe('when initialFocus is null', () => {
@@ -184,24 +182,27 @@ describe('focusModalChildElement', () => {
184182
<button id="secondary">Secondary</button>
185183
</dialog>
186184
`;
187-
document.body.appendChild(container);
188185

189-
const dialogElement = container.querySelector(
190-
'dialog',
191-
) as HTMLDialogElement;
192-
const secondaryButton = container.querySelector(
193-
'#secondary',
194-
) as HTMLButtonElement;
186+
try {
187+
document.body.appendChild(container);
195188

196-
const mockFocus = jest.fn();
197-
secondaryButton.focus = mockFocus;
189+
const dialogElement = container.querySelector(
190+
'dialog',
191+
) as HTMLDialogElement;
192+
const secondaryButton = container.querySelector(
193+
'#secondary',
194+
) as HTMLButtonElement;
198195

199-
const result = focusModalChildElement(dialogElement, '#secondary');
196+
const mockFocus = jest.fn();
197+
secondaryButton.focus = mockFocus;
200198

201-
expect(mockFocus).toHaveBeenCalled();
202-
expect(result).toBe(secondaryButton);
199+
const result = focusModalChildElement(dialogElement, '#secondary');
203200

204-
document.body.removeChild(container);
201+
expect(mockFocus).toHaveBeenCalled();
202+
expect(result).toBe(secondaryButton);
203+
} finally {
204+
document.body.removeChild(container);
205+
}
205206
});
206207

207208
test('focuses element by ref', () => {
@@ -212,28 +213,31 @@ describe('focusModalChildElement', () => {
212213
<button id="secondary">Secondary</button>
213214
</dialog>
214215
`;
215-
document.body.appendChild(container);
216216

217-
const dialogElement = container.querySelector(
218-
'dialog',
219-
) as HTMLDialogElement;
220-
const secondaryButton = container.querySelector(
221-
'#secondary',
222-
) as HTMLButtonElement;
217+
try {
218+
document.body.appendChild(container);
223219

224-
const mockFocus = jest.fn();
225-
secondaryButton.focus = mockFocus;
220+
const dialogElement = container.querySelector(
221+
'dialog',
222+
) as HTMLDialogElement;
223+
const secondaryButton = container.querySelector(
224+
'#secondary',
225+
) as HTMLButtonElement;
226226

227-
const buttonRef = {
228-
current: secondaryButton,
229-
} as React.RefObject<HTMLButtonElement>;
227+
const mockFocus = jest.fn();
228+
secondaryButton.focus = mockFocus;
230229

231-
const result = focusModalChildElement(dialogElement, buttonRef);
230+
const buttonRef = {
231+
current: secondaryButton,
232+
} as React.RefObject<HTMLButtonElement>;
232233

233-
expect(mockFocus).toHaveBeenCalled();
234-
expect(result).toBe(secondaryButton);
234+
const result = focusModalChildElement(dialogElement, buttonRef);
235235

236-
document.body.removeChild(container);
236+
expect(mockFocus).toHaveBeenCalled();
237+
expect(result).toBe(secondaryButton);
238+
} finally {
239+
document.body.removeChild(container);
240+
}
237241
});
238242

239243
test('identifies autoFocus element without calling focus()', () => {
@@ -244,22 +248,27 @@ describe('focusModalChildElement', () => {
244248
<button>Close</button>
245249
</dialog>
246250
`;
247-
document.body.appendChild(container);
248251

249-
const dialogElement = container.querySelector(
250-
'dialog',
251-
) as HTMLDialogElement;
252-
const inputElement = container.querySelector('input') as HTMLInputElement;
252+
try {
253+
document.body.appendChild(container);
253254

254-
const mockFocus = jest.fn();
255-
inputElement.focus = mockFocus;
255+
const dialogElement = container.querySelector(
256+
'dialog',
257+
) as HTMLDialogElement;
258+
const inputElement = container.querySelector(
259+
'input',
260+
) as HTMLInputElement;
256261

257-
const result = focusModalChildElement(dialogElement, 'auto');
262+
const mockFocus = jest.fn();
263+
inputElement.focus = mockFocus;
258264

259-
expect(mockFocus).not.toHaveBeenCalled(); // Browser handles autoFocus
260-
expect(result).toBe(inputElement);
265+
const result = focusModalChildElement(dialogElement, 'auto');
261266

262-
document.body.removeChild(container);
267+
expect(mockFocus).not.toHaveBeenCalled(); // Browser handles autoFocus
268+
expect(result).toBe(inputElement);
269+
} finally {
270+
document.body.removeChild(container);
271+
}
263272
});
264273

265274
test('focuses first focusable element when no autoFocus exists', () => {
@@ -270,26 +279,29 @@ describe('focusModalChildElement', () => {
270279
<input />
271280
</dialog>
272281
`;
273-
document.body.appendChild(container);
274282

275-
const dialogElement = container.querySelector(
276-
'dialog',
277-
) as HTMLDialogElement;
278-
const buttonElement = container.querySelector(
279-
'button',
280-
) as HTMLButtonElement;
283+
try {
284+
document.body.appendChild(container);
281285

282-
const mockFocus = jest.fn();
283-
buttonElement.focus = mockFocus;
286+
const dialogElement = container.querySelector(
287+
'dialog',
288+
) as HTMLDialogElement;
289+
const buttonElement = container.querySelector(
290+
'button',
291+
) as HTMLButtonElement;
284292

285-
mockQueryFirstFocusableElement.mockReturnValue(buttonElement);
293+
const mockFocus = jest.fn();
294+
buttonElement.focus = mockFocus;
286295

287-
const result = focusModalChildElement(dialogElement, 'auto');
296+
mockQueryFirstFocusableElement.mockReturnValue(buttonElement);
288297

289-
expect(mockFocus).toHaveBeenCalled();
290-
expect(result).toBe(buttonElement);
298+
const result = focusModalChildElement(dialogElement, 'auto');
291299

292-
document.body.removeChild(container);
300+
expect(mockFocus).toHaveBeenCalled();
301+
expect(result).toBe(buttonElement);
302+
} finally {
303+
document.body.removeChild(container);
304+
}
293305
});
294306

295307
test('does not focus when initialFocus is null', () => {
@@ -299,24 +311,27 @@ describe('focusModalChildElement', () => {
299311
<button>Submit</button>
300312
</dialog>
301313
`;
302-
document.body.appendChild(container);
303314

304-
const dialogElement = container.querySelector(
305-
'dialog',
306-
) as HTMLDialogElement;
307-
const buttonElement = container.querySelector(
308-
'button',
309-
) as HTMLButtonElement;
315+
try {
316+
document.body.appendChild(container);
310317

311-
const mockFocus = jest.fn();
312-
buttonElement.focus = mockFocus;
318+
const dialogElement = container.querySelector(
319+
'dialog',
320+
) as HTMLDialogElement;
321+
const buttonElement = container.querySelector(
322+
'button',
323+
) as HTMLButtonElement;
313324

314-
const result = focusModalChildElement(dialogElement, null);
325+
const mockFocus = jest.fn();
326+
buttonElement.focus = mockFocus;
315327

316-
expect(mockFocus).not.toHaveBeenCalled();
317-
expect(result).toBeNull();
328+
const result = focusModalChildElement(dialogElement, null);
318329

319-
document.body.removeChild(container);
330+
expect(mockFocus).not.toHaveBeenCalled();
331+
expect(result).toBeNull();
332+
} finally {
333+
document.body.removeChild(container);
334+
}
320335
});
321336
});
322337
});

0 commit comments

Comments
 (0)