This repository has been archived by the owner on Oct 15, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test.js
163 lines (146 loc) · 4.21 KB
/
index.test.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
154
155
156
157
158
159
160
161
162
163
/* eslint no-console:0 */
import test from 'ava'
import sinon from 'sinon'
import configValidator from './index'
const EMPTY_RESULT = {
errors: [],
warnings: [],
warningMessage: undefined,
errorMessage: undefined,
}
test('No errors or warnings result in an return object with sensible "empty" values', t => {
const result = configValidator('Webpack Config', {}, [])
t.same(result, EMPTY_RESULT)
})
test('Errors are returned in the `errors` property of the return object ' +
'and as formatted string under `errorMessage`', t => {
const config = {bar: 'baz'}
const validators = [{key: 'bar', validate: () => 'error'}]
const result = configValidator('Webpack Config', config, validators)
t.same(result.errors, [
{
key: 'bar',
message: 'error',
value: 'baz',
validator: validators[0],
type: 'error',
},
])
t.ok(result.errorMessage) // Test implementation details of this in `formatMessagesAsString` tests
})
test('Warnings are returned in the `warnings` property of the return object ' +
'and as formatted string under `warningMessage`', t => {
const config = {bar: 'baz'}
const validators = [{key: 'bar', validate: () => ({warning: 'warning'})}]
const result = configValidator('Webpack Config', config, validators)
t.same(result.warnings, [
{
key: 'bar',
message: 'warning',
value: 'baz',
validator: validators[0],
type: 'warning',
},
])
t.ok(result.warningMessage) // Test implementation details of this in `formatMessagesAsString` tests
})
test('Unknown fields result in a warning', t => {
const config = {bar: 'baz', cat: 'bag'}
const validators = [{key: 'bar', validate: () => 'error'}]
const result = configValidator('Webpack Config', config, validators)
t.ok(result.errors.length === validators.length)
t.ok(result.errorMessage)
t.ok(result.warnings.length === 1)
t.same(result.warnings[0], {
key: 'cat',
message: 'Unknown key',
type: 'warning',
})
t.ok(result.warningMessage)
})
test('Nested fields should not trigger warnings if they validate', t => {
const config = {
foo: {
bar: true,
baz: 23,
},
}
const validators = [
{key: 'foo.bar', validate() {}},
{key: 'foo.baz', validate() {}},
]
const result = configValidator('Webpack Config', config, validators)
t.same(result, EMPTY_RESULT)
})
test('Null fields should not trigger warnings if they validate', t => {
const config = {
foo: null,
help: 'me',
}
const validators = [
{key: 'foo', validate() {}},
{key: 'help', validate() {}},
]
const result = configValidator('Webpack Config', config, validators)
t.same(result, EMPTY_RESULT)
})
test('Array fields should not have nested checks', t => {
const config = {
foo: null,
help: ['me', 'you', 'them'],
}
const validators = [
{key: 'foo', validate() {}},
{key: 'help', validate() {}},
]
const result = configValidator('Webpack Config', config, validators)
t.same(result, EMPTY_RESULT)
})
test('Nested fields trigger when not validated', t => {
const config = {
foo: {
bar: true,
baz: 23,
},
}
const validators = [
{key: 'foo.bar', validate() {}},
]
const result = configValidator('Webpack Config', config, validators)
t.ok(result.warnings.length === 1)
t.same(result.warnings[0], {
key: 'foo.baz',
message: 'Unknown key',
type: 'warning',
})
})
test('Deeply nested fields trigger when not validated', t => {
const config = {
foo: {
bar: {
cat: 'sink',
},
baz: 23,
},
}
const validators = [
{key: 'foo.bar.cat', validate: () => 'error'},
]
const result = configValidator('Webpack Config', config, validators)
t.ok(result.errors.length === 1)
t.same(result.errors[0], {
key: 'foo.bar.cat',
message: 'error',
type: 'error',
validator: validators[0],
value: 'sink',
})
})
test('Does not log anything, that\'s left to the API consumer', t => {
const consoleLogSpy = sinon.spy(console, 'log')
const config = {bar: 'baz'}
const validators = [{key: 'bar', validate: () => ({warning: 'warning'})}]
configValidator('Webpack Config', config, validators)
t.true(consoleLogSpy.callCount === 0)
consoleLogSpy.restore()
})