|
| 1 | +import {getRelease, createOrUpdateRelease} from '../src/release' |
| 2 | +import * as github from '@actions/github' |
| 3 | +import {Inputs} from '../src/context' |
| 4 | + |
| 5 | +const fs = jest.requireActual('fs') |
| 6 | + |
| 7 | +jest.mock('@actions/core') |
| 8 | +jest.mock('@actions/github') |
| 9 | + |
| 10 | +let gh: ReturnType<typeof github.getOctokit> |
| 11 | + |
| 12 | +describe('getRelease', () => { |
| 13 | + beforeEach(() => { |
| 14 | + jest.clearAllMocks() |
| 15 | + gh = github.getOctokit('_') |
| 16 | + }) |
| 17 | + |
| 18 | + it('should return the latest release when multiple releases exist', async () => { |
| 19 | + const mockResponse: any = { |
| 20 | + headers: {}, |
| 21 | + status: 200, |
| 22 | + data: [ |
| 23 | + { |
| 24 | + tag_name: 'v1.0.2', |
| 25 | + target_commitish: 'main', |
| 26 | + draft: false, |
| 27 | + }, |
| 28 | + { |
| 29 | + tag_name: 'v1.0.1', |
| 30 | + target_commitish: 'main', |
| 31 | + draft: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + tag_name: 'v1.0.0', |
| 35 | + target_commitish: 'main', |
| 36 | + draft: false, |
| 37 | + }, |
| 38 | + ], |
| 39 | + } |
| 40 | + |
| 41 | + const mockReleases = jest.spyOn(gh.rest.repos, 'listReleases') |
| 42 | + mockReleases.mockResolvedValue(mockResponse) |
| 43 | + |
| 44 | + const [releases, latestRelease] = await getRelease(gh) |
| 45 | + |
| 46 | + expect(releases).toHaveLength(3) |
| 47 | + expect(latestRelease).toBe('v1.0.2') |
| 48 | + }) |
| 49 | + |
| 50 | + it('should return the latest for the current branch', async () => { |
| 51 | + const mockResponse: any = { |
| 52 | + headers: {}, |
| 53 | + status: 200, |
| 54 | + data: [ |
| 55 | + { |
| 56 | + tag_name: 'v1.0.2', |
| 57 | + target_commitish: 'dev', |
| 58 | + draft: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + tag_name: 'v1.0.1', |
| 62 | + target_commitish: 'main', |
| 63 | + draft: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + tag_name: 'v1.0.0', |
| 67 | + target_commitish: 'main', |
| 68 | + draft: false, |
| 69 | + }, |
| 70 | + ], |
| 71 | + } |
| 72 | + |
| 73 | + const mockReleases = jest.spyOn(gh.rest.repos, 'listReleases') |
| 74 | + mockReleases.mockResolvedValue(mockResponse) |
| 75 | + |
| 76 | + const [releases, latestRelease] = await getRelease(gh) |
| 77 | + |
| 78 | + expect(releases).toHaveLength(3) |
| 79 | + expect(latestRelease).toBe('v1.0.1') |
| 80 | + }) |
| 81 | + |
| 82 | + it('should return the latest non-draft release', async () => { |
| 83 | + const mockResponse: any = { |
| 84 | + headers: {}, |
| 85 | + status: 200, |
| 86 | + data: [ |
| 87 | + { |
| 88 | + tag_name: 'v1.0.2', |
| 89 | + target_commitish: 'dev', |
| 90 | + draft: false, |
| 91 | + }, |
| 92 | + { |
| 93 | + tag_name: 'v1.0.1', |
| 94 | + target_commitish: 'main', |
| 95 | + draft: true, |
| 96 | + }, |
| 97 | + { |
| 98 | + tag_name: 'v1.0.0', |
| 99 | + target_commitish: 'main', |
| 100 | + draft: false, |
| 101 | + }, |
| 102 | + ], |
| 103 | + } |
| 104 | + |
| 105 | + const mockReleases = jest.spyOn(gh.rest.repos, 'listReleases') |
| 106 | + mockReleases.mockResolvedValue(mockResponse) |
| 107 | + |
| 108 | + const [releases, latestRelease] = await getRelease(gh) |
| 109 | + |
| 110 | + expect(releases).toHaveLength(3) |
| 111 | + expect(latestRelease).toBe('v1.0.0') |
| 112 | + }) |
| 113 | + |
| 114 | + it('should return v0.0.0 when no releases exist', async () => { |
| 115 | + const mockResponse: any = { |
| 116 | + headers: {}, |
| 117 | + status: 200, |
| 118 | + data: [], |
| 119 | + } |
| 120 | + |
| 121 | + const mockReleases = jest.spyOn(gh.rest.repos, 'listReleases') |
| 122 | + mockReleases.mockResolvedValue(mockResponse) |
| 123 | + |
| 124 | + const [releases, latestRelease] = await getRelease(gh) |
| 125 | + |
| 126 | + expect(releases).toHaveLength(0) |
| 127 | + expect(latestRelease).toBe('v0.0.0') |
| 128 | + }) |
| 129 | +}) |
| 130 | + |
| 131 | +describe('createOrUpdateRelease', () => { |
| 132 | + let mockResponse: any |
| 133 | + let mockNotes: any |
| 134 | + const inputs: Inputs = { |
| 135 | + githubToken: '_', |
| 136 | + majorLabel: 'major', |
| 137 | + minorLabel: 'minor', |
| 138 | + header: 'header', |
| 139 | + footer: 'footer', |
| 140 | + } |
| 141 | + beforeEach(() => { |
| 142 | + jest.clearAllMocks() |
| 143 | + gh = github.getOctokit('_') |
| 144 | + mockResponse = { |
| 145 | + headers: {}, |
| 146 | + status: 200, |
| 147 | + data: [ |
| 148 | + { |
| 149 | + id: 1, |
| 150 | + tag_name: 'v1.0.0', |
| 151 | + target_commitish: 'main', |
| 152 | + draft: false, |
| 153 | + body: 'header', |
| 154 | + }, |
| 155 | + { |
| 156 | + id: 2, |
| 157 | + tag_name: 'v1.0.1', |
| 158 | + target_commitish: 'main', |
| 159 | + draft: true, |
| 160 | + body: 'header', |
| 161 | + }, |
| 162 | + ], |
| 163 | + } |
| 164 | + |
| 165 | + mockNotes = { |
| 166 | + headers: {}, |
| 167 | + status: 200, |
| 168 | + data: { |
| 169 | + body: 'header', |
| 170 | + name: 'v1.0.1', |
| 171 | + }, |
| 172 | + } |
| 173 | + }) |
| 174 | + |
| 175 | + it('should create a new release draft', async () => { |
| 176 | + const mockInputCreate: any = { |
| 177 | + headers: {}, |
| 178 | + status: 200, |
| 179 | + data: [ |
| 180 | + { |
| 181 | + id: 1, |
| 182 | + tag_name: 'v1.0.0', |
| 183 | + target_commitish: 'main', |
| 184 | + draft: false, |
| 185 | + }, |
| 186 | + ], |
| 187 | + } |
| 188 | + |
| 189 | + const mockReleases = jest.spyOn(gh.rest.repos, 'createRelease') |
| 190 | + mockReleases.mockResolvedValue(mockResponse) |
| 191 | + |
| 192 | + const mockRelease = jest.spyOn(gh.rest.repos, 'listReleases') |
| 193 | + mockRelease.mockResolvedValue(mockInputCreate) |
| 194 | + |
| 195 | + const mockReleaseNotes = jest.spyOn(gh.rest.repos, 'generateReleaseNotes') |
| 196 | + mockReleaseNotes.mockResolvedValue(mockNotes) |
| 197 | + |
| 198 | + const response = await createOrUpdateRelease(gh, inputs, mockInputCreate.data, 'v1.0.0', 'v1.0.1') |
| 199 | + |
| 200 | + expect(mockReleases).toHaveBeenCalledTimes(1) |
| 201 | + }) |
| 202 | + |
| 203 | + it('should update an existing release draft', async () => { |
| 204 | + const mockInputUpdate: any = { |
| 205 | + headers: {}, |
| 206 | + status: 200, |
| 207 | + data: [ |
| 208 | + { |
| 209 | + id: 1, |
| 210 | + tag_name: 'v1.0.0', |
| 211 | + target_commitish: 'main', |
| 212 | + draft: false, |
| 213 | + }, |
| 214 | + { |
| 215 | + id: 2, |
| 216 | + tag_name: 'v1.0.1', |
| 217 | + target_commitish: 'main', |
| 218 | + draft: true, |
| 219 | + }, |
| 220 | + ], |
| 221 | + } |
| 222 | + |
| 223 | + const mockReleases = jest.spyOn(gh.rest.repos, 'updateRelease') |
| 224 | + mockReleases.mockResolvedValue(mockResponse) |
| 225 | + |
| 226 | + const mockRelease = jest.spyOn(gh.rest.repos, 'listReleases') |
| 227 | + mockRelease.mockResolvedValue(mockInputUpdate) |
| 228 | + |
| 229 | + const mockReleaseNotes = jest.spyOn(gh.rest.repos, 'generateReleaseNotes') |
| 230 | + mockReleaseNotes.mockResolvedValue(mockNotes) |
| 231 | + |
| 232 | + const response = await createOrUpdateRelease(gh, inputs, mockInputUpdate.data, 'v1.0.0', 'v1.0.1') |
| 233 | + |
| 234 | + expect(mockReleases).toHaveBeenCalledTimes(1) |
| 235 | + }) |
| 236 | +}) |
0 commit comments