{"version":3,"file":"index.cjs","names":["TestEngine","ReactInteractor"],"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 19, 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 19, 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 root HTML element rendered by React\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,QAAO,GAAG;;AAGZ,MAAM,2BAA2B;;;;;;;;;;AAWjC,SAAgB,iBACd,MACA,iBACA,QACe;CACf,MAAM,SAAS,QAAQ,eAAe,SAAS;CAC/C,MAAM,YAAY,OAAO,YAAY,SAAS,cAAc,MAAM,CAAC;CAEnE,MAAM,wCAAkB,UAAU;CAClC,MAAM,SAAS,sBAAsB;AACrC,WAAU,aAAa,0BAA0B,OAAO;AACxD,uCAAU,KAAK,OAAO,KAAK,CAAC;CAE5B,MAAM,gBAAgB;AACpB,wCAAU,KAAK,SAAS,CAAC;AACzB,SAAO,YAAY,UAAU;AAC7B,SAAO,QAAQ,SAAS;;AAY1B,QATe,IAAIA,sEACL,0BAA0B,OAAO,EAC7C,IAAIC,4CAAiB,EACrB,EACE,OAAO,iBACR,EACD,QACD;;;;;;;;;;;AAcH,SAAgB,yBACd,aACA,iBACA,SACe;CACf,MAAM,SAAS,sBAAsB;AACrC,aAAY,aAAa,0BAA0B,OAAO;CAE1D,MAAM,gBAAgB;AACpB,cAAY,gBAAgB,yBAAyB;AACrD,SAAO,QAAQ,SAAS;;AAY1B,QATe,IAAID,sEACL,0BAA0B,OAAO,EAC7C,IAAIC,4CAAiB,EACrB,EACE,OAAO,iBACR,EACD,QACD"}