|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import { cleanup, getTempDirectory, writeFiles } from '@rock-js/test-helpers'; |
| 4 | +import * as tools from '@rock-js/tools'; |
| 5 | +import { updateAndroidBuildGradle } from "../initInExistingProject.js"; |
| 6 | + |
| 7 | +const directory = getTempDirectory('test_updateAndroidBuildGradle'); |
| 8 | + |
| 9 | +afterEach(() => { |
| 10 | + cleanup(directory); |
| 11 | +}); |
| 12 | + |
| 13 | +describe('updateAndroidBuildGradle', () => { |
| 14 | + const workingCases = [ |
| 15 | + { |
| 16 | + name: 'cliFile is commented out', |
| 17 | + content: ` |
| 18 | + react { |
| 19 | + // cliFile = file("../../node_modules/react-native/cli.js") |
| 20 | + } |
| 21 | + `, |
| 22 | + expected: ` |
| 23 | + react { |
| 24 | + cliFile = file("../../node_modules/rock/dist/src/bin.js") |
| 25 | + } |
| 26 | + `, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: 'cliFile is not commented out', |
| 30 | + content: ` |
| 31 | + reactNativeDir = file("../../node_modules/react-native") |
| 32 | +
|
| 33 | + react { |
| 34 | + cliFile = file("\${reactNativeDir}/cli.js") |
| 35 | + } |
| 36 | + `, |
| 37 | + expected: ` |
| 38 | + reactNativeDir = file("../../node_modules/react-native") |
| 39 | +
|
| 40 | + react { |
| 41 | + cliFile = file("../../node_modules/rock/dist/src/bin.js") |
| 42 | + } |
| 43 | + `, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'cliFile is using a variable', |
| 47 | + content: ` |
| 48 | + reactNativeDir = file("../../node_modules/react-native") |
| 49 | +
|
| 50 | + react { |
| 51 | + cliFile = file("\${reactNativeDir}/cli.js") |
| 52 | + } |
| 53 | + `, |
| 54 | + expected: ` |
| 55 | + reactNativeDir = file("../../node_modules/react-native") |
| 56 | +
|
| 57 | + react { |
| 58 | + cliFile = file("../../node_modules/rock/dist/src/bin.js") |
| 59 | + } |
| 60 | + `, |
| 61 | + }, |
| 62 | + ]; |
| 63 | + |
| 64 | + it.each(workingCases)('should update the Android build.gradle file when $name', ({ content, expected }) => { |
| 65 | + const files = { |
| 66 | + 'android/app/build.gradle': content, |
| 67 | + }; |
| 68 | + writeFiles(directory, files); |
| 69 | + updateAndroidBuildGradle(directory, 'android'); |
| 70 | + |
| 71 | + expect(fs.readFileSync(path.join(directory, 'android/app/build.gradle'), 'utf8')).toStrictEqual(expected) |
| 72 | + }); |
| 73 | + |
| 74 | + it('should not update the Android build.gradle file when cliFile is already set', () => { |
| 75 | + const content = ` |
| 76 | + react { |
| 77 | + cliFile = file("../../node_modules/rock/dist/src/bin.js") |
| 78 | + } |
| 79 | + `; |
| 80 | + const files = { |
| 81 | + 'android/app/build.gradle': content, |
| 82 | + }; |
| 83 | + |
| 84 | + writeFiles(directory, files); |
| 85 | + updateAndroidBuildGradle(directory, 'android'); |
| 86 | + |
| 87 | + expect(fs.readFileSync(path.join(directory, 'android/app/build.gradle'), 'utf8')).toStrictEqual(content) |
| 88 | + }); |
| 89 | + |
| 90 | + it('should display a warning when unable to update the Android build.gradle file', () => { |
| 91 | + const warn = vi.spyOn(tools.logger, 'warn'); |
| 92 | + |
| 93 | + const content = ` |
| 94 | + react {} |
| 95 | + `; |
| 96 | + |
| 97 | + const files = { |
| 98 | + 'android/app/build.gradle': content, |
| 99 | + }; |
| 100 | + |
| 101 | + writeFiles(directory, files); |
| 102 | + updateAndroidBuildGradle(directory, 'android'); |
| 103 | + |
| 104 | + expect(fs.readFileSync(path.join(directory, 'android/app/build.gradle'), 'utf8')).toStrictEqual(content) |
| 105 | + |
| 106 | + expect(warn).toHaveBeenCalledWith( |
| 107 | + expect.stringContaining('Unable to update') |
| 108 | + ) |
| 109 | + }); |
| 110 | +}); |
0 commit comments