Skip to content

Commit a22ba36

Browse files
committed
Add more tests
1 parent da3ebfc commit a22ba36

10 files changed

Lines changed: 218 additions & 15 deletions

File tree

__tests__/actions.test.ts

Lines changed: 124 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const issues_complete: jest.SpiedFunction<
2727
const issues_generateMessage: jest.SpiedFunction<
2828
typeof import('../src/github/issues.js').generateMessage
2929
> = jest.fn()
30+
const issues_parse: jest.SpiedFunction<
31+
typeof import('../src/github/issues.js').parse
32+
> = jest.fn()
3033
const repos_exists: jest.SpiedFunction<
3134
typeof import('../src/github/repos.js').exists
3235
> = jest.fn()
@@ -68,7 +71,8 @@ jest.unstable_mockModule('../src/github/issues.js', () => {
6871
return {
6972
addLabels: issues_addLabels,
7073
complete: issues_complete,
71-
generateMessage: issues_generateMessage
74+
generateMessage: issues_generateMessage,
75+
parse: issues_parse
7276
}
7377
})
7478
jest.unstable_mockModule('../src/github/repos.js', () => {
@@ -238,9 +242,7 @@ describe('actions', () => {
238242
}
239243
]
240244
},
241-
{
242-
issue: { state: 'open', number: 1 }
243-
} as any
245+
{ state: 'open', number: 1 } as any
244246
)
245247

246248
expect(repos_deleteRepositories).toHaveBeenCalled()
@@ -269,9 +271,7 @@ describe('actions', () => {
269271
}
270272
]
271273
},
272-
{
273-
issue: { state: 'closed', number: 1 }
274-
} as any
274+
{ state: 'closed', number: 1 } as any
275275
)
276276

277277
expect(repos_deleteRepositories).toHaveBeenCalled()
@@ -282,6 +282,42 @@ describe('actions', () => {
282282
})
283283
})
284284

