react-component-snapshot 0.0.4 → 0.0.5
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/README.md +29 -9
- package/package.json +1 -1
- package/src/index.mjs +94 -3
- package/tsconfig.json +0 -14
package/README.md
CHANGED
|
@@ -1,18 +1,38 @@
|
|
|
1
1
|
# react-component-snapshot
|
|
2
2
|
|
|
3
|
-
Transform a React component to match its snapshot in Jest
|
|
3
|
+
Transform a React component to match its snapshot in **Jest** with **Testing Library**
|
|
4
4
|
|
|
5
5
|
```javascript
|
|
6
6
|
import snapshotOf from 'react-component-snapshot'
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## With `@testing-library/react`
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
import {
|
|
13
|
+
render
|
|
14
|
+
} from '@testing-library/react'
|
|
7
15
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
16
|
+
import snapshotOf from 'react-component-snapshot'
|
|
17
|
+
|
|
18
|
+
describe('snapshotOf', () => {
|
|
19
|
+
class Component extends React.Component {
|
|
20
|
+
render () {
|
|
21
|
+
return <div />
|
|
22
|
+
}
|
|
11
23
|
}
|
|
12
|
-
} = render(
|
|
13
|
-
<Component />
|
|
14
|
-
)
|
|
15
24
|
|
|
16
|
-
|
|
17
|
-
|
|
25
|
+
it('matches the snapshot', () => {
|
|
26
|
+
const {
|
|
27
|
+
container: {
|
|
28
|
+
firstElementChild: element
|
|
29
|
+
}
|
|
30
|
+
} = render(
|
|
31
|
+
<Component />
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
expect(snapshotOf(element))
|
|
35
|
+
.toMatchSnapshot()
|
|
36
|
+
})
|
|
37
|
+
})
|
|
18
38
|
```
|
package/package.json
CHANGED
package/src/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* @typedef {import('@testing-library/react').RenderResult} RenderType
|
|
2
3
|
* @typedef {ReactComponentSnapshotTypes.FiberNode} FiberNode
|
|
3
4
|
*/
|
|
4
5
|
|
|
@@ -93,15 +94,105 @@ export function getStateNodeProps (stateNode) {
|
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
/**
|
|
96
|
-
* @param {
|
|
97
|
+
* @param {RenderType} render
|
|
97
98
|
* @returns {Element | null}
|
|
98
99
|
*/
|
|
99
100
|
export function getComponentElement ({
|
|
100
101
|
container: {
|
|
101
|
-
firstElementChild:
|
|
102
|
+
firstElementChild: componentElement = null
|
|
102
103
|
}
|
|
103
104
|
}) {
|
|
104
|
-
return
|
|
105
|
+
return componentElement
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @param {RenderType} render
|
|
110
|
+
* @returns {Element | null}
|
|
111
|
+
*/
|
|
112
|
+
export function getComponentElementFromRender (render) {
|
|
113
|
+
return (
|
|
114
|
+
getComponentElementFromContainerElement(
|
|
115
|
+
getContainerElementFromRender(render)
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @param {RenderType} render
|
|
122
|
+
* @returns {Element}
|
|
123
|
+
*/
|
|
124
|
+
export function getContainerElementFromRender ({
|
|
125
|
+
container: containerElement
|
|
126
|
+
}) {
|
|
127
|
+
return containerElement
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {Element} containerElement
|
|
132
|
+
* @returns {Element | null}
|
|
133
|
+
*/
|
|
134
|
+
function getComponentElementFromContainerElement ({
|
|
135
|
+
firstElementChild: componentElement = null
|
|
136
|
+
}) {
|
|
137
|
+
return componentElement
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {RenderType} render
|
|
142
|
+
* @returns {Record<PropertyKey, unknown> | null}
|
|
143
|
+
*/
|
|
144
|
+
export function toSnapshotFromRender (render) {
|
|
145
|
+
const element = getComponentElementFromRender(render)
|
|
146
|
+
|
|
147
|
+
if (element instanceof HTMLElement) {
|
|
148
|
+
const fiberNode = getFiber(element)
|
|
149
|
+
|
|
150
|
+
if (fiberNode) {
|
|
151
|
+
return (
|
|
152
|
+
transform(fiberNode)
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return null
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* @param {Element} containerElement
|
|
162
|
+
* @returns {Record<PropertyKey, unknown> | null}
|
|
163
|
+
*/
|
|
164
|
+
export function toSnapshotFromContainerElement (containerElement) {
|
|
165
|
+
const element = getComponentElementFromContainerElement(containerElement)
|
|
166
|
+
|
|
167
|
+
if (element instanceof HTMLElement) {
|
|
168
|
+
const fiberNode = getFiber(element)
|
|
169
|
+
|
|
170
|
+
if (fiberNode) {
|
|
171
|
+
return (
|
|
172
|
+
transform(fiberNode)
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return null
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @param {Element | null} componentElement
|
|
182
|
+
* @returns {Record<PropertyKey, unknown> | null}
|
|
183
|
+
*/
|
|
184
|
+
export function toSnapshotFromComponentElement (componentElement) {
|
|
185
|
+
if (componentElement instanceof HTMLElement) {
|
|
186
|
+
const fiberNode = getFiber(componentElement)
|
|
187
|
+
|
|
188
|
+
if (fiberNode) {
|
|
189
|
+
return (
|
|
190
|
+
transform(fiberNode)
|
|
191
|
+
)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return null
|
|
105
196
|
}
|
|
106
197
|
|
|
107
198
|
/**
|