|
| 1 | +/* |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with this work for additional information |
| 5 | + regarding copyright ownership. The ASF licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, |
| 13 | + software distributed under the License is distributed on an |
| 14 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + KIND, either express or implied. See the License for the |
| 16 | + specific language governing permissions and limitations |
| 17 | + under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +const path = require('node:path'); |
| 21 | +const { CordovaError, events } = require('cordova-common'); |
| 22 | +const build = require('../../../lib/build'); |
| 23 | +const run = require('../../../lib/run'); |
| 24 | + |
| 25 | +describe('cordova/lib/run', () => { |
| 26 | + const testProjectPath = path.join('/test', 'project', 'path'); |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + run.root = testProjectPath; |
| 30 | + }); |
| 31 | + |
| 32 | + describe('runListDevices method', () => { |
| 33 | + beforeEach(() => { |
| 34 | + spyOn(events, 'emit'); |
| 35 | + spyOn(run, 'execListDevices').and.returnValue(Promise.resolve(["iPhone Xs"])); |
| 36 | + spyOn(run, 'execListEmulatorTargets').and.returnValue(Promise.resolve(["iPhone 15 Simulator"])); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should delegate to "listDevices" when the "runListDevices" method options param contains "options.device".', () => { |
| 40 | + return run.runListDevices({ options: { device: true } }).then(() => { |
| 41 | + expect(run.execListDevices).toHaveBeenCalled(); |
| 42 | + expect(run.execListEmulatorTargets).not.toHaveBeenCalled(); |
| 43 | + |
| 44 | + expect(events.emit).toHaveBeenCalledWith('log', '\tiPhone Xs'); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should delegate to "listDevices" when the "runListDevices" method options param contains "options.emulator".', () => { |
| 49 | + return run.runListDevices({ options: { emulator: true } }).then(() => { |
| 50 | + expect(run.execListDevices).not.toHaveBeenCalled(); |
| 51 | + expect(run.execListEmulatorTargets).toHaveBeenCalled(); |
| 52 | + |
| 53 | + expect(events.emit).toHaveBeenCalledWith('log', '\tiPhone 15 Simulator'); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should delegate to both "listEmulators" and "listDevices" when the "runListDevices" method does not contain "options.device" or "options.emulator".', () => { |
| 58 | + return run.runListDevices().then(() => { |
| 59 | + expect(run.execListDevices).toHaveBeenCalled(); |
| 60 | + expect(run.execListEmulatorTargets).toHaveBeenCalled(); |
| 61 | + |
| 62 | + expect(events.emit).toHaveBeenCalledWith('log', '\tiPhone Xs'); |
| 63 | + expect(events.emit).toHaveBeenCalledWith('log', '\tiPhone 15 Simulator'); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('run method', () => { |
| 69 | + beforeEach(() => { |
| 70 | + spyOn(build, 'run').and.returnValue(Promise.resolve()); |
| 71 | + spyOn(build, 'findXCodeProjectIn').and.returnValue('ProjectName'); |
| 72 | + spyOn(run, 'execListDevices').and.resolveTo([]); |
| 73 | + spyOn(run, 'execListEmulatorTargets').and.resolveTo([]); |
| 74 | + spyOn(run, 'listDevices').and.resolveTo(); |
| 75 | + spyOn(run, 'deployToMac').and.resolveTo(); |
| 76 | + spyOn(run, 'deployToSim').and.resolveTo(); |
| 77 | + spyOn(run, 'checkDeviceConnected').and.rejectWith(new Error('No Device Connected')); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('--list option', () => { |
| 81 | + beforeEach(() => { |
| 82 | + spyOn(run, 'listEmulators').and.returnValue(Promise.resolve()); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should delegate to listDevices method if `options.device` specified', () => { |
| 86 | + return run.run({ list: true, device: true }).then(() => { |
| 87 | + expect(run.listDevices).toHaveBeenCalled(); |
| 88 | + expect(run.listEmulators).not.toHaveBeenCalled(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should delegate to listEmulators method if `options.device` specified', () => { |
| 93 | + return run.run({ list: true, emulator: true }).then(() => { |
| 94 | + expect(run.listDevices).not.toHaveBeenCalled(); |
| 95 | + expect(run.listEmulators).toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should delegate to both listEmulators and listDevices methods if neither `options.device` nor `options.emulator` are specified', () => { |
| 100 | + return run.run({ list: true }).then(() => { |
| 101 | + expect(run.listDevices).toHaveBeenCalled(); |
| 102 | + expect(run.listEmulators).toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should not accept device and emulator options together', () => { |
| 108 | + spyOn(Promise, 'reject'); |
| 109 | + |
| 110 | + run.run({ |
| 111 | + device: true, |
| 112 | + emulator: true |
| 113 | + }); |
| 114 | + |
| 115 | + expect(Promise.reject).toHaveBeenCalledWith(new CordovaError('Only one of "device"/"emulator" options should be specified')); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should run on a simulator if --device is not specified and no device is connected', () => { |
| 119 | + return run.run({ }).then(() => { |
| 120 | + expect(run.deployToSim).toHaveBeenCalled(); |
| 121 | + expect(build.run).toHaveBeenCalled(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should try to run on a device if --device is not specified and a device is connected', () => { |
| 126 | + run.execListDevices.and.resolveTo(["iPhone 12 Plus"]); |
| 127 | + |
| 128 | + return run.run({ }).then(() => { |
| 129 | + expect(run.checkDeviceConnected).toHaveBeenCalled(); |
| 130 | + expect(build.run).toHaveBeenCalledWith(jasmine.objectContaining({ device: true })); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should try to run on a device if --device is specified', () => { |
| 135 | + return run.run({ device: true }).then(() => { |
| 136 | + expect(run.checkDeviceConnected).toHaveBeenCalled(); |
| 137 | + expect(build.run).toHaveBeenCalledWith(jasmine.objectContaining({ device: true })); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should not run a build if --noBuild is passed', () => { |
| 142 | + return run.run({ emulator: true, nobuild: true }).then(() => { |
| 143 | + expect(build.run).not.toHaveBeenCalled(); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should try to launch the macOS Catalyst app bundle', () => { |
| 148 | + return run.run({ device: true, target: 'mac', release: true }).then(() => { |
| 149 | + expect(run.deployToMac).toHaveBeenCalledWith(path.join(testProjectPath, 'build', 'Release-maccatalyst', 'ProjectName.app')); |
| 150 | + }); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments