|
| 1 | +const Log = require("logger"); |
| 2 | +const { applyElectronSwitches } = require("../../../js/electron_helper"); |
| 3 | + |
| 4 | +describe("electron switches", () => { |
| 5 | + let commandLine; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + commandLine = { |
| 9 | + appendSwitch: vi.fn() |
| 10 | + }; |
| 11 | + vi.spyOn(Log, "error").mockImplementation(() => {}); |
| 12 | + }); |
| 13 | + |
| 14 | + it("does nothing when electronSwitches is undefined", () => { |
| 15 | + applyElectronSwitches(commandLine, undefined); |
| 16 | + |
| 17 | + expect(commandLine.appendSwitch).not.toHaveBeenCalled(); |
| 18 | + expect(Log.error).not.toHaveBeenCalled(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("applies string entries as switches without values", () => { |
| 22 | + applyElectronSwitches(commandLine, ["no-sandbox", "disable-http-cache"]); |
| 23 | + |
| 24 | + expect(commandLine.appendSwitch).toHaveBeenCalledTimes(2); |
| 25 | + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "no-sandbox"); |
| 26 | + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "disable-http-cache"); |
| 27 | + expect(Log.error).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("applies object entries as switches with values", () => { |
| 31 | + applyElectronSwitches(commandLine, [ |
| 32 | + { "js-flags": "--max-old-space-size=8192" }, |
| 33 | + { "password-store": "basic" } |
| 34 | + ]); |
| 35 | + |
| 36 | + expect(commandLine.appendSwitch).toHaveBeenCalledTimes(2); |
| 37 | + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "js-flags", "--max-old-space-size=8192"); |
| 38 | + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "password-store", "basic"); |
| 39 | + expect(Log.error).not.toHaveBeenCalled(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("allows one object entry to define multiple switches with values", () => { |
| 43 | + applyElectronSwitches(commandLine, [ |
| 44 | + "no-sandbox", |
| 45 | + { |
| 46 | + "js-flags": "--max-old-space-size=8192", |
| 47 | + "password-store": "basic" |
| 48 | + } |
| 49 | + ]); |
| 50 | + |
| 51 | + expect(commandLine.appendSwitch).toHaveBeenCalledTimes(3); |
| 52 | + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "no-sandbox"); |
| 53 | + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "js-flags", "--max-old-space-size=8192"); |
| 54 | + expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(3, "password-store", "basic"); |
| 55 | + expect(Log.error).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("logs an error for invalid entries", () => { |
| 59 | + applyElectronSwitches(commandLine, ["no-sandbox", ["js-flags", "--max-old-space-size=8192"], null]); |
| 60 | + |
| 61 | + expect(commandLine.appendSwitch).toHaveBeenCalledTimes(1); |
| 62 | + expect(commandLine.appendSwitch).toHaveBeenCalledWith("no-sandbox"); |
| 63 | + expect(Log.error).toHaveBeenCalledTimes(2); |
| 64 | + }); |
| 65 | + |
| 66 | + it("logs an error when electronSwitches is not an array", () => { |
| 67 | + applyElectronSwitches(commandLine, { "js-flags": "--max-old-space-size=8192" }); |
| 68 | + |
| 69 | + expect(commandLine.appendSwitch).not.toHaveBeenCalled(); |
| 70 | + expect(Log.error).toHaveBeenCalledWith(expect.stringContaining("electronSwitches must be an array of strings or objects")); |
| 71 | + }); |
| 72 | +}); |
0 commit comments