|
| 1 | +require('../../../mocks/mockAll'); |
| 2 | +const mockRunTask = jest.spyOn(require('../../../../utils/runTask'), 'runTask'); |
| 3 | + |
| 4 | +import path from 'path'; |
| 5 | +import { getProjectPath } from '../../../../utils/getProjectPath'; |
| 6 | +import { runUpgradeTasks } from '../../../../utils/upgrade/runUpgradeTasks'; |
| 7 | +import { mockFs } from '../../../mocks/mockFs'; |
| 8 | + |
| 9 | +describe('runUpgradeTasks', () => { |
| 10 | + afterEach(() => { |
| 11 | + mockRunTask.mockReset(); |
| 12 | + }); |
| 13 | + it('should execute upgrade.yml tasks', async () => { |
| 14 | + mockRunTask.mockResolvedValueOnce(undefined); |
| 15 | + mockFs.writeFileSync( |
| 16 | + path.join(getProjectPath(), '.upgrade/upgrade.yml'), |
| 17 | + ` |
| 18 | +env: |
| 19 | + value: true |
| 20 | +tasks: |
| 21 | + - type: app_delegate |
| 22 | + label: AppDelegate.mm modification |
| 23 | + actions: |
| 24 | + - append: test` |
| 25 | + ); |
| 26 | + |
| 27 | + const result = await runUpgradeTasks(); |
| 28 | + |
| 29 | + expect(result.didRun).toBeTruthy(); |
| 30 | + expect(mockRunTask).toHaveBeenCalledWith({ |
| 31 | + configPath: path.join(getProjectPath(), '.upgrade/upgrade.yml'), |
| 32 | + packageName: 'upgrade.yml', |
| 33 | + task: { |
| 34 | + type: 'app_delegate', |
| 35 | + label: 'AppDelegate.mm modification', |
| 36 | + actions: [{ append: 'test' }], |
| 37 | + }, |
| 38 | + }); |
| 39 | + }); |
| 40 | + it('should not execute when tasks does not meet condition', async () => { |
| 41 | + mockRunTask.mockResolvedValueOnce(undefined); |
| 42 | + mockFs.writeFileSync( |
| 43 | + path.join(getProjectPath(), '.upgrade/upgrade.yml'), |
| 44 | + ` |
| 45 | +env: |
| 46 | + value: true |
| 47 | +tasks: |
| 48 | + - type: app_delegate |
| 49 | + when: |
| 50 | + value: false |
| 51 | + actions: |
| 52 | + - append: test` |
| 53 | + ); |
| 54 | + |
| 55 | + const result = await runUpgradeTasks(); |
| 56 | + |
| 57 | + expect(result.didRun).toBeTruthy(); |
| 58 | + expect(mockRunTask).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + it('should handle not finding upgrade.yml', async () => { |
| 61 | + const result = await runUpgradeTasks(); |
| 62 | + |
| 63 | + expect(result.didRun).toBeFalsy(); |
| 64 | + }); |
| 65 | + it('should handle invalid upgrade.yml', async () => { |
| 66 | + mockFs.writeFileSync( |
| 67 | + path.join(getProjectPath(), '.upgrade/upgrade.yml'), |
| 68 | + 'random' |
| 69 | + ); |
| 70 | + const result = await runUpgradeTasks(); |
| 71 | + |
| 72 | + expect(result.didRun).toBeFalsy(); |
| 73 | + }); |
| 74 | + it('should handle failed tasks', async () => { |
| 75 | + mockRunTask.mockRejectedValueOnce(new Error('random')); |
| 76 | + mockFs.writeFileSync( |
| 77 | + path.join(getProjectPath(), '.upgrade/upgrade.yml'), |
| 78 | + ` |
| 79 | +tasks: |
| 80 | + - type: app_delegate |
| 81 | + actions: |
| 82 | + - append: test` |
| 83 | + ); |
| 84 | + |
| 85 | + const result = await runUpgradeTasks(); |
| 86 | + |
| 87 | + expect(result.didRun).toBeTruthy(); |
| 88 | + if (result.didRun) { |
| 89 | + expect(result.failedTaskCount).toBe(1); |
| 90 | + } |
| 91 | + }); |
| 92 | +}); |
0 commit comments