1 | 'use strict'
|
2 |
|
3 | const la = require('lazy-ass')
|
4 | const is = require('check-more-types')
|
5 | const Result = require('folktale/result')
|
6 | const jsesc = require('jsesc')
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function snapshotIndex (options) {
|
14 | const counters = options.counters
|
15 | const file = options.file
|
16 | const specName = options.specName
|
17 |
|
18 | la(is.object(counters), 'expected counters', counters)
|
19 | la(is.unemptyString(specName), 'expected specName', specName)
|
20 | la(is.unemptyString(file), 'missing filename', file)
|
21 |
|
22 | if (!(specName in counters)) {
|
23 | counters[specName] = 1
|
24 | } else {
|
25 | counters[specName] += 1
|
26 | }
|
27 | return counters[specName]
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 | function strip (o) {
|
33 | if (is.fn(o)) {
|
34 | return o
|
35 | }
|
36 | return JSON.parse(JSON.stringify(o))
|
37 | }
|
38 |
|
39 | function compare (options) {
|
40 | const expected = options.expected
|
41 | const value = options.value
|
42 |
|
43 | const e = JSON.stringify(expected)
|
44 | const v = JSON.stringify(value)
|
45 | if (e === v) {
|
46 | return Result.Ok()
|
47 | }
|
48 | return Result.Error(`${e} !== ${v}`)
|
49 | }
|
50 |
|
51 | const sameTypes = (a, b) => typeof expected === typeof value
|
52 |
|
53 | const compareTypes = options => {
|
54 | const expected = options.expected
|
55 | const value = options.value
|
56 | return sameTypes(expected, value) ? Result.Ok() : Result.Error('no message')
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function exportText (name, value) {
|
65 | la(is.unemptyString(name), 'expected snapshot name, got:', name)
|
66 | la(is.string(value), 'expected string value', value)
|
67 |
|
68 |
|
69 |
|
70 | const serialized = value
|
71 | .split('\n')
|
72 | .map(line => {
|
73 | return jsesc(line, {
|
74 | quotes: 'backtick',
|
75 | minimal: true
|
76 | })
|
77 | })
|
78 | .join('\n')
|
79 | const withNewLines = '\n' + serialized + '\n'
|
80 | return `exports['${name}'] = \`${withNewLines}\`\n`
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function exportObject (name, value) {
|
87 | const serialized = jsesc(value, {
|
88 | json: true,
|
89 | compact: false,
|
90 | indent: ' ',
|
91 | minimal: true
|
92 | })
|
93 | return `exports['${name}'] = ${serialized}\n`
|
94 | }
|
95 |
|
96 | const isSurroundedByNewLines = s =>
|
97 | is.string(s) && s.length > 1 && s[0] === '\n' && s[s.length - 1] === '\n'
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | function removeExtraNewLines (snapshots) {
|
104 | Object.keys(snapshots).forEach(key => {
|
105 | const value = snapshots[key]
|
106 | if (isSurroundedByNewLines(value)) {
|
107 | snapshots[key] = value.substr(1, value.length - 2)
|
108 | }
|
109 | })
|
110 | return snapshots
|
111 | }
|
112 |
|
113 | const DEFAULT_EXTENSION = '.snapshot.js'
|
114 |
|
115 | module.exports = {
|
116 | snapshotIndex,
|
117 | strip,
|
118 | compare,
|
119 | sameTypes,
|
120 | compareTypes,
|
121 | exportText,
|
122 | exportObject,
|
123 | removeExtraNewLines,
|
124 | DEFAULT_EXTENSION
|
125 | }
|