Skip to content

Commit

Permalink
Merge pull request #996 from lbrooks/test-DeviceMap
Browse files Browse the repository at this point in the history
cleanup: Add tests for DeviceMap
  • Loading branch information
alexpargon authored Feb 7, 2025
2 parents 0efb245 + fd7396a commit 6d9fb9e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/api/comms/deviceMap/DeviceMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import DeviceMap from "./DeviceMap";
import log from "electron-log/renderer";


describe('DeviceMap', () => {
beforeEach(() => {
vi.mock("electron-log/renderer", () => ({
default: {
error: vi.fn(),
warn: vi.fn(),
info: vi.fn(),
verbose: vi.fn(),
debug: vi.fn(),
silly: vi.fn(),
},
}));
});

afterEach(() => {
vi.clearAllMocks();
});

it('should return false with new key', async () => {
const map = new DeviceMap();
expect(map.isUpdated("key", "value")).toBe(false);

expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.warn).toHaveBeenCalledWith("comparison between keys", false, undefined, "value");
});
it('should return false with differing values', async () => {
const map = new DeviceMap();
map.set("key", "original");
expect(map.isUpdated("key", "value")).toBe(false);

expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.warn).toHaveBeenCalledWith("comparison between keys", false, "original", "value");

});
it('should return true with same value', async () => {
const map = new DeviceMap();
map.set("key", "value");
expect(map.isUpdated("key", "value")).toBe(true);

expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.warn).toHaveBeenCalledWith("comparison between keys", true, "value", "value");
});
});
3 changes: 1 addition & 2 deletions src/api/comms/deviceMap/DeviceMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import log from "electron-log/renderer";
export default class DeviceMap extends Map<string, string> {
isUpdated(key: string, value: string): boolean {
log.warn("comparison between keys", this.get(key) === value, this.get(key), value);
if (this.get(key) === value) return true;
return false;
return this.get(key) === value;
}
}

0 comments on commit 6d9fb9e

Please sign in to comment.