UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

470 lines (469 loc) 13.6 kB
"use strict"; /* eslint-disable @typescript-eslint/no-unsafe-call */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const { mockFs } = require('../../mocks/mockAll'); const path_1 = __importDefault(require("path")); const constants_1 = require("../../../constants"); const androidManifestTask_1 = require("../../../tasks/androidManifestTask"); const mockAll_1 = require("../../mocks/mockAll"); const mockAndroidManifestTemplate_1 = require("../../mocks/mockAndroidManifestTemplate"); describe('androidManifestTask', () => { it('should throw if setting attributes with empty block', async () => { const content = ''; const task = { task: 'android_manifest', actions: [ { attributes: { test: 1, }, }, ], }; await expect((0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', })).rejects.toThrowError('you must set block'); }); it('should skip if condition not met', async () => { const content = ''; const task = { task: 'android_manifest', actions: [ { when: { test: 'random' }, attributes: { test: 1, }, }, ], }; await expect((0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', })).resolves.not.toThrow(); }); it('should throw if block is invalid', async () => { const content = ''; const task = { task: 'android_manifest', actions: [ { block: 'random', }, ], }; await expect((0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', })).rejects.toThrowError('Invalid block'); }); it('should throw text into empty body ', async () => { const content = ''; const task = { task: 'android_manifest', actions: [ { block: 'activity', append: '<test />', }, ], }; await expect((0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', })).rejects.toThrowError('block could not be found'); }); it('should prepend text into empty body without block', async () => { let content = ''; const task = { task: 'android_manifest', actions: [ { prepend: '<test />', append: '<test />', }, ], }; content = await (0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); content = await (0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toEqual(` <test /> `); }); it('should skip insert when ifNotPresent exists', async () => { const content = mockAndroidManifestTemplate_1.mockAndroidManifestTemplate; const task = { task: 'android_manifest', actions: [ { block: 'activity', ifNotPresent: 'intent-filter', append: '<test />', }, { block: 'activity', ifNotPresent: 'intent-filter', append: '<test />', }, ], }; await (0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(mockAll_1.mockPrompter.log.message).toHaveBeenCalledWith(expect.stringContaining('found existing ')); }); it('should prepend text into existing body ', async () => { let content = ` <manifest> <test3 /> <application> <activity> <test1 /> </activity> </application> </manifest> `; const task = { task: 'android_manifest', actions: [ { block: 'application', prepend: '<test />', }, ], }; content = await (0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task, content, packageName: 'test-package', }); expect(content).toEqual(` <manifest> <test3 /> <application> <test /> <activity> <test1 /> </activity> </application> </manifest> `); }); it('should set, replace and delete attributes', async () => { let content = ` <manifest> <test3 /> <application test2="not ok" test3="replace me"> <activity> <test1 /> </activity> </application> </manifest> `; const task = { task: 'android_manifest', actions: [ { block: 'application', attributes: { test2: { $delete: true }, nonExisting: { $delete: true }, test3: 'OK', test: 'OK', }, }, ], }; content = await (0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task, content, packageName: 'test-package', }); expect(content).toEqual(` <manifest> <test3 /> <application test3="OK" test="OK"> <activity> <test1 /> </activity> </application> </manifest> `); }); it('should append text into existing body ', async () => { let content = ` <manifest> <test3 /> <application> <activity> <test1 /> </activity> </application> </manifest> `; const task = { task: 'android_manifest', actions: [ { block: 'manifest', append: '<test />', }, ], }; content = await (0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task, content, packageName: 'test-package', }); expect(content).toEqual(` <manifest> <test3 /> <application> <activity> <test1 /> </activity> </application> <test /> </manifest> `); }); it('should insert text after point with comment', async () => { let content = ` <manifest> <test3 /> <application> <activity> <test1 /> </activity> </application> </manifest> `; const task = { task: 'android_manifest', actions: [ { block: 'manifest', after: 'test3', prepend: '<test />', comment: 'test comment', }, ], }; content = await (0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toContain(` <manifest> <test3 /> <!-- test comment --> <test /> <application> <activity> <test1 /> </activity> </application> </manifest> `); }); it('should insert text before point', async () => { let content = ` <manifest> <test3 /> <application> <activity> <test1 /> </activity> </application> </manifest> `; const task = { task: 'android_manifest', actions: [ { block: 'manifest', before: { regex: 'test3' }, append: '<test />', }, ], }; content = await (0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content).toContain(` <manifest> <test /> <test3 /> <application> <activity> <test1 /> </activity> </application> </manifest> `); }); it('should throw when insertion point not found with strict', async () => { const content = ` <manifest> <test3 /> <application> <activity> <test1 /> </activity> </application> </manifest> `; const taskInsertBefore = { task: 'android_manifest', actions: [ { block: 'manifest', before: 'random', append: '<test />', strict: true, }, ], }; const taskInsertBeforeNonStrict = { task: 'android_manifest', actions: [ { block: 'manifest', before: 'random', append: '<test />', }, ], }; await expect((0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: taskInsertBefore, content, packageName: 'test-package', })).rejects.toThrowError('insertion point'); await expect((0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: taskInsertBeforeNonStrict, content, packageName: 'test-package', })).resolves.not.toThrowError('insertion point'); const taskInsertAfter = { task: 'android_manifest', actions: [ { block: 'manifest', after: 'random', prepend: '<test2 />', strict: true, }, ], }; const taskInsertAfterNonStrict = { task: 'android_manifest', actions: [ { block: 'manifest', after: 'random', prepend: '<test2s />', }, ], }; await expect((0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: taskInsertAfter, content, packageName: 'test-package', })).rejects.toThrowError('insertion point'); await expect((0, androidManifestTask_1.androidManifestTask)({ configPath: 'path/to/config', task: taskInsertAfterNonStrict, content, packageName: 'test-package', })).resolves.not.toThrowError('insertion point'); }); describe('runTask', () => { it('should read and write android manifest file', async () => { let content = ` <manifest> <test3 /> <application> <activity> <test1 /> </activity> </application> </manifest> `; const manifestPath = path_1.default.resolve(__dirname, `../../mock-project/${constants_1.Constants.ANDROID_MAIN_FILE_PATH}/${constants_1.Constants.ANDROID_MANIFEST_FILE_NAME}`); mockFs.writeFileSync(manifestPath, content); const task = { task: 'android_manifest', actions: [ { block: 'manifest', prepend: '<test />', }, ], }; await (0, androidManifestTask_1.runTask)({ configPath: 'path/to/config', task, packageName: 'test-package', }); content = mockFs.readFileSync(manifestPath); // @ts-ignore expect(content).toContain(task.actions[0].prepend); }); it('should throw when android manifest does not exist', async () => { const task = { task: 'android_manifest', actions: [ { block: 'manifest', prepend: 'test2;', }, ], }; await expect((0, androidManifestTask_1.runTask)({ configPath: 'path/to/config', task: task, packageName: 'test-package', })).rejects.toThrowError('AndroidManifest.xml file not found'); }); }); });