|
27 | 27 | function make(htmlStr) { |
28 | 28 | let range = document.createRange(); |
29 | 29 | let fragment = range.createContextualFragment(htmlStr); |
| 30 | + let wa = getWorkArea(); |
30 | 31 | for (let i = fragment.children.length - 1; i >= 0; i--) { |
31 | 32 | const child = fragment.children[i]; |
32 | 33 | HTMx.processElement(child); |
33 | | - document.body.appendChild(child); |
| 34 | + wa.appendChild(child); |
34 | 35 | } |
35 | | - return document.body.lastChild; |
| 36 | + return wa.lastChild; |
| 37 | + } |
| 38 | + |
| 39 | + function getWorkArea() { |
| 40 | + return byId("work-area"); |
| 41 | + } |
| 42 | + |
| 43 | + function clearWorkArea() { |
| 44 | + getWorkArea().innerHTML = ""; |
36 | 45 | } |
37 | 46 | </script> |
38 | 47 |
|
39 | 48 | <script> |
40 | 49 | describe("Core HTMx Tests", function(){ |
41 | 50 | beforeEach(function() { |
42 | 51 | this.server = sinon.fakeServer.create(); |
| 52 | + clearWorkArea(); |
43 | 53 | }); |
44 | 54 | afterEach(function() { |
45 | 55 | this.server.restore(); |
| 56 | + clearWorkArea(); |
46 | 57 | }); |
47 | 58 |
|
48 | 59 | // bootstrap test |
49 | 60 | it('issues a GET request on click and swaps content', function() |
50 | 61 | { |
51 | | - this.server.respondWith("GET", "/core_test1", "Clicked!"); |
| 62 | + this.server.respondWith("GET", "/test", "Clicked!"); |
52 | 63 |
|
53 | | - let btn = make('<button hx-get="/core_test1">Click Me!</button>') |
| 64 | + let btn = make('<button hx-get="/test">Click Me!</button>') |
54 | 65 | btn.click(); |
55 | 66 | this.server.respond(); |
56 | 67 | btn.innerHTML.should.equal("Clicked!"); |
57 | 68 | }); |
58 | 69 |
|
59 | 70 | it('processes inner content properly', function() |
60 | 71 | { |
61 | | - this.server.respondWith("GET", "/core_test2", '<a hx-get="/core_test2_2">Click Me</a>'); |
62 | | - this.server.respondWith("GET", "/core_test2_2", "Clicked!"); |
| 72 | + this.server.respondWith("GET", "/test", '<a hx-get="/test2">Click Me</a>'); |
| 73 | + this.server.respondWith("GET", "/test2", "Clicked!"); |
63 | 74 |
|
64 | | - let div = make('<div hx-get="/core_test2"></div>') |
| 75 | + let div = make('<div hx-get="/test"></div>') |
65 | 76 | div.click(); |
66 | 77 | this.server.respond(); |
67 | | - div.innerHTML.should.equal('<a hx-get="/core_test2_2">Click Me</a>'); |
| 78 | + div.innerHTML.should.equal('<a hx-get="/test2">Click Me</a>'); |
68 | 79 | let a = div.querySelector('a'); |
69 | 80 | a.click(); |
70 | 81 | this.server.respond(); |
|
73 | 84 |
|
74 | 85 | it('handles swap outerHTML properly', function() |
75 | 86 | { |
76 | | - this.server.respondWith("GET", "/core_test3", '<a id="a-outer-html-test" hx-get="/core_test3_2">Click Me</a>'); |
77 | | - this.server.respondWith("GET", "/core_test3_2", "Clicked!"); |
| 87 | + this.server.respondWith("GET", "/test", '<a id="a1" hx-get="/test2">Click Me</a>'); |
| 88 | + this.server.respondWith("GET", "/test2", "Clicked!"); |
78 | 89 |
|
79 | | - let div = make('<div id="div-outer-html-test" hx-get="/core_test3" hx-swap="outerHTML"></div>') |
| 90 | + let div = make('<div id="d1" hx-get="/test" hx-swap="outerHTML"></div>') |
80 | 91 | div.click(); |
81 | | - should.equal(byId("div-outer-html-test"), div); |
| 92 | + should.equal(byId("d1"), div); |
82 | 93 | this.server.respond(); |
83 | | - should.equal(byId("div-outer-html-test"), null); |
84 | | - byId("a-outer-html-test").click(); |
| 94 | + should.equal(byId("d1"), null); |
| 95 | + byId("a1").click(); |
85 | 96 | this.server.respond(); |
86 | | - byId("a-outer-html-test").innerHTML.should.equal('Clicked!'); |
| 97 | + byId("a1").innerHTML.should.equal('Clicked!'); |
87 | 98 | }); |
88 | 99 |
|
89 | 100 | it('handles prepend properly', function() |
90 | 101 | { |
91 | 102 | let i = 0; |
92 | | - this.server.respondWith("GET", "/core_test4", function(xhr){ |
| 103 | + this.server.respondWith("GET", "/test", function(xhr){ |
93 | 104 | i++; |
94 | | - xhr.respond(200, {}, '<a id="a-prepend-test-' + i + '" hx-get="/core_test4_2" hx-swap="innerHTML">' + i + '</a>'); |
| 105 | + xhr.respond(200, {}, '<a id="a' + i + '" hx-get="/test2" hx-swap="innerHTML">' + i + '</a>'); |
95 | 106 | }); |
96 | | - this.server.respondWith("GET", "/core_test4_2", "*"); |
| 107 | + this.server.respondWith("GET", "/test2", "*"); |
97 | 108 |
|
98 | | - let div = make('<div hx-get="/core_test4" hx-swap="prepend">*</div>') |
| 109 | + let div = make('<div hx-get="/test" hx-swap="prepend">*</div>') |
99 | 110 | div.click(); |
100 | 111 | this.server.respond(); |
101 | 112 | div.innerText.should.equal("1*"); |
102 | 113 |
|
103 | | - byId("a-prepend-test-1").click(); |
| 114 | + byId("a1").click(); |
104 | 115 | this.server.respond(); |
105 | 116 | div.innerText.should.equal("**"); |
106 | 117 |
|
107 | 118 | div.click(); |
108 | 119 | this.server.respond(); |
109 | 120 | div.innerText.should.equal("2**"); |
110 | 121 |
|
111 | | - byId("a-prepend-test-2").click(); |
| 122 | + byId("a2").click(); |
112 | 123 | this.server.respond(); |
113 | 124 | div.innerText.should.equal("***"); |
114 | 125 | }); |
115 | 126 |
|
| 127 | + it('handles prepend properly with no initial content', function() |
| 128 | + { |
| 129 | + let i = 0; |
| 130 | + this.server.respondWith("GET", "/test", function(xhr){ |
| 131 | + i++; |
| 132 | + xhr.respond(200, {}, '<a id="a' + i + '" hx-get="/test2" hx-swap="innerHTML">' + i + '</a>'); |
| 133 | + }); |
| 134 | + this.server.respondWith("GET", "/test2", "*"); |
| 135 | + |
| 136 | + let div = make('<div hx-get="/test" hx-swap="prepend"></div>') |
| 137 | + div.click(); |
| 138 | + this.server.respond(); |
| 139 | + div.innerText.should.equal("1"); |
| 140 | + |
| 141 | + byId("a1").click(); |
| 142 | + this.server.respond(); |
| 143 | + div.innerText.should.equal("*"); |
| 144 | + |
| 145 | + div.click(); |
| 146 | + this.server.respond(); |
| 147 | + div.innerText.should.equal("2*"); |
| 148 | + |
| 149 | + byId("a2").click(); |
| 150 | + this.server.respond(); |
| 151 | + div.innerText.should.equal("**"); |
| 152 | + }); |
| 153 | + |
116 | 154 | it('handles append properly', function() |
117 | 155 | { |
118 | 156 | let i = 0; |
119 | | - this.server.respondWith("GET", "/core_test5", function(xhr){ |
| 157 | + this.server.respondWith("GET", "/test", function(xhr){ |
120 | 158 | i++; |
121 | | - xhr.respond(200, {}, '<a id="a-append-test-' + i + '" hx-get="/core_test5_2" hx-swap="innerHTML">' + i + '</a>'); |
| 159 | + xhr.respond(200, {}, '<a id="a' + i + '" hx-get="/test2" hx-swap="innerHTML">' + i + '</a>'); |
122 | 160 | }); |
123 | | - this.server.respondWith("GET", "/core_test5_2", "*"); |
| 161 | + this.server.respondWith("GET", "/test2", "*"); |
124 | 162 |
|
125 | | - let div = make('<div hx-get="/core_test5" hx-swap="append">*</div>') |
| 163 | + let div = make('<div hx-get="/test" hx-swap="append">*</div>') |
126 | 164 | div.click(); |
127 | 165 | this.server.respond(); |
128 | 166 | div.innerText.should.equal("*1"); |
129 | 167 |
|
130 | | - byId("a-append-test-1").click(); |
| 168 | + byId("a1").click(); |
131 | 169 | this.server.respond(); |
132 | 170 | div.innerText.should.equal("**"); |
133 | 171 |
|
134 | 172 | div.click(); |
135 | 173 | this.server.respond(); |
136 | 174 | div.innerText.should.equal("**2"); |
137 | 175 |
|
138 | | - byId("a-append-test-2").click(); |
| 176 | + byId("a2").click(); |
139 | 177 | this.server.respond(); |
140 | 178 | div.innerText.should.equal("***"); |
141 | 179 | }); |
142 | 180 |
|
| 181 | + it('handles append properly with no initial content', function() |
| 182 | + { |
| 183 | + let i = 0; |
| 184 | + this.server.respondWith("GET", "/test", function(xhr){ |
| 185 | + i++; |
| 186 | + xhr.respond(200, {}, '<a id="a' + i + '" hx-get="/test2" hx-swap="innerHTML">' + i + '</a>'); |
| 187 | + }); |
| 188 | + this.server.respondWith("GET", "/test2", "*"); |
| 189 | + |
| 190 | + let div = make('<div hx-get="/test" hx-swap="append"></div>') |
| 191 | + div.click(); |
| 192 | + this.server.respond(); |
| 193 | + div.innerText.should.equal("1"); |
| 194 | + |
| 195 | + byId("a1").click(); |
| 196 | + this.server.respond(); |
| 197 | + div.innerText.should.equal("*"); |
| 198 | + |
| 199 | + div.click(); |
| 200 | + this.server.respond(); |
| 201 | + div.innerText.should.equal("*2"); |
| 202 | + |
| 203 | + byId("a2").click(); |
| 204 | + this.server.respond(); |
| 205 | + div.innerText.should.equal("**"); |
| 206 | + }); |
| 207 | + |
| 208 | + it('handles hx-target properly', function() |
| 209 | + { |
| 210 | + this.server.respondWith("GET", "/test", "Clicked!"); |
| 211 | + |
| 212 | + let btn = make('<button hx-get="/test" hx-target="#s1">Click Me!</button>'); |
| 213 | + let target = make('<span id="s1">Initial</span>'); |
| 214 | + btn.click(); |
| 215 | + target.innerHTML.should.equal("Initial"); |
| 216 | + this.server.respond(); |
| 217 | + target.innerHTML.should.equal("Clicked!"); |
| 218 | + }); |
| 219 | + |
| 220 | + it('handles 204 NO CONTENT responses properly', function() |
| 221 | + { |
| 222 | + this.server.respondWith("GET", "/test", [204, {}, "No Content!"]); |
| 223 | + |
| 224 | + let btn = make('<button hx-get="/test">Click Me!</button>'); |
| 225 | + btn.click(); |
| 226 | + btn.innerHTML.should.equal("Click Me!"); |
| 227 | + this.server.respond(); |
| 228 | + btn.innerHTML.should.equal("Click Me!"); |
| 229 | + }); |
| 230 | + |
| 231 | + it('handles hx-trigger with non-default value', function() |
| 232 | + { |
| 233 | + this.server.respondWith("GET", "/test", "Focused!"); |
| 234 | + |
| 235 | + let btn = make('<button hx-get="/test" hx-trigger="focus">Focus Me!</button>'); |
| 236 | + btn.focus(); |
| 237 | + btn.innerHTML.should.equal("Focus Me!"); |
| 238 | + this.server.respond(); |
| 239 | + btn.innerHTML.should.equal("Focused!"); |
| 240 | + }); |
| 241 | + |
143 | 242 | }) |
144 | 243 | </script> |
145 | 244 |
|
|
148 | 247 | </script> |
149 | 248 | <em>Work Area</em> |
150 | 249 | <hr/> |
| 250 | +<div id="work-area"> |
| 251 | + |
| 252 | +</div> |
151 | 253 | </body> |
152 | 254 | </html> |
0 commit comments