Skip to content

fix: allow more client options to be configured via resource url (#4156) #4157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions client-src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
54 changes: 54 additions & 0 deletions test/client/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!!");

Expand Down