Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed May 16, 2024
1 parent abcee5c commit 9e7c5e8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/lib/wifi-control-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module.exports = class WiFiControlRequest {
network = await this.getNetworkToConnect();
}
await this.setWifiCredentials(network);
console.log('Done! WiFi credentials have been set.');
}

async getNetworkToConnectFromJson() {
Expand Down Expand Up @@ -216,9 +215,14 @@ module.exports = class WiFiControlRequest {
if (!this.device || this.device.isOpen === false) {
this.device = await usbUtils.getOneUsbDevice({ api: this.api, idOrName: this.deviceId });
}
await this.device.setWifiCredentials({ ssid, password });
this.stopSpin();
return;
const { pass } = await this.device.setWifiCredentials({ ssid, password }, { timeout: JOIN_NETWORK_TIMEOUT });
if (pass) {
this.stopSpin();
this.ui.stdout.write('Wi-Fi network configured successfully, your device should now restart.');
this.ui.stdout.write(os.EOL);
await this.device.reset();
return;
}
} catch (error) {
lastError = error;
await utilities.delay(TIME_BETWEEN_RETRIES);
Expand Down
41 changes: 36 additions & 5 deletions src/lib/wifi-control-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('Wifi Control Request', () => {
close: sinon.stub(),
reset: sinon.stub(),
scanWifiNetworks: sinon.stub(),
joinNewWifiNetwork: sinon.stub()
joinNewWifiNetwork: sinon.stub(),
setWifiCredentials: sinon.stub()
};
usbUtil.getOneUsbDevice = sinon.stub();
});
Expand Down Expand Up @@ -420,26 +421,56 @@ describe('Wifi Control Request', () => {
});
});

describe('setWifiCredentials', () => {
it('joins a network', async () => {
openDevice.setWifiCredentials.resolves({ pass: true });
usbUtil.getOneUsbDevice.resolves(openDevice);
const wifiControlRequest = new WifiControlRequest('deviceId', { ui, newSpin, stopSpin });
await wifiControlRequest.setWifiCredentials({ ssid: 'network1', password: 'password' });
expect(openDevice.setWifiCredentials).to.have.been.calledOnce;
expect(openDevice.setWifiCredentials).to.have.been.calledWith({ ssid: 'network1', password: 'password' }, { timeout: 30000 });
expect(newSpin).to.have.been.calledWith('Setting Wi-Fi credentials for \'network1\'');
expect(stopSpin).to.have.been.calledOnce;
expect(ui.stdout.write).to.have.been.calledWith('Wi-Fi network configured successfully, your device should now restart.');
});

it('throw error if fails', async () => {
openDevice.setWifiCredentials.rejects(new Error('error'));
usbUtil.getOneUsbDevice.resolves(openDevice);
let error;
const wifiControlRequest = new WifiControlRequest('deviceId', { ui, newSpin, stopSpin });
try {
await wifiControlRequest.setWifiCredentials({ ssid: 'network1', password: 'password' });
} catch (_error) {
error = _error;
}
expect(error.message).to.eql('Unable to set Wi-Fi credentials: error');
expect(openDevice.setWifiCredentials).to.have.been.calledWith({ ssid: 'network1', password: 'password' }, { timeout: 30000 });
expect(newSpin).to.have.been.calledWith('Setting Wi-Fi credentials for \'network1\'');
expect(stopSpin).to.have.been.calledOnce;
});
});

describe('configureWifi', () => {
it('performs the wifi configuration flow', async () => {
const wifiControlRequest = new WifiControlRequest('deviceId', { ui, newSpin, stopSpin });
wifiControlRequest.getNetworkToConnect = sinon.stub().resolves({ ssid: 'network1', password: 'password' });
wifiControlRequest.getNetworkToConnectFromJson = sinon.stub();
wifiControlRequest.joinWifi = sinon.stub().resolves(true);
wifiControlRequest.setWifiCredentials = sinon.stub().resolves(true);
await wifiControlRequest.configureWifi();
expect(wifiControlRequest.getNetworkToConnect).to.have.been.calledOnce;
expect(wifiControlRequest.joinWifi).to.have.been.calledOnce;
expect(wifiControlRequest.setWifiCredentials).to.have.been.calledOnce;
expect(wifiControlRequest.getNetworkToConnectFromJson).not.to.have.been.called;
});

it('performs the wifi configuration flow from json', async () => {
const wifiControlRequest = new WifiControlRequest('deviceId', { ui, newSpin, stopSpin, file: 'file' });
wifiControlRequest.getNetworkToConnect = sinon.stub();
wifiControlRequest.getNetworkToConnectFromJson = sinon.stub().resolves({ ssid: 'network1', password: 'password' });
wifiControlRequest.joinWifi = sinon.stub().resolves(true);
wifiControlRequest.setWifiCredentials = sinon.stub().resolves(true);
await wifiControlRequest.configureWifi();
expect(wifiControlRequest.getNetworkToConnect).not.to.have.been.called;
expect(wifiControlRequest.joinWifi).to.have.been.calledOnce;
expect(wifiControlRequest.setWifiCredentials).to.have.been.calledOnce;
expect(wifiControlRequest.getNetworkToConnectFromJson).to.have.been.calledOnce;
});
});
Expand Down

0 comments on commit 9e7c5e8

Please sign in to comment.