From 7ad536b5e9f60c5fa2c0f0c4429b30e870fdfcae Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Fri, 2 Jul 2021 14:57:16 -0700 Subject: [PATCH] feat(test): check login.ts when base missing --- test/browser/pages/login.test.ts | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/browser/pages/login.test.ts b/test/browser/pages/login.test.ts index 0bd500ebb..d002d72a8 100644 --- a/test/browser/pages/login.test.ts +++ b/test/browser/pages/login.test.ts @@ -42,4 +42,49 @@ describe("login", () => { expect(el?.value).toBe("/hello-world") }) }) + describe("there is not an element with id 'base'", () => { + let spy: jest.SpyInstance + + beforeAll(() => { + // This is important because we're manually requiring the file + // If you don't call this before all tests + // the module registry from other tests may cause side effects. + jest.resetModuleRegistry() + }) + + beforeEach(() => { + const dom = new JSDOM() + global.document = dom.window.document + spy = jest.spyOn(document, "getElementById") + + const location: LocationLike = { + pathname: "/healthz", + origin: "http://localhost:8080", + } + + global.location = location as Location + }) + + afterEach(() => { + spy.mockClear() + jest.resetModules() + // Reset the global.document + global.document = undefined as any as Document + global.location = undefined as any as Location + }) + + afterAll(() => { + jest.restoreAllMocks() + }) + + it("should do nothing", () => { + spy.mockImplementation(() => null) + // Load file + require("../../../src/browser/pages/login") + + // It's called once by getOptions in the top of the file + // and then another to get the base element + expect(spy).toHaveBeenCalledTimes(2) + }) + }) })