@@ -3,36 +3,108 @@ import { withBrowser } from 'pleasantest';
3
3
test (
4
4
'injectHTML' ,
5
5
withBrowser ( async ( { utils, page } ) => {
6
- const getHTML = ( ) => page . evaluate ( ( ) => document . body . innerHTML . trim ( ) ) ;
6
+ const getHTML = ( ) =>
7
+ page . evaluate ( ( ) => document . documentElement . innerHTML . trim ( ) ) ;
7
8
8
9
await utils . injectHTML ( '<div>Hi</div>' ) ;
9
- expect ( await getHTML ( ) ) . toEqual ( '<div>Hi</div>' ) ;
10
+ expect ( await getHTML ( ) ) . toMatchInlineSnapshot ( `
11
+ "<head>
12
+ <meta charset="UTF-8">
13
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
14
+ <link rel="icon" href="data:;base64,=">
15
+ <title>pleasantest</title>
16
+ </head>
17
+ <body><div>Hi</div></body>"
18
+ ` ) ;
10
19
11
20
// It should fully override existing content
12
21
await utils . injectHTML ( '<div>Hiya</div>' ) ;
13
- expect ( await getHTML ( ) ) . toEqual ( '<div>Hiya</div>' ) ;
22
+ expect ( await getHTML ( ) ) . toMatchInlineSnapshot ( `
23
+ "<head>
24
+ <meta charset="UTF-8">
25
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
26
+ <link rel="icon" href="data:;base64,=">
27
+ <title>pleasantest</title>
28
+ </head>
29
+ <body><div>Hiya</div></body>"
30
+ ` ) ;
14
31
15
32
// Executes scripts by default
16
33
await utils . injectHTML ( `
17
34
<div>hello</div>
18
35
<script>document.querySelector('div').textContent = 'changed'</script>
19
36
` ) ;
20
37
expect ( await getHTML ( ) ) . toMatchInlineSnapshot ( `
21
- "<div>changed</div>
22
- <script>document.querySelector('div').textContent = 'changed'</script>"
38
+ "<head>
39
+ <meta charset="UTF-8">
40
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
41
+ <link rel="icon" href="data:;base64,=">
42
+ <title>pleasantest</title>
43
+ </head>
44
+ <body>
45
+ <div>changed</div>
46
+ <script>document.querySelector('div').textContent = 'changed'</script>
47
+ </body>"
23
48
` ) ;
24
49
25
50
// Can pass option to not execute
26
51
await utils . injectHTML (
27
52
`
28
53
<div>hello</div>
29
- <script>document.querySelector('div').textContent = 'changed'</script>
54
+ <script foo="bar" asdf >document.querySelector('div').textContent = 'changed'</script>
30
55
` ,
31
56
{ executeScriptTags : false } ,
32
57
) ;
33
58
expect ( await getHTML ( ) ) . toMatchInlineSnapshot ( `
34
- "<div>hello</div>
35
- <script>document.querySelector('div').textContent = 'changed'</script>"
59
+ "<head>
60
+ <meta charset="UTF-8">
61
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
62
+ <link rel="icon" href="data:;base64,=">
63
+ <title>pleasantest</title>
64
+ </head>
65
+ <body>
66
+ <div>hello</div>
67
+ <script foo="bar" asdf="">document.querySelector('div').textContent = 'changed'</script>
68
+ </body>"
69
+ ` ) ;
70
+
71
+ // Stuff in <head> should be left as-is and not re-executed after injectHTML is called again below
72
+ await page . evaluate ( ( ) => {
73
+ const script = document . createElement ( 'script' ) ;
74
+ script . text =
75
+ 'document.body.querySelector("div").innerHTML = "changed from script in head"' ;
76
+ document . head . append ( script ) ;
77
+ } ) ;
78
+
79
+ expect ( await getHTML ( ) ) . toMatchInlineSnapshot ( `
80
+ "<head>
81
+ <meta charset="UTF-8">
82
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
83
+ <link rel="icon" href="data:;base64,=">
84
+ <title>pleasantest</title>
85
+ <script>document.body.querySelector("div").innerHTML = "changed from script in head"</script></head>
86
+ <body>
87
+ <div>changed from script in head</div>
88
+ <script foo="bar" asdf="">document.querySelector('div').textContent = 'changed'</script>
89
+ </body>"
90
+ ` ) ;
91
+
92
+ await utils . injectHTML (
93
+ `
94
+ <div>injected HTML</div>
95
+ ` ,
96
+ ) ;
97
+
98
+ expect ( await getHTML ( ) ) . toMatchInlineSnapshot ( `
99
+ "<head>
100
+ <meta charset="UTF-8">
101
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
102
+ <link rel="icon" href="data:;base64,=">
103
+ <title>pleasantest</title>
104
+ <script>document.body.querySelector("div").innerHTML = "changed from script in head"</script></head>
105
+ <body>
106
+ <div>injected HTML</div>
107
+ </body>"
36
108
` ) ;
37
109
} ) ,
38
110
) ;
0 commit comments