This repository was archived by the owner on Aug 7, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidation.test.js
More file actions
143 lines (123 loc) · 4.14 KB
/
Copy pathvalidation.test.js
File metadata and controls
143 lines (123 loc) · 4.14 KB
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
/**
* Unit tests for input validation functions.
*/
import {jest} from '@jest/globals'
// This is a Jest mock function that simulates the validateInput function
// This is a Jest mock function that simulates the validateInput function
const mockValidateInput = flags => {
const {token, enterprise, owner, repository, csv, md, json, unique: _unique} = flags
// Ensure GitHub token is provided
if (!token) {
throw new Error('GitHub Personal Access Token (PAT) not provided')
}
// Ensure at least one processing option is provided
if (!(enterprise || owner || repository)) {
throw new Error('no options provided')
}
// Ensure only one processing option is provided at a time
if ((enterprise && owner) || (enterprise && repository) || (owner && repository)) {
throw new Error('can only use one of: enterprise, owner, repository')
}
// Validate output file paths when provided
if (csv === '') {
throw new Error('please provide a valid path for the CSV output')
}
if (md === '') {
throw new Error('please provide a valid path for the markdown output')
}
if (json === '') {
throw new Error('please provide a valid path for the JSON output')
}
// Process unique flag
const uniqueFlag = _unique === 'both' ? 'both' : _unique === 'true'
// Return uniqueFlag only if validation passes
return uniqueFlag
}
// Mock flags for testing
const baseFlags = {
token: 'test-token',
enterprise: null,
owner: null,
repository: null,
csv: null,
md: null,
json: null,
unique: false,
}
/**
* Test suite for input validation.
*/
describe('report input validation', () => {
beforeEach(() => {
jest.clearAllMocks()
})
/**
* Test token validation
*/
describe('token validation', () => {
/**
* Unit tests for input validation functions.
*/
it('should throw an error if no/empty GitHub token is provided', () => {
expect(() => mockValidateInput({...baseFlags, token: ''})).toThrow(
'GitHub Personal Access Token (PAT) not provided',
)
})
})
/**
* Test processing options validation
*/
describe('processing options validation', () => {
it('should throw an error if no processing options are provided', () => {
expect(() => mockValidateInput({...baseFlags, enterprise: null, owner: null, repository: null})).toThrow(
'no options provided',
)
})
it('should throw an error if multiple processing options are provided', () => {
expect(() =>
mockValidateInput({
...baseFlags,
enterprise: 'test-enterprise',
owner: 'test-owner',
}),
).toThrow('can only use one of: enterprise, owner, repository')
})
})
/**
* Test output path validation
*/
describe('output path validation', () => {
it('should throw an error if CSV output path is not provided', () => {
expect(() => mockValidateInput({...baseFlags, owner: 'mona', csv: ''})).toThrow(
'please provide a valid path for the CSV output',
)
})
it('should throw an error if Markdown output path is not provided', () => {
expect(() => mockValidateInput({...baseFlags, owner: 'mona', md: ''})).toThrow(
'please provide a valid path for the markdown output',
)
})
it('should throw an error if JSON output path is not provided', () => {
expect(() => mockValidateInput({...baseFlags, owner: 'mona', json: ''})).toThrow(
'please provide a valid path for the JSON output',
)
})
})
/**
* Test unique flag validation
*/
describe('unique flag validation', () => {
it('should return "both" for unique flag when set to "both"', () => {
const result = mockValidateInput({...baseFlags, owner: 'mona', unique: 'both'})
expect(result).toBe('both')
})
it('should return true for unique flag when set to true', () => {
const result = mockValidateInput({...baseFlags, owner: 'mona', unique: 'true'})
expect(result).toBe(true)
})
it('should return false for unique flag when set to false', () => {
const result = mockValidateInput({...baseFlags, owner: 'mona', unique: 'false'})
expect(result).toBe(false)
})
})
})