@@ -3,36 +3,108 @@ import { withBrowser } from 'pleasantest';
33test (
44 'injectHTML' ,
55 withBrowser ( async ( { utils, page } ) => {
6- const getHTML = ( ) => page . evaluate ( ( ) => document . body . innerHTML . trim ( ) ) ;
6+ const getHTML = ( ) =>
7+ page . evaluate ( ( ) => document . documentElement . innerHTML . trim ( ) ) ;
78
89 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+ ` ) ;
1019
1120 // It should fully override existing content
1221 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+ ` ) ;
1431
1532 // Executes scripts by default
1633 await utils . injectHTML ( `
1734 <div>hello</div>
1835 <script>document.querySelector('div').textContent = 'changed'</script>
1936 ` ) ;
2037 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>"
2348 ` ) ;
2449
2550 // Can pass option to not execute
2651 await utils . injectHTML (
2752 `
2853 <div>hello</div>
29- <script>document.querySelector('div').textContent = 'changed'</script>
54+ <script foo="bar" asdf >document.querySelector('div').textContent = 'changed'</script>
3055 ` ,
3156 { executeScriptTags : false } ,
3257 ) ;
3358 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>"
36108 ` ) ;
37109 } ) ,
38110) ;
0 commit comments