Skip to content

Commit

Permalink
web: Add methods to DevicesManager
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Mar 13, 2024
1 parent 7153302 commit c26d540
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
41 changes: 41 additions & 0 deletions web/src/components/storage/DevicesManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,55 @@ export default class DevicesManager {
return compact(uniq(sids).map(sid => this.stagingDevice(sid)));
}

/**
* Devices deleted.
*
* @note The devices are extracted from the actions.
*
* @returns {StorageDevice[]}
*/
deletedDevices() {
const sids = this.actions
.filter(a => a.delete)
.map(a => a.device);
const devices = sids.map(sid => this.systemDevice(sid));
return compact(devices);
}

/**
* Systems deleted.
*
* @returns {string[]}
*/
deletedSystems() {
const systems = this.deletedDevices()
.map(d => d.systems)
.flat();
return compact(systems);
}

/**
* @param {number} sid
* @param {StorageDevice[]} source
* @returns {StorageDevice|undefined}
*/
#device(sid, source) {
return source.find(d => d.sid === sid);
}

/**
* @param {StorageDevice} device
* @param {StorageDevice[]} source
* @returns {boolean}
*/
#exist(device, source) {
return this.#device(device.sid, source) !== undefined;
}

/**
* @param {StorageDevice} device
* @returns {boolean}
*/
#isUsed(device) {
const sids = uniq(compact(this.actions.map(a => a.device)));

Expand Down
51 changes: 51 additions & 0 deletions web/src/components/storage/DevicesManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,54 @@ describe("usedDevices", () => {
});
});
});

describe("deletedDevices", () => {
beforeEach(() => {
system = [
{ sid: 60 },
{ sid: 62 },
{ sid: 63 },
{ sid: 64 }
];
actions = [
{ device: 60, delete: true },
// This device does not exist in system.
{ device: 61, delete: true },
{ device: 62, delete: false },
{ device: 63, delete: true }
];
});

it("includes all deleted devices", () => {
const manager = new DevicesManager(system, staging, actions);
const sids = manager.deletedDevices().map(d => d.sid)
.sort();
expect(sids).toEqual([60, 63]);
});
});

describe("deletedSystems", () => {
beforeEach(() => {
system = [
{ sid: 60, systems: ["Windows XP"] },
{ sid: 62, systems: ["Ubuntu"] },
{ sid: 63, systems: ["openSUSE Leap"] },
{ sid: 64 }
];
actions = [
{ device: 60, delete: true },
// This device does not exist in system.
{ device: 61, delete: true },
{ device: 62, delete: false },
{ device: 63, delete: true }
];
});

it("includes all deleted systems", () => {
const manager = new DevicesManager(system, staging, actions);
const systems = manager.deletedSystems();
expect(systems.length).toEqual(2);
expect(systems).toContain("Windows XP");
expect(systems).toContain("openSUSE Leap");
});
});

0 comments on commit c26d540

Please sign in to comment.