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 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
- const {
9
- container: {
10
- firstElementChild: element
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
- expect(snapshotOf(element))
17
- .toMatchSnapshot()
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-component-snapshot",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Transform a React component to match its snapshot in Jest",
5
5
  "keywords": [
6
6
  "react",
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 {{ container: { firstElementChild: Element | null } }} component
97
+ * @param {RenderType} render
97
98
  * @returns {Element | null}
98
99
  */
99
100
  export function getComponentElement ({
100
101
  container: {
101
- firstElementChild: element
102
+ firstElementChild: componentElement = null
102
103
  }
103
104
  }) {
104
- return element
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
  /**
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "NodeNext",
4
- "target": "ESNext",
5
- "allowJs": true,
6
- "checkJs": true,
7
- "noEmit": true,
8
- "strict": true,
9
- "jsx": "react"
10
- },
11
- "include": [
12
- "."
13
- ]
14
- }