| 1 | import { render } from "ink-testing-library"; |
| 2 | import React from "react"; |
| 3 | |
| 4 | import { ServiceContainerProvider } from "../services/ServiceContainerContext.js"; |
| 5 | |
| 6 | import { |
| 7 | createTestServiceContainer, |
| 8 | TestServiceContainer, |
| 9 | } from "./testServiceContainer.js"; |
| 10 | |
| 11 | export interface RenderWithServicesOptions { |
| 12 | services?: Record<string, any>; |
| 13 | container?: TestServiceContainer; |
| 14 | } |
| 15 | |
| 16 | export interface RenderWithServicesResult { |
| 17 | container: TestServiceContainer; |
| 18 | lastFrame: () => string | undefined; |
| 19 | stdin: { |
| 20 | write: (input: string) => void; |
| 21 | }; |
| 22 | frames: string[]; |
| 23 | unmount: () => void; |
| 24 | rerender: (element: React.ReactElement) => void; |
| 25 | } |
| 26 | |
| 27 | export function renderWithServices( |
| 28 | ui: React.ReactElement, |
| 29 | options?: RenderWithServicesOptions, |
| 30 | ): RenderWithServicesResult { |
| 31 | const container = options?.container || createTestServiceContainer(); |
| 32 | |
| 33 | // Register default services if provided |
| 34 | if (options?.services) { |
| 35 | Object.entries(options.services).forEach(([name, value]) => { |
| 36 | container.register(name, () => Promise.resolve(value)); |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | const rendered = render( |
| 41 | <ServiceContainerProvider container={container}> |
| 42 | {ui} |
| 43 | </ServiceContainerProvider>, |
| 44 | ); |
| 45 | |
| 46 | return { |
| 47 | ...rendered, |
| 48 | container, |
| 49 | }; |
| 50 | } |