285+
describe('expire()', () => {
286+
it('Expires an Open Class', async () => {
287+
mocktokit.paginate.mockResolvedValue([
288+
{
289+
state: 'open',
290+
number: 1
291+
}
292+
])
293+
issues_parse.mockReturnValue({
294+
action: AllowedIssueAction.CREATE,
295+
customerName: 'Nick Testing Industries',
296+
customerAbbr: 'NA1',
297+
startDate: new Date(2023, 10, 17),
298+
endDate: new Date(2023, 10, 20),
299+
administrators: [
300+
{
301+
handle: 'ncalteen',
302+
email: 'ncalteen@github.com'
303+
}
304+
],
305+
attendees: [
306+
{
307+
handle: 'ncalteen-testuser',
308+
email: 'ncalteen+testing@github.com'
309+
}
310+
]
311+
})
312+
313+
await actions.expire()
314+
315+
expect(repos_deleteRepositories).toHaveBeenCalled()
316+
expect(users_removeUsers).toHaveBeenCalled()
317+
expect(teams_deleteTeam).toHaveBeenCalled()
318+
})
319+
})
320+
285321
describe('addAdmin()', () => {
286322
it('Throws on Invalid Format', async () => {
287323
try {
@@ -359,6 +395,48 @@ describe('actions', () => {
359395
expect(teams_addUser).toHaveBeenCalled()
360396
expect(issues_complete).toHaveBeenCalled()
361397
})
398+
399+
it('Throws on Invalid Admin', async () => {
400+
mocktokit.graphql.mockResolvedValue({
401+
user: {
402+
isEmployee: false,
403+
email: 'fakeuser@notgithub.com'
404+
}
405+
})
406+
407+
try {
408+
await actions.addAdmin(
409+
{
410+
action: AllowedIssueAction.CREATE,
411+
customerName: 'Nick Testing Industries',
412+
customerAbbr: 'NA1',
413+
startDate: new Date(2024, 10, 17),
414+
endDate: new Date(2024, 10, 20),
415+
administrators: [
416+
{
417+
handle: 'ncalteen',
418+
email: 'ncalteen@github.com'
419+
}
420+
],
421+
attendees: [
422+
{
423+
handle: 'ncalteen-testuser',
424+
email: 'ncalteen+testing@github.com'
425+
}
426+
]
427+
},
428+
{
429+
issue: { number: 1 },
430+
comment: {
431+
body: '.add-admin fakeuser,fakeuser@notgithub.com'
432+
}
433+
} as any
434+
)
435+
} catch (error: any) {
436+
// eslint-disable-next-line jest/no-conditional-expect
437+
expect(error.message).toBe('Admins Must be GitHub/Microsoft Employees')
438+
}
439+
})
362440
})
363441

364442
describe('addUser()', () => {
@@ -434,4 +512,43 @@ describe('actions', () => {
434512
expect(issues_complete).toHaveBeenCalled()
435513
})
436514
})
515+
516+
describe('removeAdmin()', () => {
517+
it('Throws on Invalid Format', async () => {
518+
try {
519+
await actions.removeAdmin(
520+
{
521+
action: AllowedIssueAction.CREATE,
522+
customerName: 'Nick Testing Industries',
523+
customerAbbr: 'NA1',
524+
startDate: new Date(2024, 10, 17),
525+
endDate: new Date(2024, 10, 20),
526+
administrators: [
527+
{
528+
handle: 'ncalteen',
529+
email: 'ncalteen@github.com'
530+
}
531+
],
532+
attendees: [
533+
{
534+
handle: 'ncalteen-testuser',
535+
email: 'ncalteen+testing@github.com'
536+
}
537+
]
538+
},
539+
{
540+
issue: { number: 1 },
541+
comment: {
542+
body: '.remove-admin invalid format'
543+
}
544+
} as any
545+
)
546+
} catch (error: any) {
547+
// eslint-disable-next-line jest/no-conditional-expect
548+
expect(error.message).toBe(
549+
'Invalid Format! Try `.remove-admin handle,email`'
550+
)
551+
}
552+
})
553+
})
437554
})

__tests__/events.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { jest } from '@jest/globals'
2+
import * as core from '../__fixtures__/core.js'
23
import { AllowedIssueAction, AllowedIssueCommentAction } from '../src/enums'
34

5+
jest.unstable_mockModule('@actions/core', () => core)
6+
47
const events = await import('../src/events.js')
58

69
describe('events', () => {
@@ -9,6 +12,16 @@ describe('events', () => {
912
})
1013

1114
describe('getAction()', () => {
15+
it('Returns Expire Action for an Expire Event', () => {
16+
core.getInput.mockReturnValueOnce('true')
17+
18+
expect(
19+
events.getAction('issues', {
20+
action: 'opened'
21+
} as any)
22+
).toBe(AllowedIssueAction.EXPIRE)
23+
})
24+
1225
it('Returns Create Action for Issue Opened Event', () => {
1326
expect(
1427
events.getAction('issues', {

__tests__/github/issues.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,30 @@ describe('issues', () => {
462462
).toBe('string')
463463
})
464464

465+
it('Generates an Expire Message', () => {
466+
expect(
467+
typeof issues.generateMessage({
468+
action: AllowedIssueAction.EXPIRE,
469+
customerName: 'Nick Testing Industries',
470+
customerAbbr: 'NA1',
471+
startDate: new Date(2024, 10, 17),
472+
endDate: new Date(2024, 10, 20),
473+
administrators: [
474+
{
475+
handle: 'ncalteen',
476+
email: 'ncalteen@github.com'
477+
}
478+
],
479+
attendees: [
480+
{
481+
handle: 'ncalteen-testuser',
482+
email: 'ncalteen+testing@github.com'
483+
}
484+
]
485+
})
486+
).toBe('string')
487+
})
488+
465489
it('Generates a Remove Admin Message', () => {
466490
github.context.payload.comment.body =
467491
'.remove-admin ncalteen,ncalteen@github.com'
@@ -543,4 +567,22 @@ describe('issues', () => {
543567
}
544568
})
545569
})
570+
571+
describe('addLabels()', () => {
572+
it('Adds Labels to an Issue', async () => {
573+
await issues.addLabels(
574+
{
575+
number: 1
576+
} as any,
577+
['label1', 'label2']
578+
)
579+
580+
expect(mocktokit.rest.issues.addLabels).toHaveBeenCalledWith({
581+
issue_number: 1,
582+
owner: 'githubschool',
583+
repo: 'gh-github-intermediate-issueops',
584+
labels: ['label1', 'label2']
585+
})
586+
})
587+
})
546588
})

__tests__/main.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const actions_create: jest.SpiedFunction<
3030
const actions_close: jest.SpiedFunction<
3131
typeof import('../src/actions.js').close
3232
> = jest.fn()
33+
const actions_expire: jest.SpiedFunction<
34+
typeof import('../src/actions.js').expire
35+
> = jest.fn()
3336
const actions_addAdmin: jest.SpiedFunction<
3437
typeof import('../src/actions.js').addAdmin
3538
> = jest.fn()
@@ -57,6 +60,7 @@ jest.unstable_mockModule('../src/actions.js', () => {
5760
return {
5861
create: actions_create,
5962
close: actions_close,
63+
expire: actions_expire,
6064
addAdmin: actions_addAdmin,
6165
addUser: actions_addUser,
6266
removeAdmin: actions_removeAdmin,
@@ -66,6 +70,9 @@ jest.unstable_mockModule('../src/actions.js', () => {
6670

6771
const main = await import('../src/main.js')
6872

73+
const { Octokit } = await import('@octokit/rest')
74+
const mocktokit = jest.mocked(new Octokit())
75+
6976
describe('main', () => {
7077
afterEach(() => {
7178
jest.resetAllMocks()
@@ -85,6 +92,14 @@ describe('main', () => {
8592
expect(core.setFailed).not.toHaveBeenCalled()
8693
})
8794

95+
it('Processes an Expire Event', async () => {
96+
events_getAction.mockReturnValue(AllowedIssueAction.EXPIRE)
97+
98+
await main.run()
99+
100+
expect(actions_expire).toHaveBeenCalledTimes(1)
101+
})
102+
88103
it('Processes a Class Create Event', async () => {
89104
events_getAction.mockReturnValue(AllowedIssueAction.CREATE)
90105

@@ -132,4 +147,14 @@ describe('main', () => {
132147

133148
expect(actions_removeUser).toHaveBeenCalledTimes(1)
134149
})
150+
151+
it('Comments if an Error Occurs', async () => {
152+
events_getAction.mockReturnValue(AllowedIssueAction.CREATE)
153+
actions_create.mockRejectedValue(new Error('Test Error'))
154+
155+
await main.run()
156+
157+
expect(mocktokit.rest.issues.createComment).toHaveBeenCalled()
158+
expect(core.setFailed).toHaveBeenCalled()
159+
})
135160
})

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

dist/index.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jest.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ const jestConfig: JestConfigWithTsJest = {
99
coverageReporters: ['json-summary', 'text', 'lcov'],
1010
coverageThreshold: {
1111
global: {
12-
branches: 50,
13-
functions: 50,
14-
lines: 50,
15-
statements: 50
12+
branches: 70,
13+
functions: 70,
14+
lines: 70,
15+
statements: 70
1616
}
1717
},
1818
extensionsToTreatAsEsm: ['.ts'],

src/actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ export async function expire(): Promise<void> {
126126
)
127127

128128
// If the end date has passed, close the request.
129-
if (request.endDate < new Date())
129+
if (request.endDate < new Date()) {
130130
await close(request, issue as IssuesEvent['issue'])
131+
}
131132
}
132133

133134
core.info('Expired Open Classes')

src/github/repos.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ export async function configure(
135135
cwd: workspace,
136136
listeners: {
137137
stdout: (data: Buffer) => {
138+
/* istanbul ignore next */
138139
core.info(data.toString())
139140
},
140141
stderr: (data: Buffer) => {
142+
/* istanbul ignore next */
141143
core.error(data.toString())
142144
}
143145
}

0 commit comments

Comments
 (0)