-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (127 loc) · 3.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const objectHash = require('object-hash')
const underline = require('ansi-underline')
const {findMatches} = require('find-css-matches')
const INDENTATION = ' '
const SERIALIZER_ID = '___JEST__CSS__MATCH__SERIALIZER__OBJECT___'
const OPTIONS_KEY_ARRAY = [
'recursive',
'includeHtml',
'includeCss',
'includePartialMatches'
]
const formatSelector = (a, b) => [a, b ? underline(b) : b]
const cssHashFormatter = (cssArray, indent) => `${indent}${objectHash(cssArray)}`
const cssListFormatter = (cssArray, indent) => `${indent}${cssArray.join(`\n${indent}`)}`
/**
* @param {Array<String>} keys
* @param {Object} obj
* @returns {Object}
*/
function pick (keys, obj) {
return keys.reduce((acc, key) => {
if (obj[key] !== undefined) {
acc[key] = obj[key]
}
return acc
}, {})
}
/**
* stringifies an array of objects from find-css-matches
* @param {Array<Object>} array
* @param {Object} options
* @param {String} indent
* @returns {String}
*/
function stringifyArray (array, options, indent = '') {
const result = array.map((child, index) => {
return `${indent}[${index}]\n${stringify(child, options, indent)}`
}).join('\n')
if (indent === '') {
return result.replace(/\n$/, '')
}
return result
}
/**
* stringifies an object from find-css-matches
* @param {Object} param0
* @param {String} param0.html
* @param {Array<Object>} param0.matches
* @param {Array<Object>} param0.children
* @param {Object} options
* @param {Function} options.formatCss
* @param {String} indent
* @returns {String}
*/
function stringify ({html, matches, children = []}, options, indent = '') {
let result = ''
if (html) {
result += `${indent}${html}\n`
}
if (matches.length) {
result += matches.map(({selector, mediaText, css}) => {
// split the selector into separate lines for each comma
let str = `${indent}${selector.replace(/,\s*/g, `,\n${indent}`)}\n`
if (mediaText) {
str += `${indent}@media ${mediaText}\n`
}
if (css && options.formatCss) {
str += `${options.formatCss(css, indent)}\n`
}
return str
}).join('\n')
} else {
result += `${indent}<NULL>\n`
}
if (children.length) {
indent += INDENTATION
result += `\n${stringifyArray(children, options, indent)}`
}
return result
}
/**
* @param {String|Object|Array<Object>} styles
* @param {Object} instanceOptions
* @param {Object} expect
* @returns {Function}
*/
function serializer (styles, instanceOptions, expect) {
/**
* @param {String} html
* @param {Object} localOptions
* @returns {Array}
*/
async function findMatchesForSnapshot (html, localOptions) {
const userOptions = Object.assign({}, instanceOptions, localOptions)
const options = pick(OPTIONS_KEY_ARRAY, userOptions)
if (options.includePartialMatches) {
options.formatSelector = formatSelector
}
let formatCss
if (userOptions.includeCssHash === true) {
formatCss = cssHashFormatter
options.includeCss = true
} else if (options.includeCss === true) {
formatCss = cssListFormatter
}
let matches = await findMatches(styles, html, options)
if (!Array.isArray(matches)) {
matches = [matches]
}
const value = stringifyArray(matches, {formatCss}, '')
return {value, [SERIALIZER_ID]: true}
}
findMatchesForSnapshot.test = test
findMatchesForSnapshot.print = ({value}, serialize) => value
if (expect && typeof expect.addSnapshotSerializer === 'function') {
expect.addSnapshotSerializer(findMatchesForSnapshot)
}
return findMatchesForSnapshot
}
/**
* @param {*} value
* @returns {Boolean}
*/
function test (value) {
return !!value && typeof value === 'object' && value[SERIALIZER_ID] === true
}
module.exports = serializer