From c7987f114f19fe22f583f24f8b6399d2a77bf276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sun, 27 Oct 2019 20:34:03 +0000 Subject: [PATCH 1/2] fix(tests): include typescript in jest coverage --- ui/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui/package.json b/ui/package.json index 1b3442bb2..a31d89c90 100644 --- a/ui/package.json +++ b/ui/package.json @@ -69,8 +69,8 @@ }, "jest": { "collectCoverageFrom": [ - "src/**/*.{js,jsx}", - "!src/**/*.stories.{js,jsx}" + "src/**/*.{js,jsx,tsx}", + "!src/**/*.stories.{js,jsx,tsx}" ] }, "devDependencies": { From 6ea4149d8aee2b4ff3eee6ad19b017d732a0f7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sun, 27 Oct 2019 21:46:25 +0000 Subject: [PATCH 2/2] fix(tests): refactor entrypoint for full test coverage --- ui/src/AppBoot.js | 11 ++++++++--- ui/src/AppBoot.test.js | 13 +++++++++---- ui/src/index.test.js | 44 +++++++++++++++++++++++++++++++++++++----- ui/src/index.tsx | 19 ++++++------------ 4 files changed, 62 insertions(+), 25 deletions(-) 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") );