This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
group.test.js
131 lines (116 loc) · 3.91 KB
/
group.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
const fs = require('fs')
const Group = require('./group')
const capture = require('./capture')
describe('group.js', () => {
describe('#Group', () => {
;[
'title',
'desc',
'scheme',
'host',
'version',
'basePath',
'params',
'query',
'reqHeaders',
].forEach((methodName) => {
test(`should be able to chain ${methodName}() method`, () => {
const group = new Group()
expect(group[methodName]).toBeInstanceOf(Function)
expect(
group[methodName]('blah', 'blah', 'blah', 'blah')[methodName]
).toBeInstanceOf(Function)
})
})
describe('#basePath()', () => {
test('should strip a preceding slash', () => {
const group = new Group()
group.basePath('/path')
expect(group.docs.basePath).toBe('path')
})
test('should strip a trailing slash', () => {
const group = new Group()
group.basePath('path/')
expect(group.docs.basePath).toBe('path')
})
})
describe('#val()', () => {
test('should return the captured value', () => {
const group = new Group()
const capturedValue = group.val({}, '')
expect(capturedValue[capture.contextSymbol]).toBeDefined()
})
test('should set the description of the captured value', () => {
const group = new Group()
const desc = 'value desc'
const capturedValue = group.val({}, desc)
expect(capturedValue[capture.contextSymbol].docs.descriptions[0]).toBe(
desc
)
})
})
describe('#group()', () => {
test('should reuse an existing child group for a same title', () => {
const group = new Group()
const childGroup = group.group('Sample Group')
expect(group.group('Sample Group')).toBe(childGroup)
})
})
describe('#action()', () => {
test('should reuse an existing child action for a same title and treat as another example', () => {
const group = new Group()
const action = group.action('Sample Action')
const exampleIndex = action.example
const anotherAction = group.action('Sample Action')
expect(anotherAction).toBe(action)
expect(anotherAction.example).toBe(exampleIndex + 1)
})
})
describe('#is()', () => {
test('should call `collectFn` with itself as the parameter', () => {
const group = new Group()
group.is((doc) => {
expect(doc).toBe(group)
})
})
test('should return itself if `collectFn` is synchronized', () => {
const group = new Group()
expect(group.is(() => {})).toBe(group)
})
test('should return a promise that resolves with itself if `collectFn` returns a promise', () => {
const group = new Group()
group
.is(
() =>
new Promise((resolve) => {
resolve()
})
)
.then((doc) => expect(doc).toBe(group))
})
})
describe('#uncapture()', () => {
test('should return an uncaptured object for a proxy', () => {
const group = new Group()
expect(group.uncapture(capture(null))).toBeNull()
})
})
describe('#emit()', () => {
test("should accept a custom function as the 'generator' param", () => {
const group = new Group()
group.title('Sample Documentation')
expect(group.emit(undefined, () => 'fake result')).toBe('fake result')
})
test('should return text directly if parameter `file` is omitted', () => {
const group = new Group()
group.title('Sample Documentation')
expect(group.emit().includes('Sample Documentation')).toBe(true)
})
test('should write to file for a given file path', () => {
const group = new Group()
group.emit('sample-for-test.apib')
fs.unlinkSync('sample-for-test.apib')
})
})
})
})