react-component-snapshot 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-component-snapshot",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Transform a React component to match its snapshot in Jest",
5
5
  "keywords": [
6
6
  "react",
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "debug": "^4.4.0",
34
- "react-component-name": "^0.0.3"
34
+ "react-component-name": "^0.0.4"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@babel/core": "^7.26.10",
@@ -0,0 +1,12 @@
1
+ import type React from 'react'
2
+
3
+ type FiberNode = ReactComponentSnapshotTypes.FiberNode
4
+ type StateNodeType = Element | Text | React.Component<any, any>
5
+
6
+ export function getStateNodeFiber (stateNode: StateNodeType) : Record<PropertyKey, unknown> | null
7
+
8
+ export function getStateNodeProps (stateNode: StateNodeType): Record<PropertyKey, unknown> | null
9
+
10
+ export function getFiber (element: Element): FiberNode | null
11
+
12
+ export default function getSnapshot (fiberNode: FiberNode): Record<PropertyKey, unknown>
@@ -8,7 +8,8 @@ import {
8
8
  } from 'react-component-name'
9
9
 
10
10
  import {
11
- REACT_COMPONENT_SNAPSHOT
11
+ REACT_COMPONENT_FORWARD_REF_TYPE,
12
+ REACT_COMPONENT_SNAPSHOT_TYPE
12
13
  } from './symbols.mjs'
13
14
 
14
15
  /**
@@ -16,7 +17,7 @@ import {
16
17
  * @returns {v is object}
17
18
  */
18
19
  function isObject (v) {
19
- return (v || false) instanceof Object && !Array.isArray(v)
20
+ return typeof (v || false) === 'object' && !Array.isArray(v)
20
21
  }
21
22
 
22
23
  /**
@@ -144,8 +145,14 @@ function getProps (fiberNode) {
144
145
  Object.fromEntries(
145
146
  Object.entries(props)
146
147
  .filter(([key]) => key !== 'children')
147
- .filter(([key, value]) => {
148
- if (isObject(value)) return !('current' in value)
148
+ .filter(([key]) => key !== 'ref')
149
+ .filter(([key, value]) => { // key is not `ref` but value may be a `ref`
150
+ if (isObject(value)) {
151
+ return !(
152
+ value.$$typeof !== REACT_COMPONENT_FORWARD_REF_TYPE /* forward ref */ ||
153
+ 'current' in value /* ref shape */
154
+ )
155
+ }
149
156
  return true
150
157
  })
151
158
  )
@@ -233,7 +240,7 @@ export default function getSnapshot (fiberNode) {
233
240
  children
234
241
  },
235
242
  '$$typeof', {
236
- value: REACT_COMPONENT_SNAPSHOT
243
+ value: REACT_COMPONENT_SNAPSHOT_TYPE
237
244
  })
238
245
  )
239
246
  }
@@ -1 +1,3 @@
1
- export const REACT_COMPONENT_SNAPSHOT = Symbol.for('react.test.json')
1
+ export const REACT_COMPONENT_FORWARD_REF_TYPE = Symbol.for('react.forward_ref')
2
+
3
+ export const REACT_COMPONENT_SNAPSHOT_TYPE = Symbol.for('react.test.json')
@@ -0,0 +1,21 @@
1
+ import type {
2
+ RenderResult as RenderType
3
+ } from '@testing-library/react'
4
+
5
+ export function getComponentElement (render: RenderType): Element | null
6
+
7
+ export function getComponentElementFromRender (render: RenderType): Element | null
8
+
9
+ export function getContainerElementFromRender (render: RenderType): Element
10
+
11
+ export function toSnapshotFromRender (render: RenderType): Record<PropertyKey, unknown> | null
12
+
13
+ export function toSnapshotFromContainerElement (containerElement: Element): Record<PropertyKey, unknown> | null
14
+
15
+ export function toSnapshotFromComponentElement (componentElement?: Element | null) : Record<PropertyKey, unknown> | null
16
+
17
+ export function snapshotOf (element?: Element | null): Record<PropertyKey, unknown> | null
18
+
19
+ export {
20
+ toSnapshotFromRender as toSnapshot
21
+ }