diff --git a/client-src/index.js b/client-src/index.js index dc85fdcff0..8109be851b 100644 --- a/client-src/index.js +++ b/client-src/index.js @@ -58,6 +58,40 @@ if (parsedResourceQuery["live-reload"] === "true") { log.info("Live Reloading enabled."); } +if (parsedResourceQuery.progress === "true") { + options.progress = true; + + log.info("Progress reporting enabled."); +} + +if (parsedResourceQuery.overlay) { + try { + options.overlay = JSON.parse(parsedResourceQuery.overlay); + } catch (e) { + log.error("Error parsing overlay options from resource query:", e); + } + + // Fill in default "true" params for partially-specified objects. + if (typeof options.overlay === "object") { + options.overlay = { + errors: true, + warnings: true, + ...options.overlay, + }; + } + + if ( + options.overlay === true || + (options.overlay.errors && options.overlay.warnings) + ) { + log.info("Overlay is enabled for errors and warnings."); + } else if (options.overlay.errors) { + log.info("Overlay is enabled for errors."); + } else if (options.overlay.warnings) { + log.info("Overlay is enabled for warnings."); + } +} + if (parsedResourceQuery.logging) { options.logging = parsedResourceQuery.logging; } diff --git a/test/client/index.test.js b/test/client/index.test.js index ee3c64a947..75a902cb1a 100644 --- a/test/client/index.test.js +++ b/test/client/index.test.js @@ -208,6 +208,60 @@ describe("index", () => { expect(overlay.show).toBeCalled(); }); + test("should parse overlay options from resource query", () => { + jest.isolateModules(() => { + // Pass JSON config with warnings disabled + global.__resourceQuery = `?overlay=${encodeURIComponent( + `{"warnings": false}` + )}`; + overlay.show.mockReset(); + socket.mockReset(); + jest.unmock("../../client-src/utils/parseURL.js"); + require("../../client-src"); + onSocketMessage = socket.mock.calls[0][1]; + + onSocketMessage.warnings(["warn1"]); + expect(overlay.show).not.toBeCalled(); + + onSocketMessage.errors(["error1"]); + expect(overlay.show).toBeCalledTimes(1); + }); + + jest.isolateModules(() => { + // Pass JSON config with errors disabled + global.__resourceQuery = `?overlay=${encodeURIComponent( + `{"errors": false}` + )}`; + overlay.show.mockReset(); + socket.mockReset(); + jest.unmock("../../client-src/utils/parseURL.js"); + require("../../client-src"); + onSocketMessage = socket.mock.calls[0][1]; + + onSocketMessage.errors(["error1"]); + expect(overlay.show).not.toBeCalled(); + + onSocketMessage.warnings(["warn1"]); + expect(overlay.show).toBeCalledTimes(1); + }); + + jest.isolateModules(() => { + // Use simple boolean + global.__resourceQuery = "?overlay=true"; + jest.unmock("../../client-src/utils/parseURL.js"); + socket.mockReset(); + overlay.show.mockReset(); + require("../../client-src"); + onSocketMessage = socket.mock.calls[0][1]; + + onSocketMessage.warnings(["warn2"]); + expect(overlay.show).toBeCalledTimes(1); + + onSocketMessage.errors(["error2"]); + expect(overlay.show).toBeCalledTimes(2); + }); + }); + test("should run onSocketMessage.error", () => { onSocketMessage.error("error!!");