|
| 1 | +import { JSDOM } from "jsdom" |
| 2 | +import { LocationLike } from "../../unit/util.test" |
| 3 | + |
| 4 | +describe("login", () => { |
| 5 | + describe("there is an element with id 'base'", () => { |
| 6 | + beforeEach(() => { |
| 7 | + const dom = new JSDOM() |
| 8 | + global.document = dom.window.document |
| 9 | + |
| 10 | + const location: LocationLike = { |
| 11 | + pathname: "/healthz", |
| 12 | + origin: "http://localhost:8080", |
| 13 | + } |
| 14 | + |
| 15 | + global.location = location as Location |
| 16 | + }) |
| 17 | + afterEach(() => { |
| 18 | + // Reset the global.document |
| 19 | + global.document = undefined as any as Document |
| 20 | + global.location = undefined as any as Location |
| 21 | + }) |
| 22 | + it("should set the value to options.base", () => { |
| 23 | + // Mock getElementById |
| 24 | + const spy = jest.spyOn(document, "getElementById") |
| 25 | + // Create a fake element and set the attribute |
| 26 | + const mockElement = document.createElement("input") |
| 27 | + mockElement.setAttribute("id", "base") |
| 28 | + mockElement.setAttribute( |
| 29 | + "data-settings", |
| 30 | + '{"base":"./hello-world","csStaticBase":"./static/development/Users/jp/Dev/code-server","logLevel":2,"disableTelemetry":false,"disableUpdateCheck":false}', |
| 31 | + ) |
| 32 | + document.body.appendChild(mockElement) |
| 33 | + spy.mockImplementation(() => mockElement) |
| 34 | + // Load file |
| 35 | + require("../../../src/browser/pages/login") |
| 36 | + |
| 37 | + const el: HTMLInputElement | null = document.querySelector("input#base") |
| 38 | + expect(el?.value).toBe("/hello-world") |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + describe("there is no element with id 'base'", () => { |
| 43 | + let initialDom: Document |
| 44 | + beforeEach(() => { |
| 45 | + const dom = new JSDOM() |
| 46 | + initialDom = dom.window.document |
| 47 | + global.document = dom.window.document |
| 48 | + |
| 49 | + const location: LocationLike = { |
| 50 | + pathname: "/healthz", |
| 51 | + origin: "http://localhost:8080", |
| 52 | + } |
| 53 | + |
| 54 | + global.location = location as Location |
| 55 | + }) |
| 56 | + afterEach(() => { |
| 57 | + // Reset the global.document |
| 58 | + global.document = undefined as any as Document |
| 59 | + global.location = undefined as any as Location |
| 60 | + }) |
| 61 | + |
| 62 | + it("should not change the DOM", () => { |
| 63 | + // Load file |
| 64 | + require("../../../src/browser/pages/login") |
| 65 | + const currentDom = global.document |
| 66 | + expect(initialDom).toBe(currentDom) |
| 67 | + }) |
| 68 | + }) |
| 69 | +}) |
0 commit comments