github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/web/src/test-helpers.ts (about) 1 import { Component } from "react" 2 import { render } from "react-dom" 3 4 /** 5 * Generic test helper functions 6 */ 7 8 /** 9 * There are a couple places in our tests where we rely on asserting on 10 * and manipulating React component state directly. This isn't possible 11 * to do with React Testing Library. Instead, we use React's testing utils 12 * (with minor Typescript gymnastics) to get the component instance and 13 * access the DOM. 14 */ 15 export function renderTestComponent<T>(component: JSX.Element) { 16 const container = document.createElement("div") 17 const rootTree = render<T>(component, container) 18 19 if (isComponent<T>(rootTree)) { 20 return { container, rootTree } 21 } else { 22 throw new Error("React render did not return a component") 23 } 24 } 25 26 // A rudimentary helper function to type the output of `render()` properly 27 function isComponent<T>( 28 renderOutput: void | Element | Component<T> 29 ): renderOutput is Component<T> { 30 return !!renderOutput 31 }