Skip to content

Commit e6b2cf3

Browse files
authored
fix: get connectivity return value (#987)
* fix: get connectivity return value * fix lint * revert type * add an empty case
1 parent 75800a5 commit e6b2cf3

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

lib/commands/network.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export async function mobileSetConnectivity(wifi, data, airplaneMode) {
9797
*/
9898
export async function mobileGetConnectivity(services = SUPPORTED_SERVICE_NAMES) {
9999
const svcs = _.castArray(services);
100-
const unsupportedServices = _.difference(services, SUPPORTED_SERVICE_NAMES);
100+
const unsupportedServices = _.difference(svcs, SUPPORTED_SERVICE_NAMES);
101101
if (!_.isEmpty(unsupportedServices)) {
102102
throw new errors.InvalidArgumentError(
103103
`${util.pluralize(
@@ -120,11 +120,11 @@ export async function mobileGetConnectivity(services = SUPPORTED_SERVICE_NAMES)
120120
),
121121
};
122122
await B.all(_.values(statePromises));
123-
return {
124-
wifi: Boolean(statePromises.wifi.value()),
125-
data: Boolean(statePromises.data.value()),
126-
airplaneMode: Boolean(statePromises.airplaneMode.value()),
127-
};
123+
return _.reduce(
124+
statePromises,
125+
(state, v, k) => _.isUndefined(v.value()) ? state : {...state, [k]: Boolean(v.value())},
126+
{}
127+
);
128128
}
129129

130130
/**

lib/commands/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,15 @@ export interface GetConnectivityResult {
245245
/**
246246
* True if wifi is enabled
247247
*/
248-
wifi: boolean;
248+
wifi?: boolean;
249249
/**
250250
* True if mobile data connection is enabled
251251
*/
252-
data: boolean;
252+
data?: boolean;
253253
/**
254254
* True if Airplane Mode is enabled
255255
*/
256-
airplaneMode: boolean;
256+
airplaneMode?: boolean;
257257
}
258258

259259
export type ServiceType = 'wifi' | 'data' | 'airplaneMode';

test/unit/commands/network-specs.js

+42
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {ADB} from 'appium-adb';
33
import {AndroidDriver} from '../../../lib/driver';
44
import B from 'bluebird';
55
import { SettingsApp } from 'io.appium.settings';
6+
import { errors } from 'appium/driver';
67

78
/** @type {AndroidDriver} */
89
let driver;
@@ -118,6 +119,47 @@ describe('Network', function () {
118119
driver.setDataState.calledWithExactly(true).should.be.true;
119120
});
120121
});
122+
describe('mobileGetConnectivity', function () {
123+
it('should raise unsupported services in string', async function () {
124+
await driver.mobileGetConnectivity('bad')
125+
.should.eventually.rejectedWith(errors.InvalidArgumentError);
126+
});
127+
it('should raise unsupported services in array', async function () {
128+
await driver.mobileGetConnectivity(['bad', 'array'])
129+
.should.eventually.rejectedWith(errors.InvalidArgumentError);
130+
});
131+
it('should raise unsupported services with an empty array', async function () {
132+
await driver.mobileGetConnectivity().should.eventually.eql({});
133+
});
134+
it('should return all supported services', async function () {
135+
adb.isWifiOn.returns(true);
136+
adb.isDataOn.returns(true);
137+
adb.isAirplaneModeOn.returns(true);
138+
await driver.mobileGetConnectivity()
139+
.should.eventually.eql({ wifi: true, data: true, airplaneMode: true });
140+
});
141+
it('should return only wifi', async function () {
142+
adb.isWifiOn.returns(true);
143+
adb.isDataOn.returns(true);
144+
adb.isAirplaneModeOn.returns(true);
145+
await driver.mobileGetConnectivity('wifi')
146+
.should.eventually.eql({ wifi: true });
147+
});
148+
it('should return only data', async function () {
149+
adb.isWifiOn.returns(true);
150+
adb.isDataOn.returns(true);
151+
adb.isAirplaneModeOn.returns(true);
152+
await driver.mobileGetConnectivity(['data'])
153+
.should.eventually.eql({ data: true });
154+
});
155+
it('should return only data and airplaneMode', async function () {
156+
adb.isWifiOn.returns(true);
157+
adb.isDataOn.returns(true);
158+
adb.isAirplaneModeOn.returns(false);
159+
await driver.mobileGetConnectivity(['data', 'airplaneMode'])
160+
.should.eventually.eql({ data: true, airplaneMode: false});
161+
});
162+
});
121163
describe('toggleData', function () {
122164
it('should toggle data', async function () {
123165
adb.isDataOn.returns(false);

0 commit comments

Comments
 (0)