Skip to content

Commit 5842c6f

Browse files
committed
Add more testing
1 parent 64cedd2 commit 5842c6f

14 files changed

Lines changed: 558 additions & 35 deletions

File tree

__fixtures__/core.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { jest } from '@jest/globals'
2+
3+
export const debug = jest.fn()
4+
export const error = jest.fn()
5+
export const info = jest.fn()
6+
export const getInput = jest.fn()
7+
export const setOutput = jest.fn()
8+
export const setFailed = jest.fn()
9+
export const warning = jest.fn()

__fixtures__/exec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { jest } from '@jest/globals'
2+
3+
export const exec = jest.fn()

__fixtures__/github.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as octokit from './octokit.js'
2+
3+
export const getOctokit = () => octokit
4+
5+
export const context = {
6+
eventName: 'issues',
7+
payload: {
8+
action: 'opened',
9+
issue: {
10+
body: '### Customer Name\n\nNick Testing Industries\n\n### Customer Abbreviation\n\nNA1\n\n### Start Date\n\n2024-10-17\n\n### End Date\n\n2024-10-20\n\n### Administrators\n\nncalteen,ncalteen@github.com\n\n### Attendees\nncalteen-testuser,ncalteen+github@gmail.com',
11+
number: 1
12+
}
13+
}
14+
}

__fixtures__/octokit.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { jest } from '@jest/globals'
2+
import { Endpoints } from '@octokit/types'
3+
4+
export const graphql = jest.fn()
5+
export const paginate = jest.fn()
6+
export const rest = {
7+
issues: {
8+
addLabels: jest.fn(),
9+
createComment: jest.fn(),
10+
get: jest.fn<
11+
() => Promise<
12+
Endpoints['GET /repos/{owner}/{repo}/issues/{issue_number}']['response']
13+
>
14+
>(),
15+
listComments: jest.fn(),
16+
removeLabel: jest.fn(),
17+
update: jest.fn()
18+
},
19+
orgs: {
20+
checkMembershipForUser: jest.fn(),
21+
createOrUpdateCustomPropertiesValuesForRepos: jest.fn()
22+
},
23+
pulls: {
24+
create:
25+
jest.fn<
26+
() => Promise<Endpoints['POST /repos/{owner}/{repo}/pulls']['response']>
27+
>(),
28+
listFiles: jest.fn()
29+
},
30+
repos: {
31+
createOrUpdateEnvironment: jest.fn(),
32+
createPagesSite: jest.fn(),
33+
createUsingTemplate: jest.fn(),
34+
get: jest.fn<
35+
() => Promise<Endpoints['GET /repos/{owner}/{repo}']['response']>
36+
>(),
37+
getContent: jest.fn(),
38+
update: jest.fn()
39+
},
40+
teams: {
41+
addOrUpdateMembershipForUserInOrg: jest.fn(),
42+
addOrUpdateRepoPermissionsInOrg: jest.fn(),
43+
create: jest.fn(),
44+
removeMembershipForUserInOrg: jest.fn()
45+
}
46+
}

