@atomic-testing/react-19
Version:
Adapter for integrating [Atomic Testing](https://atomic-testing.dev) with [React 19](https://react.dev). It maps React components to the core scene part APIs.
1 lines • 3.83 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":["node: ReactNode","partDefinitions: T","option?: Readonly<Partial<IReactTestEngineOption>>","rootElement: HTMLElement","_option?: Readonly<Partial<IReactTestEngineOption>>"],"sources":["../src/createTestEngine.ts"],"sourcesContent":["import { ReactNode } from 'react';\nimport { createRoot } from 'react-dom/client';\n\nimport { byAttribute, ScenePart, TestEngine } from '@atomic-testing/core';\nimport { ReactInteractor } from '@atomic-testing/react-core';\nimport { act } from '@testing-library/react';\n\nimport { IReactTestEngineOption } from './types';\n\nlet _rootId = 0;\nfunction getNextRootElementId() {\n return `${_rootId++}`;\n}\n\nconst rootElementAttributeName = 'data-atomic-testing-react';\n\n/**\n * Create test engine for React 18 or later, for React 17 or before, use createLegacyTestEngine\n * This function takes a react node and render it into a container element. For rendered\n * components, use createRenderedTestEngine\n * @param node The React node to render\n * @param partDefinitions The scene part definitions\n * @param option\n * @returns The test engine\n */\nexport function createTestEngine<T extends ScenePart>(\n node: ReactNode,\n partDefinitions: T,\n option?: Readonly<Partial<IReactTestEngineOption>>\n): TestEngine<T> {\n const rootEl = option?.rootElement ?? document.body;\n const container = rootEl.appendChild(document.createElement('div'));\n\n const root = createRoot(container);\n const rootId = getNextRootElementId();\n container.setAttribute(rootElementAttributeName, rootId);\n act(() => root.render(node));\n\n const cleanup = () => {\n act(() => root.unmount());\n rootEl.removeChild(container);\n return Promise.resolve();\n };\n\n const engine = new TestEngine(\n byAttribute(rootElementAttributeName, rootId),\n new ReactInteractor(),\n {\n parts: partDefinitions,\n },\n cleanup\n );\n\n return engine;\n}\n\n/**\n * Create test engine for React 18 or later, for React 17 or before, use createRenderedLegacyTestEngine\n * This function takes an html element purportedly rendered by React and create a test engine for it, it\n * can be useful in environment such as Storybook where Storybook renders the component and the test\n * @param rootElement The React node to render\n * @param partDefinitions The scene part definitions\n * @param option\n * @returns The test engine\n */\nexport function createRenderedTestEngine<T extends ScenePart>(\n rootElement: HTMLElement,\n partDefinitions: T,\n _option?: Readonly<Partial<IReactTestEngineOption>>\n): TestEngine<T> {\n const rootId = getNextRootElementId();\n rootElement.setAttribute(rootElementAttributeName, rootId);\n\n const cleanup = () => {\n rootElement.removeAttribute(rootElementAttributeName);\n return Promise.resolve();\n };\n\n const engine = new TestEngine(\n byAttribute(rootElementAttributeName, rootId),\n new ReactInteractor(),\n {\n parts: partDefinitions,\n },\n cleanup\n );\n\n return engine;\n}\n"],"mappings":";;;;;;AASA,IAAI,UAAU;AACd,SAAS,uBAAuB;AAC9B,WAAU;AACX;AAED,MAAM,2BAA2B;;;;;;;;;;AAWjC,SAAgB,iBACdA,MACAC,iBACAC,QACe;CACf,MAAM,SAAS,QAAQ,eAAe,SAAS;CAC/C,MAAM,YAAY,OAAO,YAAY,SAAS,cAAc,MAAM,CAAC;CAEnE,MAAM,OAAO,WAAW,UAAU;CAClC,MAAM,SAAS,sBAAsB;AACrC,WAAU,aAAa,0BAA0B,OAAO;AACxD,KAAI,MAAM,KAAK,OAAO,KAAK,CAAC;CAE5B,MAAM,UAAU,MAAM;AACpB,MAAI,MAAM,KAAK,SAAS,CAAC;AACzB,SAAO,YAAY,UAAU;AAC7B,SAAO,QAAQ,SAAS;CACzB;CAED,MAAM,SAAS,IAAI,WACjB,YAAY,0BAA0B,OAAO,EAC7C,IAAI,mBACJ,EACE,OAAO,gBACR,GACD;AAGF,QAAO;AACR;;;;;;;;;;AAWD,SAAgB,yBACdC,aACAF,iBACAG,SACe;CACf,MAAM,SAAS,sBAAsB;AACrC,aAAY,aAAa,0BAA0B,OAAO;CAE1D,MAAM,UAAU,MAAM;AACpB,cAAY,gBAAgB,yBAAyB;AACrD,SAAO,QAAQ,SAAS;CACzB;CAED,MAAM,SAAS,IAAI,WACjB,YAAY,0BAA0B,OAAO,EAC7C,IAAI,mBACJ,EACE,OAAO,gBACR,GACD;AAGF,QAAO;AACR"}