-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.test.js
132 lines (112 loc) · 3.77 KB
/
validation.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
import validateFrontmatterLine, { BEGINNING_OF_LIST, KEY_VALUE_PAIR, LIST_ITEM } from './validation'
describe('errors', () => {
test('throw an error if a line starts with a colon', () => {
const testLine = ': and then some more stuff'
const testFrontmatter = {
line: testLine,
index: 0,
array: [testLine],
nextLine: undefined,
}
expect(() => validateFrontmatterLine(testFrontmatter)).toThrow()
})
test('throw an error if a line has no colon and does not start with a dash', () => {
const testLine = 'no colon and a dash - but not at the beginning'
const testFrontmatter = {
line: testLine,
index: 0,
array: [testLine],
nextLine: undefined,
}
expect(() => validateFrontmatterLine(testFrontmatter)).toThrow()
})
test('throw an error if starting a list, but the next line is not a list item', () => {
const testLine = 'start of a new list:'
const testNextLine = 'badly formatted list item (does not start with a dash)'
const testFrontmatter = {
line: testLine,
index: 0,
array: [testLine, testNextLine],
nextLine: testNextLine,
}
expect(() => validateFrontmatterLine(testFrontmatter).toThrow())
})
test('throw an error if a list item does not have a list starter as one of its previous lines', () => {
const testLine = '- a valid list item'
const testPreviousLine = 'an invalid list starter'
const testFrontmatter = {
line: testLine,
index: 1,
array: [testPreviousLine, testLine],
nextLine: undefined,
}
expect(() => validateFrontmatterLine(testFrontmatter).toThrow())
})
})
describe('valid frontmatter', () => {
test('key value pairs separated by a colon are valid', () => {
const testLine = 'key: value'
const testFrontmatter = {
line: testLine,
index: 0,
array: [testLine],
nextLine: undefined,
}
expect(validateFrontmatterLine(testFrontmatter)).toBe(KEY_VALUE_PAIR)
})
test('beginning of a list followed by a list item is valid', () => {
const testLine = 'beginning of a list:'
const nextLine = '- a list item'
const testFrontmatter = {
line: testLine,
index: 0,
array: [testLine, nextLine],
nextLine,
}
expect(validateFrontmatterLine(testFrontmatter)).toBe(BEGINNING_OF_LIST)
})
test('a list item in the middle of a list, with a previous list starter, is valid', () => {
const testLine = '- list item'
const firstListItem = '- first list item'
const nextLine = '- another list item'
const listStarter = 'beginning of a list:'
const testFrontmatter = {
line: testLine,
index: 1,
array: [listStarter, firstListItem, testLine, nextLine],
nextLine,
}
expect(validateFrontmatterLine(testFrontmatter)).toBe(LIST_ITEM)
})
test('a list item can contain extra dashes', () => {
const testLine = '- list item - with some - dashes - one'
const nextLine = '- list item two'
const testFrontmatter = {
line: testLine,
index: 1,
array: ['a list:', testLine, nextLine],
nextLine,
}
expect(validateFrontmatterLine(testFrontmatter)).toBe(LIST_ITEM)
})
test('a list item can contain a colon', () => {
const testLine = '- list : with some colons'
const testFrontmatter = {
line: testLine,
index: 1,
array: ['a list:', testLine],
nextLine: undefined,
}
expect(validateFrontmatterLine(testFrontmatter)).toBe(LIST_ITEM)
})
test('a list item can end with a colon', () => {
const testLine = '- list : with some colons:'
const testFrontmatter = {
line: testLine,
index: 1,
array: ['a list:', testLine],
nextLine: undefined,
}
expect(validateFrontmatterLine(testFrontmatter)).toBe(LIST_ITEM)
})
})