__tests__/github/repos.test.ts

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import { jest } from '@jest/globals'
2+
import * as core from '../../__fixtures__/core.js'
3+
import * as exec from '../../__fixtures__/exec.js'
4+
import * as github from '../../__fixtures__/github.js'
5+
import * as octokit from '../../__fixtures__/octokit.js'
6+
import { AllowedIssueAction } from '../../src/enums.js'
7+
8+
jest.unstable_mockModule('@actions/core', () => core)
9+
jest.unstable_mockModule('@actions/exec', () => exec)
10+
jest.unstable_mockModule('@actions/github', () => github)
11+
jest.unstable_mockModule('@octokit/rest', async () => {
12+
class Octokit {
13+
constructor() {
14+
return octokit
15+
}
16+
}
17+
18+
return {
19+
Octokit
20+
}
21+
})
22+
23+
const repos = await import('../../src/github/repos.js')
24+
25+
const { Octokit } = await import('@octokit/rest')
26+
const mocktokit = jest.mocked(new Octokit())
27+
28+
describe('repos', () => {
29+
afterEach(() => {
30+
jest.resetAllMocks()
31+
})
32+
33+
describe('generateRepoName', () => {
34+
it('Generates a Repo Name', () => {
35+
expect(
36+
repos.generateRepoName(
37+
{
38+
action: AllowedIssueAction.CREATE,
39+
customerName: 'Nick Testing Industries',
40+
customerAbbr: 'NA1',
41+
startDate: new Date(2024, 10, 17),
42+
endDate: new Date(2024, 10, 20),
43+
administrators: [
44+
{
45+
handle: 'ncalteen',
46+
email: 'ncalteen@github.com'
47+
}
48+
],
49+
attendees: [
50+
{
51+
handle: 'ncalteen-testuser',
52+
email: 'ncalteen+testing@github.com'
53+
}
54+
]
55+
},
56+
{
57+
handle: 'ncalteen-testuser',
58+
email: 'ncalteen+testing@github.com'
59+
}
60+
)
61+
).toBe('gh-int-na1-ncalteen-testuser')
62+
})
63+
})
64+
65+
describe('create', () => {
66+
beforeEach(() => {
67+
core.getInput.mockReturnValue('github-token')
68+
mocktokit.rest.repos.createUsingTemplate.mockResolvedValue({
69+
data: {
70+
name: 'repo-name'
71+
}
72+
} as any)
73+
})
74+
75+
it('Creates a Repo', async () => {
76+
await repos.create(
77+
{
78+
action: AllowedIssueAction.CREATE,
79+
customerName: 'Nick Testing Industries',
80+
customerAbbr: 'NA1',
81+
startDate: new Date(2024, 10, 17),
82+
endDate: new Date(2024, 10, 20),
83+
administrators: [
84+
{
85+
handle: 'ncalteen',
86+
email: 'ncalteen@github.com'
87+
}
88+
],
89+
attendees: [
90+
{
91+
handle: 'ncalteen-testuser',
92+
email: 'ncalteen+testing@github.com'
93+
}
94+
]
95+
},
96+
{
97+
handle: 'ncalteen-testuser',
98+
email: 'ncalteen+testing@github.com'
99+
},
100+
{
101+
id: 1234,
102+
slug: 'team-name'
103+
}
104+
)
105+
106+
expect(mocktokit.rest.repos.createUsingTemplate).toHaveBeenCalled()
107+
expect(
108+
mocktokit.rest.teams.addOrUpdateRepoPermissionsInOrg
109+
).toHaveBeenCalled()
110+
})
111+
})
112+
113+
describe('configure', () => {
114+
beforeEach(() => {
115+
core.getInput
116+
.mockReturnValueOnce('workspace')
117+
.mockReturnValueOnce('github-token')
118+
mocktokit.rest.repos.createPagesSite.mockResolvedValue({
119+
data: {
120+
html_url: 'pages-url'
121+
}
122+
} as any)
123+
})
124+
125+
it('Configures a Repo', async () => {
126+
await repos.configure(
127+
{
128+
action: AllowedIssueAction.CREATE,
129+
customerName: 'Nick Testing Industries',
130+
customerAbbr: 'NA1',
131+
startDate: new Date(2024, 10, 17),
132+
endDate: new Date(2024, 10, 20),
133+
administrators: [
134+
{
135+
handle: 'ncalteen',
136+
email: 'ncalteen@github.com'
137+
}
138+
],
139+
attendees: [
140+
{
141+
handle: 'ncalteen-testuser',
142+
email: 'ncalteen+testing@github.com'
143+
}
144+
]
145+
},
146+
{
147+
handle: 'ncalteen-testuser',
148+
email: 'ncalteen+testing@github.com'
149+
},
150+
'gh-int-na1-ncalteen-testuser',
151+
{
152+
id: 1234,
153+
slug: 'team-name'
154+
}
155+
)
156+
157+
expect(mocktokit.rest.repos.createOrUpdateEnvironment).toHaveBeenCalled()
158+
expect(mocktokit.rest.repos.createPagesSite).toHaveBeenCalled()
159+
expect(mocktokit.rest.repos.update).toHaveBeenCalled()
160+
})
161+
})
162+
})

0 commit comments

Comments
 (0)