Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
279 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,27 @@ | ||
const _ = require('lodash'); | ||
const DetoxConfigError = require('../errors/DetoxConfigError'); | ||
|
||
function composeDeviceConfig(options, cliConfig) { | ||
const { configurations, selectedConfiguration } = options; | ||
|
||
if (_.isEmpty(configurations)) { | ||
throw new Error(`There are no device configurations in the detox config`); | ||
} | ||
|
||
const configurationName = selectedConfiguration || cliConfig.configuration; | ||
const deviceOverride = cliConfig.deviceName; | ||
|
||
const deviceConfig = (!configurationName && _.size(configurations) === 1) | ||
? _.values(configurations)[0] | ||
: configurations[configurationName]; | ||
|
||
if (!deviceConfig) { | ||
throw new Error(`Cannot determine which configuration to use. use --configuration to choose one of the following: | ||
${Object.keys(configurations)}`); | ||
} | ||
|
||
if (!deviceConfig.type) { | ||
function composeDeviceConfig({ rawDeviceConfig, cliConfig }) { | ||
if (!rawDeviceConfig.type) { | ||
throwOnEmptyType(); | ||
} | ||
|
||
deviceConfig.device = deviceOverride || deviceConfig.device || deviceConfig.name; | ||
delete deviceConfig.name; | ||
rawDeviceConfig.device = cliConfig.deviceName || rawDeviceConfig.device || rawDeviceConfig.name; | ||
delete rawDeviceConfig.name; | ||
|
||
if (_.isEmpty(deviceConfig.device)) { | ||
if (_.isEmpty(rawDeviceConfig.device)) { | ||
throwOnEmptyDevice(); | ||
} | ||
|
||
return deviceConfig; | ||
} | ||
|
||
function throwOnEmptyDevice() { | ||
throw new DetoxConfigError(`'device' property is empty, should hold the device query to run on (e.g. { "type": "iPhone 11 Pro" }, { "avdName": "Nexus_5X_API_29" })`); | ||
return rawDeviceConfig; | ||
} | ||
|
||
function throwOnEmptyType() { | ||
throw new DetoxConfigError(`'type' property is missing, should hold the device type to test on (e.g. "ios.simulator" or "android.emulator")`); | ||
} | ||
|
||
function throwOnEmptyDevice() { | ||
throw new DetoxConfigError(`'device' property is empty, should hold the device query to run on (e.g. { "type": "iPhone 11 Pro" }, { "avdName": "Nexus_5X_API_29" })`); | ||
} | ||
|
||
module.exports = composeDeviceConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,70 @@ | ||
describe('composeDeviceConfig', () => { | ||
let composeDeviceConfig; | ||
let configs; | ||
let cliConfig; | ||
let composeDeviceConfig, cliConfig, rawDeviceConfig; | ||
|
||
beforeEach(() => { | ||
composeDeviceConfig = require('./composeDeviceConfig'); | ||
|
||
cliConfig = {}; | ||
configs = [1, 2].map(i => ({ | ||
type: `someDriver${i}`, | ||
device: `someDevice${i}`, | ||
})); | ||
rawDeviceConfig = { | ||
type: 'ios.simulator', | ||
device: { | ||
type: 'iPhone X' | ||
}, | ||
}; | ||
}); | ||
|
||
describe('validation', () => { | ||
it('should throw if no configurations are passed', () => { | ||
expect(() => composeDeviceConfig({ | ||
configurations: {}, | ||
}, cliConfig)).toThrowError(/There are no device configurations/); | ||
}); | ||
const compose = () => composeDeviceConfig({ cliConfig, rawDeviceConfig }); | ||
|
||
describe('validation', () => { | ||
it('should throw if configuration driver (type) is not defined', () => { | ||
expect(() => composeDeviceConfig({ | ||
configurations: { | ||
undefinedDriver: { | ||
device: { type: 'iPhone X' }, | ||
}, | ||
}, | ||
}, cliConfig)).toThrowError(/type.*missing.*ios.simulator.*android.emulator/); | ||
delete rawDeviceConfig.type; | ||
expect(compose).toThrowError(/type.*missing.*ios.simulator.*android.emulator/); | ||
}); | ||
|
||
it('should throw if device query is not defined', () => { | ||
expect(() => composeDeviceConfig({ | ||
configurations: { | ||
undefinedDeviceQuery: { | ||
type: 'ios.simulator', | ||
}, | ||
}, | ||
}, cliConfig)).toThrowError(/device.*empty.*device.*query.*type.*avdName/); | ||
delete rawDeviceConfig.device; | ||
expect(compose).toThrowError(/device.*empty.*device.*query.*type.*avdName/); | ||
}); | ||
}); | ||
|
||
describe('for no specified configuration name', () => { | ||
beforeEach(() => { delete cliConfig.configuration; }); | ||
|
||
describe('when there is a single config', () => { | ||
it('should return it', () => { | ||
const singleDeviceConfig = configs[0]; | ||
|
||
expect(composeDeviceConfig({ | ||
configurations: {singleDeviceConfig } | ||
}, cliConfig)).toBe(singleDeviceConfig); | ||
}); | ||
describe('if a device configuration has the old .name property', () => { | ||
beforeEach(() => { | ||
rawDeviceConfig.name = rawDeviceConfig.device; | ||
delete rawDeviceConfig.device; | ||
}); | ||
|
||
describe('when there is more than one config', () => { | ||
it('should throw if there is more than one config', () => { | ||
const [config1, config2] = configs; | ||
expect(() => composeDeviceConfig({ | ||
configurations: { config1, config2 }, | ||
}, cliConfig)).toThrowError(/Cannot determine/); | ||
}); | ||
|
||
describe('but also selectedConfiguration param is specified', () => { | ||
it('should select that configuration', () => { | ||
const [config1, config2] = configs; | ||
it('should rename it to .device', () => { | ||
const { type, device, name } = compose(); | ||
|
||
expect(composeDeviceConfig({ | ||
selectedConfiguration: 'config1', | ||
configurations: { config1, config2 }, | ||
}, cliConfig)).toEqual(config1); | ||
}); | ||
}); | ||
expect(type).toBe('ios.simulator'); | ||
expect(name).toBe(undefined); | ||
expect(device).toEqual({ type: 'iPhone X' }); | ||
}); | ||
}); | ||
|
||
describe('for a specified configuration name', () => { | ||
let sampleConfigs; | ||
|
||
describe('if a device configuration has the new .device property', () => { | ||
beforeEach(() => { | ||
cliConfig.configuration = 'config2'; | ||
rawDeviceConfig.device = 'iPhone SE'; | ||
}); | ||
|
||
const [config1, config2] = [1, 2].map(i => ({ | ||
type: `someDriver${i}`, | ||
device: `someDevice${i}`, | ||
})); | ||
it('should be left intact', () => { | ||
const { type, device } = compose(); | ||
|
||
sampleConfigs = { config1, config2 }; | ||
expect(type).toBe('ios.simulator'); | ||
expect(device).toBe('iPhone SE'); | ||
}); | ||
|
||
it('should return that config', () => { | ||
expect(composeDeviceConfig({ | ||
configurations: sampleConfigs | ||
}, cliConfig)).toEqual(sampleConfigs.config2); | ||
}); | ||
describe('and there is a CLI override', () => { | ||
beforeEach(() => { | ||
cliConfig.deviceName = 'iPad Air'; | ||
}); | ||
|
||
describe('if device-name override is present', () => { | ||
beforeEach(() => { cliConfig.deviceName = 'Override'; }); | ||
it('should be override .device property', () => { | ||
const { type, device } = compose(); | ||
|
||
it('should return that config with an overriden device query', () => { | ||
expect(composeDeviceConfig({ | ||
configurations: sampleConfigs | ||
}, cliConfig)).toEqual({ | ||
...sampleConfigs.config2, | ||
device: 'Override', | ||
}); | ||
expect(type).toBe('ios.simulator'); | ||
expect(device).toBe('iPad Air'); | ||
}); | ||
}) | ||
}); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.