Skip to content

Commit

Permalink
[tests] fix unit tests to stub device
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jun 18, 2024
1 parent aef7050 commit fafade8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/cmd/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,11 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
*/
async _withDevice(fn) {
try {
await this.getUsbDevice(this.device);
await this._getUsbDevice(this.device);

if (this.device.isInDfuMode) {
await this._resetDevice(this.device);
await this.getUsbDevice(this.device);
await this._getUsbDevice(this.device);
}

return await fn();
Expand Down Expand Up @@ -366,7 +366,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
* @param {Object} dev - The USB device instance.
* @returns {Promise<void>}
*/
async getUsbDevice(dev) {
async _getUsbDevice(dev) {
if (!dev || dev.isOpen === false) {
this.device = await usbUtils.getOneUsbDevice({ api: this.api, idOrName: this.deviceId, ui: this.ui });
this.deviceId = this.device._id;
Expand Down
18 changes: 8 additions & 10 deletions src/cmd/device-protection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ describe('DeviceProtectionCommands', () => {

beforeEach(() => {
deviceProtectionCommands = new DeviceProtectionCommands();
sinon.stub(deviceProtectionCommands, '_getUsbDevice').resolves();
deviceProtectionCommands.device = {
isInDfuMode: false,
unprotectDevice: sinon.stub()
};
});

afterEach(() => {
Expand Down Expand Up @@ -351,30 +356,23 @@ describe('DeviceProtectionCommands', () => {
describe('_withDevice', () => {
it('should execute a function with the device in normal mode', async () => {
const fn = sinon.stub().resolves();
sinon.stub(deviceProtectionCommands, 'getUsbDevice').resolves();
deviceProtectionCommands.device = {
isInDfuMode: false
};
sinon.stub(deviceProtectionCommands, '_resetDevice').resolves();

await deviceProtectionCommands._withDevice(fn);

expect(deviceProtectionCommands.getUsbDevice).to.have.been.calledOnce;
expect(deviceProtectionCommands._getUsbDevice).to.have.been.calledOnce;
expect(deviceProtectionCommands._resetDevice).to.not.have.been.called;
expect(fn).to.have.been.calledOnce;
});

it('should execute a function with the device in dfu mode', async () => {
const fn = sinon.stub().resolves();
sinon.stub(deviceProtectionCommands, 'getUsbDevice').resolves();
deviceProtectionCommands.device = {
isInDfuMode: true
};
deviceProtectionCommands.device.isInDfuMode = true;
sinon.stub(deviceProtectionCommands, '_resetDevice').resolves();

await deviceProtectionCommands._withDevice(fn);

expect(deviceProtectionCommands.getUsbDevice).to.have.been.calledTwice;
expect(deviceProtectionCommands._getUsbDevice).to.have.been.calledTwice;
expect(deviceProtectionCommands._resetDevice).to.have.been.calledOnce;
expect(fn).to.have.been.calledOnce;
});
Expand Down

0 comments on commit fafade8

Please sign in to comment.