0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-08-06 19:47:38 +00:00
Files
homebrewery/client/components/dropdown/dropdown.testUtils.js
T
Gazook89 fa07af2d82 Add tests for the Dropdown structure and behavior
Largely this is written by an LLM after I provided the targets, and I've looked through the tests themselves to confirm they are accurate.  There may be improvements in how to actually *set up* the tests, though, as I am less familiar with those.

This has two test suites:  the first for html structure, the second for behavior.  Each of them calls another script that provides helper functions for setting things up and taking them down.  This keeps the test suites themselves fairly lean.
2026-05-29 11:08:05 -05:00

50 lines
914 B
JavaScript

import globalJsdom from 'jsdom-global';
import { act } from 'react';
import { createRoot } from 'react-dom/client';
let cleanupJsdom;
const setupDropdownTestLifecycle = ()=>{
beforeAll(()=>{
cleanupJsdom = globalJsdom();
});
afterAll(()=>{
cleanupJsdom?.();
});
afterEach(()=>{
document.body.innerHTML = '';
jest.restoreAllMocks();
});
};
const renderDropdown = async (element)=>{
const host = document.createElement('div');
document.body.appendChild(host);
const root = createRoot(host);
await act(async ()=>{
root.render(element);
});
return {
host,
cleanup : async ()=>{
await act(async ()=>{
root.unmount();
});
host.remove();
}
};
};
const createOpenMenu = ()=>{
const menu = document.createElement('div');
menu.className = 'menu-list';
menu.hidePopover = jest.fn();
return menu;
};
export { setupDropdownTestLifecycle, renderDropdown, createOpenMenu };