|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + */ |
| 9 | + |
| 10 | +const { |
| 11 | + generateChangelog, |
| 12 | + _computePreviousVersionFrom, |
| 13 | + _generateChangelog, |
| 14 | + _pushCommit, |
| 15 | + _createPR, |
| 16 | +} = require('../generateChangelog'); |
| 17 | + |
| 18 | +const silence = () => {}; |
| 19 | +const mockGetNpmPackageInfo = jest.fn(); |
| 20 | +const mockExecSync = jest.fn(); |
| 21 | +const mockRun = jest.fn(); |
| 22 | +const mockFetch = jest.fn(); |
| 23 | +const mockExit = jest.fn(); |
| 24 | + |
| 25 | +jest.mock('../utils.js', () => ({ |
| 26 | + log: silence, |
| 27 | + run: mockRun, |
| 28 | + getNpmPackageInfo: mockGetNpmPackageInfo, |
| 29 | +})); |
| 30 | + |
| 31 | +process.exit = mockExit; |
| 32 | +global.fetch = mockFetch; |
| 33 | + |
| 34 | +describe('Generate Changelog', () => { |
| 35 | + beforeEach(jest.clearAllMocks); |
| 36 | + |
| 37 | + describe('_computePreviousVersionFrom', () => { |
| 38 | + it('returns rc.0 when rc is 1', async () => { |
| 39 | + const currentVersion = '0.78.0-rc.1'; |
| 40 | + const expectedVersion = '0.78.0-rc.0'; |
| 41 | + |
| 42 | + const receivedVersion = await _computePreviousVersionFrom(currentVersion); |
| 43 | + |
| 44 | + expect(receivedVersion).toEqual(expectedVersion); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns previous rc version when rc is > 1', async () => { |
| 48 | + const currentVersion = '0.78.0-rc.5'; |
| 49 | + const expectedVersion = '0.78.0-rc.4'; |
| 50 | + |
| 51 | + const receivedVersion = await _computePreviousVersionFrom(currentVersion); |
| 52 | + |
| 53 | + expect(receivedVersion).toEqual(expectedVersion); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns previous patch version when rc is 0', async () => { |
| 57 | + const currentVersion = '0.78.0-rc.0'; |
| 58 | + const expectedVersion = '0.77.1'; |
| 59 | + |
| 60 | + mockGetNpmPackageInfo.mockReturnValueOnce( |
| 61 | + Promise.resolve({version: '0.77.1'}), |
| 62 | + ); |
| 63 | + |
| 64 | + const receivedVersion = await _computePreviousVersionFrom(currentVersion); |
| 65 | + |
| 66 | + expect(receivedVersion).toEqual(expectedVersion); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns patch 0 when patch is 1', async () => { |
| 70 | + const currentVersion = '0.78.1'; |
| 71 | + const expectedVersion = '0.78.0'; |
| 72 | + |
| 73 | + const receivedVersion = await _computePreviousVersionFrom(currentVersion); |
| 74 | + |
| 75 | + expect(receivedVersion).toEqual(expectedVersion); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns previous patch when patch is > 1', async () => { |
| 79 | + const currentVersion = '0.78.5'; |
| 80 | + const expectedVersion = '0.78.4'; |
| 81 | + |
| 82 | + const receivedVersion = await _computePreviousVersionFrom(currentVersion); |
| 83 | + |
| 84 | + expect(receivedVersion).toEqual(expectedVersion); |
| 85 | + }); |
| 86 | + |
| 87 | + it('returns null when patch is 0', async () => { |
| 88 | + const currentVersion = '0.78.0'; |
| 89 | + |
| 90 | + const receivedVersion = await _computePreviousVersionFrom(currentVersion); |
| 91 | + |
| 92 | + expect(receivedVersion).toBeNull(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("throws an error when the version can't be parsed", async () => { |
| 96 | + const currentVersion = '0.78.0-rc0'; |
| 97 | + |
| 98 | + await expect( |
| 99 | + _computePreviousVersionFrom(currentVersion), |
| 100 | + ).rejects.toThrow(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('_generateChangelog', () => { |
| 105 | + it('calls git in the right order', async () => { |
| 106 | + const currentVersion = '0.79.0-rc5'; |
| 107 | + const previousVersion = '0.79.0-rc4'; |
| 108 | + const token = 'token'; |
| 109 | + |
| 110 | + expectedCommandArgs = [ |
| 111 | + '@rnx-kit/rn-changelog-generator', |
| 112 | + '--base', |
| 113 | + `v${previousVersion}`, |
| 114 | + '--compare', |
| 115 | + `v${currentVersion}`, |
| 116 | + '--repo', |
| 117 | + '.', |
| 118 | + '--changelog', |
| 119 | + './CHANGELOG.md', |
| 120 | + '--token', |
| 121 | + `${token}`, |
| 122 | + ]; |
| 123 | + |
| 124 | + _generateChangelog(previousVersion, currentVersion, token); |
| 125 | + |
| 126 | + expect(mockRun).toHaveBeenCalledTimes(4); |
| 127 | + expect(mockRun).toHaveBeenNthCalledWith(1, 'git checkout main'); |
| 128 | + expect(mockRun).toHaveBeenNthCalledWith(2, 'git fetch'); |
| 129 | + expect(mockRun).toHaveBeenNthCalledWith(3, 'git pull origin main'); |
| 130 | + expect(mockRun).toHaveBeenNthCalledWith( |
| 131 | + 4, |
| 132 | + `npx ${expectedCommandArgs.join(' ')}`, |
| 133 | + ); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('_pushCommit', () => { |
| 138 | + it('calls git in the right order', async () => { |
| 139 | + const currentVersion = '0.79.0-rc5'; |
| 140 | + |
| 141 | + _pushCommit(currentVersion); |
| 142 | + |
| 143 | + expect(mockRun).toHaveBeenCalledTimes(4); |
| 144 | + expect(mockRun).toHaveBeenNthCalledWith( |
| 145 | + 1, |
| 146 | + `git checkout -b changelog/v${currentVersion}`, |
| 147 | + ); |
| 148 | + expect(mockRun).toHaveBeenNthCalledWith(2, 'git add CHANGELOG.md'); |
| 149 | + expect(mockRun).toHaveBeenNthCalledWith( |
| 150 | + 3, |
| 151 | + `git commit -m "[RN][Changelog] Add changelog for v${currentVersion}"`, |
| 152 | + ); |
| 153 | + expect(mockRun).toHaveBeenNthCalledWith( |
| 154 | + 4, |
| 155 | + `git push origin changelog/v${currentVersion}`, |
| 156 | + ); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + describe('_createPR', () => { |
| 161 | + it('throws error when status is not 201', async () => { |
| 162 | + const currentVersion = '0.79.0-rc5'; |
| 163 | + const token = 'token'; |
| 164 | + |
| 165 | + mockFetch.mockReturnValueOnce(Promise.resolve({status: 401})); |
| 166 | + |
| 167 | + const headers = { |
| 168 | + Accept: 'Accept: application/vnd.github+json', |
| 169 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 170 | + Authorization: `Bearer ${token}`, |
| 171 | + }; |
| 172 | + |
| 173 | + const content = ` |
| 174 | +## Summary |
| 175 | +Add Changelog for ${currentVersion} |
| 176 | +
|
| 177 | +## Changelog: |
| 178 | +[Internal] - Add Changelog for ${currentVersion} |
| 179 | +
|
| 180 | +## Test Plan: |
| 181 | +N/A`; |
| 182 | + |
| 183 | + const body = { |
| 184 | + title: `[RN][Changelog] Add changelog for v${currentVersion}`, |
| 185 | + head: `changelog/v${currentVersion}`, |
| 186 | + base: 'main', |
| 187 | + body: content, |
| 188 | + }; |
| 189 | + |
| 190 | + await expect(_createPR(currentVersion, token)).rejects.toThrow(); |
| 191 | + |
| 192 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 193 | + expect(mockFetch).toHaveBeenCalledWith( |
| 194 | + 'https://api.github.com/repos/facebook/react-native/pulls', |
| 195 | + { |
| 196 | + method: 'POST', |
| 197 | + headers: headers, |
| 198 | + body: JSON.stringify(body), |
| 199 | + }, |
| 200 | + ); |
| 201 | + }); |
| 202 | + it('Returns the pr url', async () => { |
| 203 | + const currentVersion = '0.79.0-rc5'; |
| 204 | + const token = 'token'; |
| 205 | + const expectedPrURL = |
| 206 | + 'https://github.com/facebook/react-native/pulls/1234'; |
| 207 | + |
| 208 | + const returnedObject = { |
| 209 | + status: 201, |
| 210 | + json: () => Promise.resolve({html_url: expectedPrURL}), |
| 211 | + }; |
| 212 | + mockFetch.mockReturnValueOnce(Promise.resolve(returnedObject)); |
| 213 | + |
| 214 | + const headers = { |
| 215 | + Accept: 'Accept: application/vnd.github+json', |
| 216 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 217 | + Authorization: `Bearer ${token}`, |
| 218 | + }; |
| 219 | + |
| 220 | + const content = ` |
| 221 | +## Summary |
| 222 | +Add Changelog for ${currentVersion} |
| 223 | +
|
| 224 | +## Changelog: |
| 225 | +[Internal] - Add Changelog for ${currentVersion} |
| 226 | +
|
| 227 | +## Test Plan: |
| 228 | +N/A`; |
| 229 | + |
| 230 | + const body = { |
| 231 | + title: `[RN][Changelog] Add changelog for v${currentVersion}`, |
| 232 | + head: `changelog/v${currentVersion}`, |
| 233 | + base: 'main', |
| 234 | + body: content, |
| 235 | + }; |
| 236 | + |
| 237 | + const receivedPrURL = await _createPR(currentVersion, token); |
| 238 | + |
| 239 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 240 | + expect(mockFetch).toHaveBeenCalledWith( |
| 241 | + 'https://api.github.com/repos/facebook/react-native/pulls', |
| 242 | + { |
| 243 | + method: 'POST', |
| 244 | + headers: headers, |
| 245 | + body: JSON.stringify(body), |
| 246 | + }, |
| 247 | + ); |
| 248 | + expect(receivedPrURL).toEqual(expectedPrURL); |
| 249 | + }); |
| 250 | + }); |
| 251 | +}); |
0 commit comments