Skip to content

Commit c062a41

Browse files
committed
feat: add tests for pages/login.ts
1 parent 729ee41 commit c062a41

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

test/browser/pages/login.test.ts

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

0 commit comments

Comments
 (0)