react-component-snapshot 0.0.16 → 0.0.18
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 +5 -3
- package/src/fiber/index.mjs +70 -20
- package/src/index.d.mts +2 -0
- package/.publish/README.md +0 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-component-snapshot",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
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.
|
|
34
|
+
"react-component-name": "^0.0.8"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@babel/core": "^7.26.10",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
"@types/debug": "^4.1.12",
|
|
46
46
|
"@types/jest": "^29.5.14",
|
|
47
47
|
"@types/react": "^19.0.12",
|
|
48
|
+
"@types/react-dom": "^19.0.4",
|
|
48
49
|
"@typescript-eslint/parser": "^8.28.0",
|
|
49
50
|
"core-js": "^3.41.0",
|
|
50
51
|
"eslint": "^9.23.0",
|
|
@@ -53,7 +54,8 @@
|
|
|
53
54
|
"husky": "^9.1.7",
|
|
54
55
|
"jest": "^29.7.0",
|
|
55
56
|
"jest-environment-jsdom": "^29.7.0",
|
|
56
|
-
"react": "19.
|
|
57
|
+
"react": "^19.1.0",
|
|
58
|
+
"react-dom": "^19.1.0"
|
|
57
59
|
},
|
|
58
60
|
"imports": {
|
|
59
61
|
"#react-component-snapshot": "./src/index.mjs"
|
package/src/fiber/index.mjs
CHANGED
|
@@ -128,6 +128,55 @@ function getSiblings (fiberNode) {
|
|
|
128
128
|
return siblings
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
/**
|
|
132
|
+
* @param {[key: PropertyKey, value: unknown]} entry
|
|
133
|
+
* @returns {PropertyKey}
|
|
134
|
+
*/
|
|
135
|
+
function getEntryKey ([key]) {
|
|
136
|
+
return key
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @param {[key: PropertyKey, value: unknown]} entry
|
|
141
|
+
* @returns {unknown}
|
|
142
|
+
*/
|
|
143
|
+
function getEntryValue ([key, value]) {
|
|
144
|
+
return value
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {[key: PropertyKey, value: unknown]} entry
|
|
149
|
+
* @returns {boolean}
|
|
150
|
+
*/
|
|
151
|
+
function entryKeyIsNotChildren (entry) {
|
|
152
|
+
return getEntryKey(entry) !== 'children'
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @param {[key: PropertyKey, value: unknown]} entry
|
|
157
|
+
* @returns {boolean}
|
|
158
|
+
*/
|
|
159
|
+
function entryKeyIsNotRef (entry) {
|
|
160
|
+
return getEntryKey(entry) !== 'ref'
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @param {[key: PropertyKey, value: unknown]} entry
|
|
165
|
+
* @returns {boolean}
|
|
166
|
+
*/
|
|
167
|
+
function entryValueIsNotRef (entry) {
|
|
168
|
+
const value = getEntryValue(entry)
|
|
169
|
+
|
|
170
|
+
if (isObject(value)) {
|
|
171
|
+
return !( // @ts-expect-error
|
|
172
|
+
value.$$typeof === REACT_COMPONENT_FORWARD_REF_TYPE /* forward ref */ ||
|
|
173
|
+
'current' in value /* ref shape */
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return true
|
|
178
|
+
}
|
|
179
|
+
|
|
131
180
|
/**
|
|
132
181
|
* @param {FiberNode} fiberNode
|
|
133
182
|
* @returns {Record<PropertyKey, unknown>}
|
|
@@ -144,17 +193,9 @@ function getProps (fiberNode) {
|
|
|
144
193
|
return (
|
|
145
194
|
Object.fromEntries(
|
|
146
195
|
Object.entries(props)
|
|
147
|
-
.filter(
|
|
148
|
-
.filter(
|
|
149
|
-
.filter(
|
|
150
|
-
if (isObject(value)) {
|
|
151
|
-
return !(
|
|
152
|
-
value.$$typeof !== REACT_COMPONENT_FORWARD_REF_TYPE /* forward ref */ ||
|
|
153
|
-
'current' in value /* ref shape */
|
|
154
|
-
)
|
|
155
|
-
}
|
|
156
|
-
return true
|
|
157
|
-
})
|
|
196
|
+
.filter(entryKeyIsNotChildren)
|
|
197
|
+
.filter(entryKeyIsNotRef) // key is not `ref`
|
|
198
|
+
.filter(entryValueIsNotRef) // ... but value may be a `ref`
|
|
158
199
|
)
|
|
159
200
|
)
|
|
160
201
|
}
|
|
@@ -162,6 +203,20 @@ function getProps (fiberNode) {
|
|
|
162
203
|
return {}
|
|
163
204
|
}
|
|
164
205
|
|
|
206
|
+
/**
|
|
207
|
+
* @param {FiberNode} fiberNode
|
|
208
|
+
* @returns {string | FiberNode}
|
|
209
|
+
*/
|
|
210
|
+
function toChildrenOf (fiberNode) {
|
|
211
|
+
const stateNode = fiberNode.stateNode
|
|
212
|
+
|
|
213
|
+
if (stateNode instanceof Text) {
|
|
214
|
+
return stateNode.data
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return fiberNode
|
|
218
|
+
}
|
|
219
|
+
|
|
165
220
|
/**
|
|
166
221
|
* @param {FiberNode} fiberNode
|
|
167
222
|
* @returns {Array<string | FiberNode>}
|
|
@@ -172,15 +227,10 @@ function getChildrenOf (fiberNode) {
|
|
|
172
227
|
if (child) {
|
|
173
228
|
const siblings = getSiblings(child)
|
|
174
229
|
|
|
175
|
-
return
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return stateNode.data
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return child
|
|
183
|
-
})
|
|
230
|
+
return (
|
|
231
|
+
[child].concat(siblings)
|
|
232
|
+
.map(toChildrenOf)
|
|
233
|
+
)
|
|
184
234
|
}
|
|
185
235
|
|
|
186
236
|
const stateNode = fiberNode.stateNode
|
package/src/index.d.mts
CHANGED
|
@@ -2,6 +2,8 @@ import type {
|
|
|
2
2
|
RenderResult as RenderType
|
|
3
3
|
} from '@testing-library/react'
|
|
4
4
|
|
|
5
|
+
import '#react-component-snapshot/fiber'
|
|
6
|
+
|
|
5
7
|
export function getComponentElement (render: RenderType): Element | null
|
|
6
8
|
|
|
7
9
|
export function getComponentElementFromRender (render: RenderType): Element | null
|
package/.publish/README.md
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
# Publish to NPM
|
|
2
|
-
|
|
3
|
-
For CircleCI or the shell
|
|
4
|
-
|
|
5
|
-
If you need an _authentication token_ to publish then you should resolve that before executing this script
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
echo ".publish/publish.sh" >> .npmignore
|
|
9
|
-
git clone https://github.com/modernpoacher/publish.git .publish
|
|
10
|
-
.publish/publish.sh
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
A Gist with the same content as `publish.sh` may need an additional step
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
echo ".publish/publish.sh" >> .npmignore
|
|
17
|
-
git clone https://gist.github.com/efd85e2b8da1ee830dd637f134994a9d.git .publish
|
|
18
|
-
chmod +x .publish/publish.sh
|
|
19
|
-
.publish/publish.sh
|
|
20
|
-
```
|