diff --git a/ui/src/AppBoot.js b/ui/src/AppBoot.js
index bf60eb3ab..c43f94869 100644
--- a/ui/src/AppBoot.js
+++ b/ui/src/AppBoot.js
@@ -51,12 +51,17 @@ const ParseDefaultFilters = settingsElement => {
return defaultFilters;
};
-const ParseUIDefaults = b64data => {
- const decoded = Buffer.from(b64data, "base64").toString("ascii");
+const ParseUIDefaults = defaultsElement => {
+ if (defaultsElement === null) {
+ return null;
+ }
+ const decoded = Buffer.from(defaultsElement.innerHTML, "base64").toString(
+ "ascii"
+ );
try {
return JSON.parse(decoded);
} catch {
- return undefined;
+ return null;
}
};
diff --git a/ui/src/AppBoot.test.js b/ui/src/AppBoot.test.js
index e42a84cc3..51d95a70b 100644
--- a/ui/src/AppBoot.test.js
+++ b/ui/src/AppBoot.test.js
@@ -138,12 +138,17 @@ describe("ParseDefaultFilters()", () => {
describe("ParseUIDefaults()", () => {
it("parses base64 encoded JSON with defaults", () => {
- const uiDefaults = ParseUIDefaults(DefaultsBase64);
+ const uiDefaults = ParseUIDefaults({ innerHTML: DefaultsBase64 });
expect(uiDefaults).toStrictEqual(DefaultsObject);
});
- it("returns undefined on invalid JSON", () => {
- const uiDefaults = ParseUIDefaults("e3h4eC9mZgo=");
- expect(uiDefaults).toBeUndefined();
+ it("returns null on null element", () => {
+ const uiDefaults = ParseUIDefaults(null);
+ expect(uiDefaults).toBeNull();
+ });
+
+ it("returns null on invalid JSON", () => {
+ const uiDefaults = ParseUIDefaults({ innerHTML: "e3h4eC9mZgo=" });
+ expect(uiDefaults).toBeNull();
});
});
diff --git a/ui/src/index.test.js b/ui/src/index.test.js
index 330f8c64c..a836de339 100644
--- a/ui/src/index.test.js
+++ b/ui/src/index.test.js
@@ -1,11 +1,45 @@
import { EmptyAPIResponse } from "__mocks__/Fetch";
import { DefaultsBase64 } from "__mocks__/Defaults";
-it("renders without crashing", () => {
- jest.spyOn(document, "getElementById").mockImplementationOnce(() => {
- return {
- innerHTML: `
${DefaultsBase64}
`
- };
+const settingsElement = {
+ dataset: {
+ sentryDsn: "",
+ version: "1.2.3",
+ defaultFiltersBase64: "WyJmb289YmFyIiwiYmFyPX5iYXoiXQ=="
+ }
+};
+
+it("renders without crashing with missing defaults div", () => {
+ const root = document.createElement("div");
+ jest.spyOn(global.document, "getElementById").mockImplementation(name => {
+ return name === "settings"
+ ? settingsElement
+ : name === "defaults"
+ ? null
+ : name === "root"
+ ? root
+ : null;
+ });
+ const response = EmptyAPIResponse();
+ response.filters = [];
+ fetch.mockResponse(JSON.stringify(response));
+ const Index = require("./index.tsx");
+ expect(Index).toBeTruthy();
+ expect(root.innerHTML).toMatch(/data-filters="foo=bar bar=~baz"/);
+});
+
+it("renders without crashing with defaults present", () => {
+ const root = document.createElement("div");
+ jest.spyOn(global.document, "getElementById").mockImplementation(name => {
+ return name === "settings"
+ ? settingsElement
+ : name === "defaults"
+ ? {
+ innerHTML: DefaultsBase64
+ }
+ : name === "root"
+ ? root
+ : null;
});
const response = EmptyAPIResponse();
response.filters = [];
diff --git a/ui/src/index.tsx b/ui/src/index.tsx
index 43702d17a..640d99ec2 100644
--- a/ui/src/index.tsx
+++ b/ui/src/index.tsx
@@ -17,25 +17,18 @@ import {
} from "./AppBoot";
import { App } from "./App";
-let uiDefaults;
-const defaultsElement = document.getElementById("defaults");
-if (defaultsElement !== null) {
- uiDefaults = ParseUIDefaults(defaultsElement.innerHTML);
-}
-
-const settingsElement = SettingsElement();
-
-SetupSentry(settingsElement);
+SetupSentry(SettingsElement());
// global timer for updating timestamps to human readable offsets
// this needs to be run before any instance
// https://www.npmjs.com/package/react-moment#pooled-timer
Moment.startPooledTimer();
-const defaultFilters = ParseDefaultFilters(settingsElement);
-
// https://wetainment.com/testing-indexjs/
export default ReactDOM.render(
- ,
- document.getElementById("root") || document.createElement("div")
+ ,
+ document.getElementById("root")
);