UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

394 lines (393 loc) 11.8 kB
"use strict"; /* eslint-disable @typescript-eslint/no-unsafe-call */ Object.defineProperty(exports, "__esModule", { value: true }); const { mockFs } = require('../../mocks/mockAll'); const mockObjectEntries = jest.spyOn(Object, 'entries'); const plistTask_1 = require("../../../tasks/plistTask"); const mockAll_1 = require("../../mocks/mockAll"); describe('pListTask', () => { it('should set value', () => { // noinspection SpellCheckingInspection let content = { CFBundleName: 'test', CFBundlePackageType: 'APPL', CFBundleShortVersionString: '1.2.3', CFBundleSignature: 'test', CFBundleVersion: '1.2.3', }; const task = { task: 'plist', actions: [ { set: { CFBundleName: 'test2', }, }, ], }; content = (0, plistTask_1.plistTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content.CFBundleName).toEqual('test2'); }); it('should assign value', () => { let content = { first: { second: { third: 'test', }, }, }; const task = { task: 'plist', actions: [ { set: { first: { assigned: 'test2', }, }, strategy: 'assign', }, ], }; content = (0, plistTask_1.plistTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content.first).toEqual({ assigned: 'test2', }); }); it('should merge value', () => { let content = { first: { second: { third: 'test', }, other: ['test'], }, }; const task = { task: 'plist', actions: [ { set: { first: { assigned: 'test2', other: ['test2'], }, }, strategy: 'merge', }, ], }; content = (0, plistTask_1.plistTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content.first).toEqual({ second: { third: 'test', }, assigned: 'test2', other: ['test2'], }); }); it('should merge and concat value', () => { let content = { first: { second: { third: 'test', }, other: ['test'], }, }; const task = { task: 'plist', actions: [ { set: { first: { assigned: 'test2', other: ['test2'], }, }, strategy: 'merge_concat', }, ], }; content = (0, plistTask_1.plistTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content.first).toEqual({ second: { third: 'test', }, assigned: 'test2', other: ['test', 'test2'], }); }); it('should assign inner value', () => { let content = { first: { second: { third: 'test', }, }, }; const task = { task: 'plist', actions: [ { set: { first: { second: { assigned: 'test2', other: ['test2'], $assign: true, }, }, }, strategy: 'merge_concat', }, ], }; content = (0, plistTask_1.plistTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content.first.second).toEqual({ assigned: 'test2', other: ['test2'], }); }); it('should set index value', () => { let content = { first: { second: [{ third: 'test' }, { forth: 'test2' }], }, }; const task = { task: 'plist', actions: [ { set: { first: { second: { $index: 0, assigned: 'test2', }, }, }, strategy: 'merge_concat', }, ], }; content = (0, plistTask_1.plistTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content.first.second).toEqual([ { third: 'test', assigned: 'test2' }, { forth: 'test2' }, ]); }); it('should delete value', () => { let content = { first: { second: { third: 'test', }, }, }; const task = { task: 'plist', actions: [ { set: { first: { second: { $delete: true, }, }, }, strategy: 'merge', }, ], }; content = (0, plistTask_1.plistTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content.first).toEqual({}); }); it('should skip if condition not met', () => { let content = { first: { second: [{ third: 'test' }, { forth: 'test2' }], }, }; const task = { task: 'plist', actions: [ { when: { test: 'random' }, set: { first: { second: { $index: 0, assigned: 'test2', }, }, }, strategy: 'merge_concat', }, ], }; content = (0, plistTask_1.plistTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', }); expect(content.first.second).not.toEqual([ { third: 'test', assigned: 'test2' }, { forth: 'test2' }, ]); }); it('should throw on random error', () => { mockObjectEntries.mockImplementationOnce(() => { throw new Error('some random error'); }); const content = { first: { second: [{ third: 'test' }, { forth: 'test2' }], }, }; const task = { task: 'plist', actions: [ { set: { first: { second: { $index: 0, assigned: 'test2', }, }, }, strategy: 'merge_concat', }, ], }; expect(() => (0, plistTask_1.plistTask)({ configPath: 'path/to/config', task: task, content, packageName: 'test-package', })).toThrowError('random error'); jest.unmock('lodash.mergewith'); }); describe('runTask', () => { it('should read and write plist file', () => { const pListPath = (0, mockAll_1.writeMockPList)(); const task = { task: 'plist', actions: [ { set: { CFBundleDisplayName: 'test2', }, }, ], }; (0, plistTask_1.runTask)({ configPath: 'path/to/config', task: task, packageName: 'test-package', }); const content = mockFs.readFileSync(pListPath); expect(content).toContain('test2'); }); it('should read and write plist file of custom target', () => { const pListPath = (0, mockAll_1.writeMockPList)('custom'); const task = { task: 'plist', target: 'custom', actions: [ { set: { CFBundleDisplayName: 'test2', }, }, ], }; (0, plistTask_1.runTask)({ configPath: 'path/to/config', task: task, packageName: 'test-package', }); const content = mockFs.readFileSync(pListPath); expect(content).toContain('test2'); }); it('should throw when plist does not exist', () => { const task = { task: 'plist', actions: [ { set: { CFBundleDisplayName: 'test2', }, }, ], }; expect(() => { (0, plistTask_1.runTask)({ configPath: 'path/to/config', task: task, packageName: 'test-package', }); }).toThrowError('Plist file not found'); }); it('should throw when project does not exist', () => { const mock = jest.spyOn(mockFs, 'readdirSync').mockImplementation(() => { throw new Error('Directory not found'); }); const task = { task: 'plist', actions: [ { set: { CFBundleDisplayName: 'test2', }, }, ], }; expect(() => { (0, plistTask_1.runTask)({ configPath: 'path/to/config', task: task, packageName: 'test-package', }); }).toThrowError('project not found'); mock.mockRestore(); }); }); });