{"version":3,"file":"index.modern.mjs","sources":["../src/types.ts","../src/getDisplayName.ts","../src/mapProps.ts","../src/mapChild.ts","../src/mapChildren.ts","../src/mapTree.ts","../src/withReactTestSymbol.ts","../src/dive.ts","../src/mapTrees.ts","../src/shallowJSON.ts"],"sourcesContent":["import type {\n  Context,\n  ElementType,\n  ForwardRefExoticComponent,\n  ForwardRefRenderFunction,\n  NamedExoticComponent,\n  Key,\n  LegacyRef,\n} from 'react';\nimport type {\n  ReactTestRendererJSON,\n  ReactTestRendererTree,\n} from 'react-test-renderer';\n\nexport function isReactTestRendererJSON(\n  value: unknown\n): value is ReactTestRendererJSON {\n  return !!value && typeof value === 'object';\n}\n\ntype NamedExoticComponentFixed = Pick<\n  NamedExoticComponent,\n  '$$typeof' | 'displayName'\n> & {\n  type?: ReactTestRendererTreeFixed['type'];\n};\n\ntype ContextComponent = Pick<NamedExoticComponent, '$$typeof'> & {\n  type?: ReactTestRendererTreeFixed['type'];\n  // eslint-disable-next-line @typescript-eslint/naming-convention\n  _context: Pick<Context<unknown>, 'displayName'>;\n};\n\ntype ForwardRefComponent = Pick<\n  ForwardRefExoticComponent<unknown>,\n  '$$typeof' | 'displayName'\n> & {\n  render: ForwardRefRenderFunction<unknown>;\n};\n\nexport type ReactTestRendererTreeFixed = Omit<\n  ReactTestRendererTree,\n  'type' | 'props' | 'children' | 'rendered' | 'instance'\n> & {\n  type:\n    | string\n    | symbol\n    | ElementType\n    | NamedExoticComponentFixed\n    | ContextComponent\n    | ForwardRefComponent;\n  props: ReactTestRendererTreeProps;\n  rendered:\n    | null\n    | ReactTestRendererTreeNodeFixed\n    | (null | ReactTestRendererTreeNodeFixed)[];\n};\n\nexport type ReactTestRendererTreeNodeFixed =\n  | string\n  | ReactTestRendererTreeFixed;\n\ntype ReactTestRendererTreeProps = {\n  children?: ReactTestRendererTreeChildren;\n  [propName: string]: unknown;\n};\n\ntype ReactTestRendererTreeChildren =\n  | ReactTestRendererTreeChild\n  | ReactTestRendererTreeChild[];\n\nexport type ReactTestRendererTreeChild =\n  | string\n  | number\n  | boolean\n  | undefined\n  | null\n  | {\n      props: ReactTestRendererTreeProps;\n      type: string | ElementType;\n      key: null | Key;\n      ref?: LegacyRef<unknown>;\n    };\n\nexport function isNamedExoticComponentType(\n  type: ReactTestRendererTreeFixed['type']\n): type is NamedExoticComponentFixed {\n  return !!type && typeof type === 'object' && '$$typeof' in type;\n}\n\nexport function isContext(\n  type: ReactTestRendererTreeFixed['type']\n): type is ContextComponent {\n  return (\n    isNamedExoticComponentType(type) &&\n    (type.$$typeof === Symbol.for('react.context') ||\n      type.$$typeof === Symbol.for('react.provider'))\n  );\n}\n\nexport function isForwardRef(\n  type: ReactTestRendererTreeFixed['type']\n): type is ForwardRefComponent {\n  return (\n    isNamedExoticComponentType(type) &&\n    type.$$typeof === Symbol.for('react.forward_ref')\n  );\n}\n","import {\n  ReactTestRendererTreeFixed,\n  isContext,\n  isForwardRef,\n  isNamedExoticComponentType,\n} from './types';\n\nconst elementSymbolMap: Readonly<Record<symbol, string>> = {\n  [Symbol.for('react.fragment')]: 'React.Fragment',\n  [Symbol.for('react.memo')]: 'Memo',\n  [Symbol.for('react.context')]: 'Consumer',\n  [Symbol.for('react.provider')]: 'Provider',\n  [Symbol.for('react.forward_ref')]: 'ForwardRef',\n};\n\nfunction getSymbol($$typeof?: symbol) {\n  if (!$$typeof || !($$typeof in elementSymbolMap)) {\n    return '';\n  }\n\n  return elementSymbolMap[$$typeof];\n}\n\nfunction prependSymbol(displayName: string, $$typeof?: symbol): string {\n  if (!$$typeof || !($$typeof in elementSymbolMap)) {\n    return displayName;\n  }\n\n  return `${getSymbol($$typeof)}${displayName}`;\n}\n\nfunction appendSymbol(displayName: string, $$typeof?: symbol): string {\n  if (!$$typeof || !($$typeof in elementSymbolMap)) {\n    return displayName;\n  }\n\n  return `${displayName}.${getSymbol($$typeof)}`;\n}\n\nexport function getDisplayName(\n  type: ReactTestRendererTreeFixed['type'],\n  fallback = 'UNDEFINED'\n): string {\n  if (typeof type === 'string') {\n    return type;\n  }\n\n  if (typeof type === 'symbol') {\n    return type in elementSymbolMap ? elementSymbolMap[type] : fallback;\n  }\n\n  if (isContext(type)) {\n    return (\n      appendSymbol(type._context.displayName || 'Context', type.$$typeof) ||\n      fallback\n    );\n  }\n\n  if (isForwardRef(type)) {\n    return (\n      type.displayName ||\n      prependSymbol(\n        type.render ? getDisplayName(type.render, '') : '',\n        type.$$typeof\n      ) ||\n      fallback\n    );\n  }\n\n  if (isNamedExoticComponentType(type)) {\n    return (\n      type.displayName ||\n      prependSymbol(\n        type.type ? getDisplayName(type.type) : '',\n        type.$$typeof\n      ) ||\n      fallback\n    );\n  }\n\n  return type.displayName || type.name || fallback;\n}\n","import type { ReactTestRendererTreeFixed } from './types';\n\nexport function mapProps(props: ReactTestRendererTreeFixed['props']) {\n  const { children, ...propsWithoutChildren } = props;\n\n  return propsWithoutChildren;\n}\n","import type { ReactTestRendererNode } from 'react-test-renderer';\nimport type { ReactTestRendererTreeChild } from './types';\nimport { getDisplayName } from './getDisplayName';\nimport { mapProps } from './mapProps';\nimport { mapChildren } from './mapChildren';\n\nexport function mapChild(\n  child: ReactTestRendererTreeChild\n): ReactTestRendererNode {\n  if (typeof child !== 'object' || child == null) {\n    return child as unknown as ReactTestRendererNode;\n  }\n\n  const { key, ref } = child;\n\n  const json = {\n    type: getDisplayName(child.type),\n    props: mapProps({\n      ...child.props,\n      ...(key && { key }),\n      ...(ref && { ref }),\n    }),\n    children: mapChildren(child.props),\n  };\n\n  Object.defineProperty(json, '$$typeof', {\n    value: Symbol.for('react.test.json'),\n  });\n\n  return json;\n}\n","import type { ReactTestRendererJSON } from 'react-test-renderer';\nimport type { ReactTestRendererTreeFixed } from './types';\nimport { mapChild } from './mapChild';\n\nexport function mapChildren(\n  props: ReactTestRendererTreeFixed['props']\n): ReactTestRendererJSON['children'] {\n  const { children } = props;\n\n  if (children == null) {\n    return children as any;\n  }\n\n  if (!Array.isArray(children)) {\n    return [mapChild(children)];\n  }\n\n  return children.flat().map(mapChild);\n}\n","import type { ReactTestRendererNode } from 'react-test-renderer';\nimport type { ReactTestRendererTreeNodeFixed } from './types';\nimport { getDisplayName } from './getDisplayName';\nimport { mapProps } from './mapProps';\nimport { mapChildren } from './mapChildren';\nimport { mapTrees } from './mapTrees';\nimport { withReactTestSymbol } from './withReactTestSymbol';\n\nexport function mapTree(\n  tree: ReactTestRendererTreeNodeFixed,\n  depth = 1\n): ReactTestRendererNode {\n  if (typeof tree === 'string') {\n    return tree;\n  }\n\n  const json = {\n    type: getDisplayName(tree.type),\n    props: mapProps(tree.props),\n    children:\n      depth > 0 ? mapTrees(tree.rendered, depth) : mapChildren(tree.props),\n  };\n\n  return withReactTestSymbol(json) as ReactTestRendererNode;\n}\n","/**\n * `react-test-renderer`'s method `.toJSON()` applies a test symbol `$$typeof` to run Jest's `petty-format` `ReactTestComponent` plugin for snapshot formatting.\n *\n * @see: https://www.npmjs.com/package/pretty-format#user-content-test\n * @see: https://github.com/facebook/jest/blob/main/packages/pretty-format/src/plugins/ReactTestComponent.ts#L26-L29\n * @see: https://github.com/facebook/react/blob/12adaffef7105e2714f82651ea51936c563fe15c/packages/react-test-renderer/src/ReactTestRenderer.js#L120-L122\n */\nexport function withReactTestSymbol<T extends object>(json: T): T {\n  Object.defineProperty(json, '$$typeof', {\n    value: Symbol.for('react.test.json'),\n  });\n\n  return json;\n}\n","import type { ReactTestRendererTreeNodeFixed } from './types';\n\nexport function dive(\n  tree: ReactTestRendererTreeNodeFixed,\n  depth: number\n): number {\n  return typeof tree === 'string' || tree.nodeType === 'host'\n    ? depth\n    : depth - 1;\n}\n","import type { ReactTestRendererJSON } from 'react-test-renderer';\nimport type {\n  ReactTestRendererTreeFixed,\n  ReactTestRendererTreeNodeFixed,\n} from './types';\nimport { mapTree } from './mapTree';\nimport { dive } from './dive';\n\nexport function mapTrees(\n  trees: ReactTestRendererTreeFixed['rendered'],\n  depth = 1\n): ReactTestRendererJSON['children'] {\n  if (!trees) {\n    return null;\n  }\n\n  if (Array.isArray(trees)) {\n    return trees\n      .filter((tree): tree is ReactTestRendererTreeNodeFixed => tree !== null)\n      .map((tree) => mapTree(tree, dive(tree, depth)));\n  }\n\n  return [mapTree(trees, dive(trees, depth))];\n}\n","import type {\n  ReactTestRendererJSON,\n  ReactTestRendererNode,\n} from 'react-test-renderer';\nimport { ReactTestRendererTreeFixed, isReactTestRendererJSON } from './types';\nimport { mapTrees } from './mapTrees';\n\nexport type ShalllowJSONOptions = {\n  /**\n   * The depth level specifying how deep a nested tree structure should be rendered. Defaults to 1.\n   */\n  depth?: number;\n  /**\n   * Skip the component under test from snapshot output. Defaults to true.\n   */\n  skipRoot?: boolean;\n};\n\n/**\n * Return an object representing the rendered tree.\n * This tree contains the platform-specific nodes like `<div>` or `<View>` and includes the user-written components as well.\n * User-written components are rendered recursively up to the specified `depth`.\n * This is handy for **shallow** [snapshot testing](https://facebook.github.io/jest/docs/en/snapshot-testing.html#snapshot-testing-with-jest).\n */\nexport function shallowJSON(\n  trees: null | ReactTestRendererTreeFixed | ReactTestRendererTreeFixed[],\n  options: ShalllowJSONOptions = {}\n): null | ReactTestRendererNode | ReactTestRendererNode[] {\n  if (!trees) {\n    return null;\n  }\n\n  const { depth = 1, skipRoot = true } = options;\n\n  let nodes = mapTrees(trees, depth);\n\n  if (skipRoot) {\n    if (Array.isArray(nodes)) {\n      nodes = nodes\n        .filter<ReactTestRendererJSON>(isReactTestRendererJSON)\n        .flatMap((node) => (node.children ? node.children : []));\n    } else {\n      nodes = isReactTestRendererJSON(nodes)\n        ? (nodes as ReactTestRendererJSON).children\n        : null;\n    }\n  }\n\n  if (Array.isArray(nodes) && nodes.length === 1) {\n    return nodes[0];\n  }\n\n  return nodes;\n}\n"],"names":["isReactTestRendererJSON","value","isNamedExoticComponentType","type","elementSymbolMap","Symbol","for","getSymbol","$$typeof","prependSymbol","displayName","getDisplayName","fallback","isContext","_context","isForwardRef","render","name","_excluded","mapProps","props","_objectWithoutPropertiesLoose","mapChild","child","key","ref","json","_extends","children","mapChildren","Object","defineProperty","Array","isArray","flat","map","mapTree","tree","depth","mapTrees","rendered","dive","nodeType","trees","filter","shallowJSON","options","skipRoot","nodes","flatMap","node","length"],"mappings":"AAcM,SAAUA,EACdC,GAEA,QAASA,GAA0B,iBAAVA,CAC3B,CAkEgB,SAAAC,EACdC,GAEA,QAASA,GAAwB,iBAATA,GAAqB,aAAcA,CAC7D,CCjFA,MAAMC,EAAqD,CACzD,CAACC,OAAOC,IAAI,mBAAoB,iBAChC,CAACD,OAAOC,IAAI,eAAgB,OAC5B,CAACD,OAAOC,IAAI,kBAAmB,WAC/B,CAACD,OAAOC,IAAI,mBAAoB,WAChC,CAACD,OAAOC,IAAI,sBAAuB,cAGrC,SAASC,EAAUC,GACjB,OAAKA,GAAcA,KAAYJ,EAIxBA,EAAiBI,GAHf,EAIX,CAEA,SAASC,EAAcC,EAAqBF,GAC1C,OAAKA,GAAcA,KAAYJ,EAIxB,GAAGG,EAAUC,KAAYE,IAHvBA,CAIX,UAUgBC,EACdR,EACAS,EAAW,aAEX,MAAoB,iBAATT,EACFA,EAGW,iBAATA,EACFA,KAAQC,EAAmBA,EAAiBD,GAAQS,ED0C/C,SACdT,GAEA,OACED,EAA2BC,KAC1BA,EAAKK,WAAaH,OAAOC,IAAI,kBAC5BH,EAAKK,WAAaH,OAAOC,IAAI,kBAEnC,CC/CMO,CAAUV,IApBMO,EAsBHP,EAAKW,SAASJ,aAAe,YAtBLF,EAsBgBL,EAAKK,WArB3CA,KAAYJ,EAIxB,GAAGM,KAAeH,EAAUC,KAH1BE,IAqBLE,GD8CA,SACJT,GAEA,OACED,EAA2BC,IAC3BA,EAAKK,WAAaH,OAAOC,IAAI,oBAEjC,CCjDMS,CAAaZ,GAEbA,EAAKO,aACLD,EACEN,EAAKa,OAASL,EAAeR,EAAKa,OAAQ,IAAM,GAChDb,EAAKK,WAEPI,EAIAV,EAA2BC,GAE3BA,EAAKO,aACLD,EACE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