react-component-snapshot 0.0.2 → 0.0.4
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/index.d.mts +14 -21
- package/package.json +5 -3
- package/src/index.mjs +28 -10
- package/src/symbols.mjs +1 -0
- package/src/react.mjs +0 -185
package/index.d.mts
CHANGED
|
@@ -12,61 +12,54 @@ declare global {
|
|
|
12
12
|
| FiberNodeForInstrinsicElement
|
|
13
13
|
| FiberNodeForTextNode
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
child: FiberNode
|
|
15
|
+
interface FiberType {
|
|
16
|
+
child: FiberNode | null
|
|
17
17
|
sibling: FiberNode | null
|
|
18
18
|
|
|
19
|
+
tag: number
|
|
20
|
+
return?: FiberRootNode | FiberNode
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface FiberRootNode extends FiberType {
|
|
24
|
+
child: FiberNode
|
|
25
|
+
|
|
19
26
|
elementType: React.FunctionComponent
|
|
20
27
|
type: React.FunctionComponent
|
|
21
28
|
|
|
22
29
|
stateNode: {
|
|
23
30
|
current: FiberRootNode
|
|
24
31
|
}
|
|
25
|
-
return?: FiberRootNode
|
|
32
|
+
return?: FiberRootNode
|
|
26
33
|
}
|
|
27
34
|
|
|
28
|
-
export interface FiberNodeForFunctionComponent {
|
|
29
|
-
child: FiberNode | null
|
|
30
|
-
sibling: FiberNode | null
|
|
31
|
-
|
|
35
|
+
export interface FiberNodeForFunctionComponent extends FiberType {
|
|
32
36
|
elementType: React.FunctionComponent
|
|
33
37
|
type: React.FunctionComponent
|
|
34
38
|
|
|
35
39
|
stateNode: null
|
|
36
|
-
return?: FiberNode // | null
|
|
37
40
|
}
|
|
38
41
|
|
|
39
|
-
export interface FiberNodeForClassComponent {
|
|
40
|
-
child: FiberNode | null
|
|
41
|
-
sibling: FiberNode | null
|
|
42
|
-
|
|
42
|
+
export interface FiberNodeForClassComponent extends FiberType {
|
|
43
43
|
elementType: React.ComponentClass
|
|
44
44
|
type: React.ComponentClass
|
|
45
45
|
|
|
46
46
|
stateNode: React.Component
|
|
47
|
-
return?: FiberNode // | null
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
export interface FiberNodeForInstrinsicElement {
|
|
51
|
-
child: FiberNode | null
|
|
52
|
-
sibling: FiberNode | null
|
|
53
|
-
|
|
49
|
+
export interface FiberNodeForInstrinsicElement extends FiberType {
|
|
54
50
|
elementType: keyof React.JSX.IntrinsicElements
|
|
55
51
|
type: keyof React.JSX.IntrinsicElements
|
|
56
52
|
|
|
57
53
|
stateNode: HTMLElement
|
|
58
|
-
return?: FiberNode
|
|
59
54
|
}
|
|
60
55
|
|
|
61
|
-
export interface FiberNodeForTextNode {
|
|
56
|
+
export interface FiberNodeForTextNode extends FiberType {
|
|
62
57
|
child: null
|
|
63
|
-
sibling: FiberNode | null
|
|
64
58
|
|
|
65
59
|
elementType: null
|
|
66
60
|
type: null
|
|
67
61
|
|
|
68
62
|
stateNode: Text
|
|
69
|
-
return?: FiberNode
|
|
70
63
|
}
|
|
71
64
|
}
|
|
72
65
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-component-snapshot",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Transform a React component to match its snapshot in Jest",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -26,10 +26,12 @@
|
|
|
26
26
|
"scripts": {
|
|
27
27
|
"lint": "eslint",
|
|
28
28
|
"lint:fix": "npm run lint -- --fix",
|
|
29
|
+
"prepare": "husky",
|
|
29
30
|
"test": "NODE_ENV=test jest"
|
|
30
31
|
},
|
|
31
32
|
"dependencies": {
|
|
32
|
-
"debug": "^4.4.0"
|
|
33
|
+
"debug": "^4.4.0",
|
|
34
|
+
"react-component-name": "^0.0.0"
|
|
33
35
|
},
|
|
34
36
|
"devDependencies": {
|
|
35
37
|
"@babel/core": "^7.26.10",
|
|
@@ -42,13 +44,13 @@
|
|
|
42
44
|
"@testing-library/react": "^16.2.0",
|
|
43
45
|
"@types/debug": "^4.1.12",
|
|
44
46
|
"@types/jest": "^29.5.14",
|
|
45
|
-
"@types/prop-types": "^15.7.14",
|
|
46
47
|
"@types/react": "^19.0.11",
|
|
47
48
|
"@typescript-eslint/parser": "^8.26.1",
|
|
48
49
|
"core-js": "^3.41.0",
|
|
49
50
|
"eslint": "^9.22.0",
|
|
50
51
|
"eslint-plugin-react": "^7.37.4",
|
|
51
52
|
"globals": "^16.0.0",
|
|
53
|
+
"husky": "^9.1.7",
|
|
52
54
|
"jest": "^29.7.0",
|
|
53
55
|
"jest-environment-jsdom": "^29.7.0",
|
|
54
56
|
"react": "19.0.0"
|
package/src/index.mjs
CHANGED
|
@@ -3,10 +3,20 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
|
-
getType
|
|
7
|
-
} from '
|
|
6
|
+
getComponentNameFromFiber as getType
|
|
7
|
+
} from 'react-component-name'
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
import {
|
|
10
|
+
REACT_COMPONENT_SNAPSHOT
|
|
11
|
+
} from './symbols.mjs'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {unknown} v
|
|
15
|
+
* @returns {v is object}
|
|
16
|
+
*/
|
|
17
|
+
function isObject (v) {
|
|
18
|
+
return (v || false) instanceof Object && !Array.isArray(v)
|
|
19
|
+
}
|
|
10
20
|
|
|
11
21
|
/**
|
|
12
22
|
* @param {PropertyKey} key
|
|
@@ -145,8 +155,8 @@ function getProps (fiberNode) {
|
|
|
145
155
|
Object.fromEntries(
|
|
146
156
|
Object.entries(props)
|
|
147
157
|
.filter(([key]) => key !== 'children')
|
|
148
|
-
.filter(([key, value]) => {
|
|
149
|
-
if ((value
|
|
158
|
+
.filter(([key, value]) => {
|
|
159
|
+
if (isObject(value)) return !('current' in value)
|
|
150
160
|
return true
|
|
151
161
|
})
|
|
152
162
|
)
|
|
@@ -160,7 +170,7 @@ function getProps (fiberNode) {
|
|
|
160
170
|
* @param {FiberNode} fiberNode
|
|
161
171
|
* @returns {Array<string | FiberNode>}
|
|
162
172
|
*/
|
|
163
|
-
function
|
|
173
|
+
function getChildrenOf (fiberNode) {
|
|
164
174
|
const child = fiberNode.child
|
|
165
175
|
|
|
166
176
|
if (child) {
|
|
@@ -207,6 +217,17 @@ function toChildren (child) {
|
|
|
207
217
|
)
|
|
208
218
|
}
|
|
209
219
|
|
|
220
|
+
/**
|
|
221
|
+
* @param {FiberNode} fiberNode
|
|
222
|
+
* @returns {Array<string | Record<PropertyKey, unknown>>}
|
|
223
|
+
*/
|
|
224
|
+
function getChildren (fiberNode) {
|
|
225
|
+
return (
|
|
226
|
+
getChildrenOf(fiberNode)
|
|
227
|
+
.map(toChildren)
|
|
228
|
+
)
|
|
229
|
+
}
|
|
230
|
+
|
|
210
231
|
/**
|
|
211
232
|
* @param {FiberNode} fiberNode
|
|
212
233
|
* @returns {Record<PropertyKey, unknown>}
|
|
@@ -214,10 +235,7 @@ function toChildren (child) {
|
|
|
214
235
|
function transform (fiberNode) {
|
|
215
236
|
const type = getType(fiberNode)
|
|
216
237
|
const props = getProps(fiberNode)
|
|
217
|
-
const children = (
|
|
218
|
-
getChildren(fiberNode)
|
|
219
|
-
.map(toChildren)
|
|
220
|
-
)
|
|
238
|
+
const children = getChildren(fiberNode)
|
|
221
239
|
|
|
222
240
|
return (
|
|
223
241
|
Object.defineProperty({
|
package/src/symbols.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const REACT_COMPONENT_SNAPSHOT = Symbol.for('react.test.json')
|
package/src/react.mjs
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
|
|
3
|
-
import debug from 'debug'
|
|
4
|
-
|
|
5
|
-
const log = debug('react-component-snapshot')
|
|
6
|
-
|
|
7
|
-
// export const REACT_LEGACY_ELEMENT_TYPE = Symbol.for('react.element')
|
|
8
|
-
// export const REACT_ELEMENT_TYPE = Symbol.for('react.transitional.element')
|
|
9
|
-
export const REACT_PORTAL_TYPE = Symbol.for('react.portal')
|
|
10
|
-
export const REACT_FRAGMENT_TYPE = Symbol.for('react.fragment')
|
|
11
|
-
export const REACT_STRICT_MODE_TYPE = Symbol.for('react.strict_mode')
|
|
12
|
-
export const REACT_PROFILER_TYPE = Symbol.for('react.profiler')
|
|
13
|
-
// export const REACT_PROVIDER_TYPE = Symbol.for('react.provider')
|
|
14
|
-
export const REACT_CONSUMER_TYPE = Symbol.for('react.consumer')
|
|
15
|
-
export const REACT_CONTEXT_TYPE = Symbol.for('react.context')
|
|
16
|
-
export const REACT_FORWARD_REF_TYPE = Symbol.for('react.forward_ref')
|
|
17
|
-
export const REACT_SUSPENSE_TYPE = Symbol.for('react.suspense')
|
|
18
|
-
export const REACT_SUSPENSE_LIST_TYPE = Symbol.for('react.suspense_list')
|
|
19
|
-
export const REACT_MEMO_TYPE = Symbol.for('react.memo')
|
|
20
|
-
export const REACT_LAZY_TYPE = Symbol.for('react.lazy')
|
|
21
|
-
// Symbol.for('react.scope')
|
|
22
|
-
// Symbol.for('react.debug_trace_mode')
|
|
23
|
-
export const REACT_OFFSCREEN_TYPE = Symbol.for('react.offscreen')
|
|
24
|
-
// Symbol.for('react.legacy_hidden')
|
|
25
|
-
// Symbol.for('react.tracing_marker')
|
|
26
|
-
// export const REACT_MEMO_CACHE_SENTINEL = Symbol.for('react.memo_cache_sentinel')
|
|
27
|
-
// export const MAYBE_ITERATOR_SYMBOL = Symbol.iterator
|
|
28
|
-
export const REACT_CLIENT_REFERENCE = Symbol.for('react.client.reference')
|
|
29
|
-
|
|
30
|
-
export function getNameFromType (type) {
|
|
31
|
-
if (type == null) return null
|
|
32
|
-
|
|
33
|
-
if (typeof type === 'function') {
|
|
34
|
-
return (
|
|
35
|
-
type.$$typeof === REACT_CLIENT_REFERENCE
|
|
36
|
-
? null
|
|
37
|
-
: type.displayName || type.name || null
|
|
38
|
-
)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (typeof type === 'string') return type
|
|
42
|
-
|
|
43
|
-
switch (type) {
|
|
44
|
-
case REACT_FRAGMENT_TYPE:
|
|
45
|
-
return 'Fragment'
|
|
46
|
-
case REACT_PORTAL_TYPE:
|
|
47
|
-
return 'Portal'
|
|
48
|
-
case REACT_PROFILER_TYPE:
|
|
49
|
-
return 'Profiler'
|
|
50
|
-
case REACT_STRICT_MODE_TYPE:
|
|
51
|
-
return 'StrictMode'
|
|
52
|
-
case REACT_SUSPENSE_TYPE:
|
|
53
|
-
return 'Suspense'
|
|
54
|
-
case REACT_SUSPENSE_LIST_TYPE:
|
|
55
|
-
return 'SuspenseList'
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (typeof type === 'object') {
|
|
59
|
-
switch (type.$$typeof) {
|
|
60
|
-
case REACT_CONTEXT_TYPE:
|
|
61
|
-
return (type.displayName || 'Context') + '.Provider'
|
|
62
|
-
case REACT_CONSUMER_TYPE:
|
|
63
|
-
return (type._context.displayName || 'Context') + '.Consumer'
|
|
64
|
-
case REACT_FORWARD_REF_TYPE:
|
|
65
|
-
{
|
|
66
|
-
const innerType = type.render
|
|
67
|
-
type = type.displayName
|
|
68
|
-
|
|
69
|
-
if (type) return type
|
|
70
|
-
|
|
71
|
-
type = innerType.displayName || innerType.name || ''
|
|
72
|
-
|
|
73
|
-
return (
|
|
74
|
-
type !== ''
|
|
75
|
-
? `ForwardRef(${type})`
|
|
76
|
-
: 'ForwardRef'
|
|
77
|
-
)
|
|
78
|
-
}
|
|
79
|
-
case REACT_MEMO_TYPE:
|
|
80
|
-
{
|
|
81
|
-
const innerType = type.displayName || null
|
|
82
|
-
|
|
83
|
-
return (
|
|
84
|
-
innerType !== null
|
|
85
|
-
? innerType
|
|
86
|
-
: getNameFromType(type.type) || 'Memo'
|
|
87
|
-
)
|
|
88
|
-
}
|
|
89
|
-
case REACT_LAZY_TYPE:
|
|
90
|
-
{
|
|
91
|
-
const innerType = type._payload
|
|
92
|
-
type = type._init
|
|
93
|
-
|
|
94
|
-
try {
|
|
95
|
-
return getNameFromType(type(innerType))
|
|
96
|
-
} catch (message) {
|
|
97
|
-
log(`Lazy error. Message was "${message}"`)
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return null
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function getType (fiberNode) {
|
|
107
|
-
const type = fiberNode.type
|
|
108
|
-
|
|
109
|
-
switch (fiberNode.tag) {
|
|
110
|
-
case 24:
|
|
111
|
-
return 'Cache'
|
|
112
|
-
case 9:
|
|
113
|
-
return (type._context.displayName || 'Context') + '.Consumer'
|
|
114
|
-
case 10:
|
|
115
|
-
return (type.displayName || 'Context') + '.Provider'
|
|
116
|
-
case 18:
|
|
117
|
-
return 'DehydratedFragment'
|
|
118
|
-
case 11:
|
|
119
|
-
{
|
|
120
|
-
return (
|
|
121
|
-
(fiberNode = type.render),
|
|
122
|
-
(fiberNode = fiberNode.displayName || fiberNode.name || ''),
|
|
123
|
-
type.displayName ||
|
|
124
|
-
(fiberNode !== '' ? `ForwardRef(${fiberNode})` : 'ForwardRef')
|
|
125
|
-
)
|
|
126
|
-
}
|
|
127
|
-
case 7:
|
|
128
|
-
return 'Fragment'
|
|
129
|
-
case 26:
|
|
130
|
-
case 27:
|
|
131
|
-
case 5:
|
|
132
|
-
return type
|
|
133
|
-
case 4:
|
|
134
|
-
return 'Portal'
|
|
135
|
-
case 3:
|
|
136
|
-
return 'Root'
|
|
137
|
-
case 6:
|
|
138
|
-
return 'Text'
|
|
139
|
-
case 16:
|
|
140
|
-
return getNameFromType(type)
|
|
141
|
-
case 8:
|
|
142
|
-
return (
|
|
143
|
-
type === REACT_STRICT_MODE_TYPE
|
|
144
|
-
? 'StrictMode'
|
|
145
|
-
: 'Mode'
|
|
146
|
-
)
|
|
147
|
-
case 22:
|
|
148
|
-
return 'Offscreen'
|
|
149
|
-
case 12:
|
|
150
|
-
return 'Profiler'
|
|
151
|
-
case 21:
|
|
152
|
-
return 'Scope'
|
|
153
|
-
case 13:
|
|
154
|
-
return 'Suspense'
|
|
155
|
-
case 19:
|
|
156
|
-
return 'SuspenseList'
|
|
157
|
-
case 25:
|
|
158
|
-
return 'TracingMarker'
|
|
159
|
-
case 1:
|
|
160
|
-
case 0:
|
|
161
|
-
case 14:
|
|
162
|
-
case 15:
|
|
163
|
-
if (typeof type === 'function') return type.displayName || type.name || null
|
|
164
|
-
if (typeof type === 'string') return type
|
|
165
|
-
break
|
|
166
|
-
case 29:
|
|
167
|
-
{
|
|
168
|
-
const type = fiberNode._debugInfo || null
|
|
169
|
-
|
|
170
|
-
if (type != null) {
|
|
171
|
-
let i = type.length - 1
|
|
172
|
-
|
|
173
|
-
for (i; i >= 0; i--) {
|
|
174
|
-
const t = type[i]
|
|
175
|
-
const n = t.name
|
|
176
|
-
|
|
177
|
-
if (typeof n === 'string') return n
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (fiberNode.return !== null) return getType(fiberNode.return)
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
return null
|
|
185
|
-
}
|