-
Notifications
You must be signed in to change notification settings - Fork 317
/
schema.test.js
109 lines (100 loc) · 3.38 KB
/
schema.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
const { schema, validateSchema } = require('../lib/schema')
const schemaJson = require('../schema.json')
const { jsonSchema } = require('../bin/generate-schema')
const template = '$CHANGES'
const context = {
payload: {
repository: {
default_branch: 'foobar',
},
},
}
const validConfigs = [
[{ template }],
[{ template, replacers: [{ search: '123', replace: '' }] }],
[{ template, replacers: [{ search: '/123/gi', replace: '' }] }],
[{ template, replacers: [{ search: '/123/gi', replace: '123' }] }],
]
const invalidConfigs = [
[{ template: true }, 'must be a string'],
[{ template: 1 }, 'must be a string'],
[{ template: ['👶'] }, 'must be a string'],
[{ template: { '👶': 'a' } }, 'must be a string'],
[{ template: null }, 'must be a string'],
[{ template: '' }, 'is not allowed to be empty'],
[{ 'category-template': ['## $TITLE'] }, 'must be a string'],
[{ 'category-template': null }, 'must be a string'],
[{ 'change-template': ['* $TITLE (#$NUMBER) @$AUTHOR'] }, 'must be a string'],
[{ 'change-template': null }, 'must be a string'],
[{ 'change-title-escapes': ['<_*'] }, 'must be a string'],
[{ 'change-title-escapes': null }, 'must be a string'],
[{ replacers: [{ search: 123 }] }, 'must be a regexp or a string'],
[{ replacers: [{ search: '123' }] }, 'is required'],
[
{ replacers: [{ replace: 123 }] },
'is required and must be a regexp or a string',
],
[{ replacers: [{ search: '123', replace: 123 }] }, 'must be a string'],
[{ commitish: false }, 'must be a string'],
]
describe('schema', () => {
describe('valid', () => {
for (const [example, expected = example] of validConfigs) {
test(`${JSON.stringify(example)} is valid`, () => {
const { error, value } = schema(context).validate(example, {
abortEarly: false,
})
expect(error).toBeNull()
expect(value).toMatchObject(expected)
if (value.replacers) {
for (const [index, arrayValue] of value.replacers.entries())
expect(arrayValue.search).toEqual(expected.replacers[index].search)
}
})
}
})
describe('invalid', () => {
for (const [example, message] of invalidConfigs) {
it(`${JSON.stringify(example)} is invalid`, () => {
const { error } = schema(context).validate(example, {
abortEarly: false,
})
expect(error && error.toString()).toMatch(message)
})
}
})
it('current schema matches the generated JSON Schema, update schema with `yarn generate-schema`', () => {
expect(jsonSchema).toMatchObject(schemaJson)
})
describe('validateSchema', () => {
it('Multiple other categories', () => {
expect(() => {
validateSchema(context, {
template,
categories: [
{
title: '📝 Other Changes',
},
{
title: '📝 Yet Other Changes',
},
],
})
}).toThrowErrorMatchingInlineSnapshot(`
"Multiple categories detected with no labels.
Only one category with no labels is supported for uncategorized pull requests."
`)
})
it('Single other categories', () => {
const expected = {
template,
categories: [
{
title: '📝 Other Changes',
},
],
}
expect(validateSchema(context, expected)).toMatchObject(expected)
})
})
})