1 | const la = require('lazy-ass')
|
2 | const is = require('check-more-types')
|
3 | const debug = require('debug')('snap-shot-store')
|
4 | const Result = require('folktale/result')
|
5 | const R = require('ramda')
|
6 |
|
7 | const isName = R.anyPass([is.unemptyString, is.strings])
|
8 | const nameLens = name =>
|
9 | is.unemptyString(name) ? R.lensProp(name) : R.lensPath(name)
|
10 | const fullName = name => (is.unemptyString(name) ? name : name.join(' - '))
|
11 |
|
12 |
|
13 | function findValue ({ snapshots, name, opts = {} }) {
|
14 | la(isName(name), 'invalid name to find spec for', name)
|
15 |
|
16 | if (opts.update) {
|
17 |
|
18 | return
|
19 | }
|
20 | if (!snapshots) {
|
21 | debug('there are no snapshots to find "%s"', name)
|
22 | return
|
23 | }
|
24 |
|
25 | const lens = nameLens(name)
|
26 | return R.view(lens, snapshots)
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function storeValue ({ snapshots, name, value, opts = {} }) {
|
40 | la(value !== undefined, 'cannot store undefined value')
|
41 | la(is.object(snapshots), 'missing snapshots object', snapshots)
|
42 | la(is.unemptyString(name) || is.strings(name), 'missing name', name)
|
43 |
|
44 | const lens = nameLens(name)
|
45 |
|
46 | if (opts.show || opts.dryRun) {
|
47 | console.log('updated snapshot "%s"', name)
|
48 | console.log(value)
|
49 | }
|
50 |
|
51 | if (!opts.dryRun) {
|
52 | debug('setting snapshot for name "%s"', name)
|
53 | return R.set(lens, value, snapshots)
|
54 | }
|
55 |
|
56 | return snapshots
|
57 | }
|
58 |
|
59 | const isValidCompareResult = is.schema({
|
60 | orElse: is.fn
|
61 | })
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function raiseIfDifferent ({ value, expected, specName, compare }) {
|
67 | la(value, 'missing value to compare', value)
|
68 | la(expected, 'missing expected value', expected)
|
69 | la(isName(specName), 'missing spec name', specName)
|
70 |
|
71 | const result = compare({ expected, value })
|
72 | la(
|
73 | isValidCompareResult(result),
|
74 | 'invalid compare result',
|
75 | result,
|
76 | 'when comparing value\n',
|
77 | value,
|
78 | 'with expected\n',
|
79 | expected
|
80 | )
|
81 |
|
82 | result.orElse(message => {
|
83 | debug('Test "%s" snapshot difference', fullName(specName))
|
84 | la(is.unemptyString(message), 'missing err string', message)
|
85 | console.log(message)
|
86 | throw new Error(message)
|
87 | })
|
88 | }
|
89 |
|
90 | function compare ({ expected, value }) {
|
91 | const e = JSON.stringify(expected)
|
92 | const v = JSON.stringify(value)
|
93 | if (e === v) {
|
94 | return Result.Ok()
|
95 | }
|
96 | return Result.Error(`${e} !== ${v}`)
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 | function strip (o) {
|
102 | if (is.fn(o)) {
|
103 | return o
|
104 | }
|
105 | let stringified
|
106 | try {
|
107 | stringified = JSON.stringify(o)
|
108 | } catch (e) {
|
109 | console.error('Could not stringify value')
|
110 | console.error('%j', o)
|
111 | throw e
|
112 | }
|
113 |
|
114 | try {
|
115 | const parsed = JSON.parse(stringified)
|
116 | return parsed
|
117 | } catch (e) {
|
118 | console.error('Could not parse value from string')
|
119 | console.error('%s', stringified)
|
120 | console.error('from value %j', o)
|
121 | throw e
|
122 | }
|
123 | }
|
124 |
|
125 | module.exports = {
|
126 | findValue,
|
127 | storeValue,
|
128 | raiseIfDifferent,
|
129 | compare,
|
130 | strip,
|
131 | isName,
|
132 | fullName
|
133 | }
|