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 pathenterprise.test.js
More file actions
229 lines (196 loc) · 6.42 KB
/
Copy pathenterprise.test.js
File metadata and controls
229 lines (196 loc) · 6.42 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Unit tests for the GitHub Enterprise module.
*/
import {jest} from '@jest/globals'
// Import the module under test
import Enterprise from '../../src/github/enterprise.js'
// Mock the Owner class to avoid GraphQL calls in Enterprise tests
jest.mock('../../src/github/owner.js', () => {
return jest.fn().mockImplementation(() => ({
login: undefined,
name: undefined,
id: undefined,
node_id: undefined,
type: undefined,
getRepositories: jest.fn().mockResolvedValue([
{name: 'repo1', owner: 'mocked-org'},
{name: 'repo2', owner: 'mocked-org'},
]),
}))
})
describe('enterprise', () => {
let enterprise
beforeEach(() => {
// Create instance with mocks
enterprise = new Enterprise('test-enterprise', {
token: 'test-token',
debug: false,
})
})
afterEach(() => {
jest.resetAllMocks()
})
/**
* Test that Enterprise class can be instantiated.
*/
test('should instantiate with valid token', () => {
expect(enterprise).toBeInstanceOf(Enterprise)
})
/**
* Test constructor options
*/
describe('constructor options', () => {
test('should handle all constructor parameters', () => {
const options = {
token: 'test-token',
hostname: 'github.enterprise.com',
debug: true,
archived: true,
forked: true,
}
const ent = new Enterprise('test-ent', options)
expect(ent.name).toBe('test-ent')
expect(ent.options).toEqual(options)
})
test('should handle default values', () => {
const ent = new Enterprise('test-ent', {token: 'test-token'})
expect(ent.name).toBe('test-ent')
expect(ent.organizations).toEqual([])
})
})
/**
* Test property getters and setters
*/
describe('property getters and setters', () => {
test('should handle name property correctly', () => {
expect(enterprise.name).toBe('test-enterprise')
enterprise.name = 'new-enterprise'
expect(enterprise.name).toBe('new-enterprise')
})
test('should handle id property correctly', () => {
expect(enterprise.id).toBeUndefined()
enterprise.id = 123
expect(enterprise.id).toBe(123)
})
test('should handle node_id property correctly', () => {
expect(enterprise.node_id).toBeUndefined()
enterprise.node_id = 'MDEwOkVudGVycHJpc2Ux'
expect(enterprise.node_id).toBe('MDEwOkVudGVycHJpc2Ux')
})
test('should handle organizations property correctly', () => {
expect(enterprise.organizations).toEqual([])
const orgs = [{login: 'org1'}, {login: 'org2'}]
enterprise.organizations = orgs
expect(enterprise.organizations).toEqual(orgs)
})
test('should return options object', () => {
expect(enterprise.options).toBeDefined()
expect(enterprise.options.token).toBe('test-token')
expect(enterprise.options.debug).toBe(false)
})
})
/**
* Test enterprise operations
*/
describe('enterprise operations', () => {
beforeEach(() => {
// Mock the octokit requests
enterprise.octokit.graphql = jest.fn()
})
test('should fetch organizations for enterprise (simplified)', async () => {
const mockGraphQLResponse = {
enterprise: {
name: 'test-enterprise',
id: 123,
node_id: 'MDEwOkVudGVycHJpc2Ux',
organizations: {
nodes: [],
pageInfo: {
hasNextPage: false,
endCursor: null,
},
},
},
}
enterprise.octokit.graphql.mockResolvedValueOnce(mockGraphQLResponse)
const orgs = await enterprise.getOrganizations('test-enterprise')
expect(Array.isArray(orgs)).toBe(true)
expect(enterprise.name).toBe('test-enterprise')
expect(enterprise.id).toBe(123)
expect(enterprise.node_id).toBe('MDEwOkVudGVycHJpc2Ux')
})
test('should handle pagination correctly (simplified)', async () => {
// First page
const mockFirstPage = {
enterprise: {
name: 'test-enterprise',
id: 123,
node_id: 'MDEwOkVudGVycHJpc2Ux',
organizations: {
nodes: [],
pageInfo: {
hasNextPage: true,
endCursor: 'cursor123',
},
},
},
}
// Second page
const mockSecondPage = {
enterprise: {
name: 'test-enterprise',
id: 123,
node_id: 'MDEwOkVudGVycHJpc2Ux',
organizations: {
nodes: [],
pageInfo: {
hasNextPage: false,
endCursor: null,
},
},
},
}
enterprise.octokit.graphql.mockResolvedValueOnce(mockFirstPage).mockResolvedValueOnce(mockSecondPage)
const orgs = await enterprise.getOrganizations('test-enterprise')
expect(orgs).toHaveLength(0)
expect(enterprise.octokit.graphql).toHaveBeenCalledTimes(2)
})
test('should handle empty enterprise name', async () => {
const orgs = await enterprise.getOrganizations('')
expect(orgs).toEqual([])
})
test('should handle null enterprise name', async () => {
const orgs = await enterprise.getOrganizations(null)
expect(orgs).toEqual([])
})
test('should handle undefined enterprise name', async () => {
const orgs = await enterprise.getOrganizations(undefined)
expect(orgs).toEqual([])
})
})
/**
* Test error handling
*/
describe('error handling', () => {
beforeEach(() => {
enterprise.octokit.graphql = jest.fn()
})
test('should handle GraphQL API errors gracefully', async () => {
const error = new Error('Enterprise not found')
enterprise.octokit.graphql.mockRejectedValueOnce(error)
await expect(enterprise.getOrganizations('nonexistent-enterprise')).rejects.toThrow('Enterprise not found')
})
test('should handle authentication errors', async () => {
const error = new Error('Bad credentials')
error.status = 401
enterprise.octokit.graphql.mockRejectedValueOnce(error)
await expect(enterprise.getOrganizations('test-enterprise')).rejects.toThrow('Bad credentials')
})
test('should handle rate limit errors', async () => {
const error = new Error('Rate limit exceeded')
error.status = 403
enterprise.octokit.graphql.mockRejectedValueOnce(error)
await expect(enterprise.getOrganizations('test-enterprise')).rejects.toThrow('Rate limit exceeded')
})
})
})