Skip to content

Commit 8491923

Browse files
committed
feat: add jest-snapshot-plugin
1 parent 409f5d1 commit 8491923

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

jest-snapshot-plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./src/jest-snapshot-plugin')

src/jest-snapshot-plugin.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
const _markup = require('pretty-format/build/plugins/lib/markup')
2+
3+
const testSymbol =
4+
typeof Symbol === 'function' && Symbol.for
5+
? Symbol.for('j-component.json')
6+
: 0xd846fe
7+
8+
const test = function test(val) {
9+
return val && val.$$typeof === testSymbol
10+
}
11+
12+
const printAttrs = function printAttrs(
13+
attrs,
14+
config,
15+
indentation,
16+
depth,
17+
refs,
18+
printer
19+
) {
20+
const indentationNext = indentation + config.indent
21+
const colors = config.colors
22+
return Array.from(attrs)
23+
.sort(function(a, b) {
24+
return a.name.localeCompare(b.name)
25+
})
26+
.map(function(attr) {
27+
const name = attr.name
28+
const value = attr.value
29+
let printed = printer(value, config, indentationNext, depth, refs)
30+
31+
if (typeof value !== 'string') {
32+
if (printed.indexOf('\n') !== -1) {
33+
printed =
34+
config.spacingOuter +
35+
indentationNext +
36+
printed +
37+
config.spacingOuter +
38+
indentation
39+
}
40+
41+
printed = '"{{' + printed + '}}"'
42+
}
43+
44+
return (
45+
config.spacingInner +
46+
indentation +
47+
colors.prop.open +
48+
name +
49+
colors.prop.close +
50+
'=' +
51+
colors.value.open +
52+
printed +
53+
colors.value.close
54+
)
55+
})
56+
.join('')
57+
}
58+
59+
const printEvent = function printEvent(
60+
event,
61+
config,
62+
indentation,
63+
depth,
64+
refs,
65+
printer
66+
) {
67+
const indentationNext = indentation + config.indent
68+
const colors = config.colors
69+
return Object.keys(event)
70+
.sort()
71+
.map(function(eventName) {
72+
const eventInfo = event[eventName]
73+
const handler = eventInfo.handler
74+
const isCapture = eventInfo.isCapture
75+
const isMutated = eventInfo.isMutated
76+
const isCatch = eventInfo.isCatch
77+
const attrName =
78+
(isCapture ? 'capture-' : '') +
79+
(isMutated ? 'mut-' : '') +
80+
(isCatch ? 'catch' : 'bind') +
81+
':' +
82+
eventName
83+
84+
const printed = printer(handler, config, indentationNext, depth, refs)
85+
return (
86+
config.spacingInner +
87+
indentation +
88+
colors.prop.open +
89+
attrName +
90+
colors.prop.close +
91+
'=' +
92+
colors.value.open +
93+
printed +
94+
colors.value.close
95+
)
96+
})
97+
.join('')
98+
}
99+
100+
const printProps = function printProps(
101+
object,
102+
config,
103+
indentation,
104+
depth,
105+
refs,
106+
printer
107+
) {
108+
return (
109+
printAttrs(object.attrs, config, indentation, depth, refs, printer) +
110+
printEvent(object.event, config, indentation, depth, refs, printer)
111+
)
112+
}
113+
114+
const serialize = function serialize(
115+
object,
116+
config,
117+
indentation,
118+
depth,
119+
refs,
120+
printer
121+
) {
122+
return ++depth > config.maxDepth
123+
? _markup.printElementAsLeaf(object.tagName, config)
124+
: _markup.printElement(
125+
object.tagName,
126+
printProps(
127+
object,
128+
config,
129+
indentation + config.indent,
130+
depth,
131+
refs,
132+
printer
133+
),
134+
_markup.printChildren(
135+
object.children,
136+
config,
137+
indentation + config.indent,
138+
depth,
139+
refs,
140+
printer
141+
),
142+
config,
143+
indentation
144+
)
145+
}
146+
147+
const plugin = {
148+
serialize,
149+
test,
150+
}
151+
152+
module.exports = plugin

0 commit comments

Comments
 (0)