|
| 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