@@ -25,10 +25,14 @@ describe("login", () => {
25
25
// Create a fake element and set the attribute
26
26
const mockElement = document . createElement ( "input" )
27
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
- )
28
+ const expected = {
29
+ base : "./hello-world" ,
30
+ csStaticBase : "./static/development/Users/jp/Dev/code-server" ,
31
+ logLevel : 2 ,
32
+ disableTelemetry : false ,
33
+ disableUpdateCheck : false ,
34
+ }
35
+ mockElement . setAttribute ( "data-settings" , JSON . stringify ( expected ) )
32
36
document . body . appendChild ( mockElement )
33
37
spy . mockImplementation ( ( ) => mockElement )
34
38
// Load file
@@ -40,11 +44,10 @@ describe("login", () => {
40
44
} )
41
45
42
46
describe ( "there is no element with id 'base'" , ( ) => {
43
- let initialDom : Document
44
47
beforeEach ( ( ) => {
45
48
const dom = new JSDOM ( )
46
- initialDom = dom . window . document
47
49
global . document = dom . window . document
50
+ global . MutationObserver = dom . window . MutationObserver
48
51
49
52
const location : LocationLike = {
50
53
pathname : "/healthz" ,
@@ -60,10 +63,43 @@ describe("login", () => {
60
63
} )
61
64
62
65
it ( "should not change the DOM" , ( ) => {
66
+ const log = console . log
67
+ const messages = [ ]
68
+
69
+ console . log = ( x : string ) => {
70
+ messages . push ( x )
71
+ }
72
+ // Select the node that will be observed for mutations
73
+ const targetNode = document . body
74
+
75
+ // Options for the observer (which mutations to observe)
76
+ const config = { attributes : true , childList : true , subtree : true }
77
+
78
+ // Callback function to execute when mutations are observed
79
+ const callback = function ( mutationsList : any , observer : any ) {
80
+ // Use traditional 'for loops' for IE 11
81
+ for ( const mutation of mutationsList ) {
82
+ if ( mutation . type === "childList" ) {
83
+ console . log ( "A child node has been added or removed." )
84
+ } else if ( mutation . type === "attributes" ) {
85
+ console . log ( "The " + mutation . attributeName + " attribute was modified." )
86
+ }
87
+ }
88
+ }
89
+
90
+ // Create an observer instance linked to the callback function
91
+ const observer = new MutationObserver ( callback )
92
+
93
+ // Start observing the target node for configured mutations
94
+ observer . observe ( targetNode , config )
95
+
63
96
// Load file
64
97
require ( "../../../src/browser/pages/login" )
65
- const currentDom = global . document
66
- expect ( initialDom ) . toBe ( currentDom )
98
+ // Nothing should have changed, which means no messages
99
+ expect ( messages . length ) . toBe ( 0 )
100
+ // Later, you can stop observing
101
+ observer . disconnect ( )
102
+ console . log = log
67
103
} )
68
104
} )
69
105
} )
0 commit comments