Debian Codegraph @

test/tests/unit/bootstrap.js

raw symbols SCIP
1 describe('bootstrap unit tests', function() {
2 it("Test that fragment parsing works as expected", function() {
3 var result = htmx.__makeFragment("foo");
4 var temp = document.createElement('div');
5 temp.appendChild(result.fragment.cloneNode(true));
6 assert.equal("foo", temp.textContent.trim())
7
8 // Test that template partials are preserved in fragment
9 result = htmx.__makeFragment(`<template partial hx-target="#test">foo</template>`);
10 temp = document.createElement('div');
11 temp.appendChild(result.fragment.cloneNode(true));
12 assert.include(temp.innerHTML, 'template')
13 })
14
15 it("__makeFragment handles multiple partials", function() {
16 var result = htmx.__makeFragment(`
17 <div>Main content</div>
18 <template partial hx-target="#test1">Partial 1</template>
19 <template partial hx-target="#test2" hx-swap="innerHTML">Partial 2</template>
20 `);
21 var temp = document.createElement('div');
22 temp.appendChild(result.fragment.cloneNode(true));
23 assert.include(temp.innerHTML, "Main content")
24 assert.include(temp.innerHTML, "template")
25 })
26
27 it("__makeFragment extracts title from HTML", function() {
28 var result = htmx.__makeFragment(`
29 <html><head><title>Test Title</title></head><body>Content</body></html>
30 `);
31 assert.equal("Test Title", result.title)
32 })
33
34 it("__makeFragment handles body tag response", function() {
35 var result = htmx.__makeFragment(`<body><div>Content</div></body>`);
36 var temp = document.createElement('div');
37 temp.appendChild(result.fragment.cloneNode(true));
38 assert.include(temp.innerHTML, "Content")
39 })
40
41 it("__makeFragment handles fragment response", function() {
42 var result = htmx.__makeFragment(`<div>Fragment</div><span>More</span>`);
43 var temp = document.createElement('div');
44 temp.appendChild(result.fragment.cloneNode(true));
45 assert.include(temp.innerHTML, "Fragment")
46 assert.include(temp.innerHTML, "More")
47 })
48
49 it("__attributeValue returns direct attribute value", function() {
50 const div = createDisconnectedHTML('<div hx-get="/test"></div>');
51 const result = htmx.__attributeValue(div, 'hx-get', 'default');
52 assert.equal(result, '/test');
53 })
54
55 it("__attributeValue returns inherited attribute from element", function() {
56 const div = createDisconnectedHTML('<div hx-get:inherited="/inherited"></div>');
57 const result = htmx.__attributeValue(div, 'hx-get', 'default');
58 assert.equal(result, '/inherited');
59 })
60
61 it("__attributeValue prefers direct attribute over inherited", function() {
62 const div = createDisconnectedHTML('<div hx-get="/direct" hx-get:inherited="/inherited"></div>');
63 const result = htmx.__attributeValue(div, 'hx-get', 'default');
64 assert.equal(result, '/direct');
65 })
66
67 it("__attributeValue finds inherited attribute on parent", function() {
68 const parent = createDisconnectedHTML('<div hx-get:inherited="/parent"><div></div></div>');
69 const child = parent.firstElementChild;
70 const result = htmx.__attributeValue(child, 'hx-get', 'default');
71 assert.equal(result, '/parent');
72 })
73
74 it("__attributeValue returns default when attribute not found", function() {
75 const div = createDisconnectedHTML('<div></div>');
76 const result = htmx.__attributeValue(div, 'hx-get', 'default');
77 assert.equal(result, 'default');
78 })
79
80 it("__parseTriggerSpecs parses simple event", function() {
81 const result = htmx.__parseTriggerSpecs('click');
82
83 assert.equal(result.length, 1);
84 assert.equal(result[0].name, 'click');
85 })
86
87 it("__parseTriggerSpecs parses event with option", function() {
88 const result = htmx.__parseTriggerSpecs('click delay:500');
89
90 assert.equal(result.length, 1);
91 assert.equal(result[0].name, 'click');
92 assert.equal(result[0].delay, '500');
93 })
94
95 it("__parseTriggerSpecs parses event with multiple options", function() {
96 const result = htmx.__parseTriggerSpecs('click delay:500 throttle:100');
97
98 assert.equal(result.length, 1);
99 assert.equal(result[0].name, 'click');
100 assert.equal(result[0].delay, '500');
101 assert.equal(result[0].throttle, '100');
102 })
103
104 it("__parseTriggerSpecs parses event with boolean opts", function() {
105 const result = htmx.__parseTriggerSpecs('click once changed');
106
107 assert.equal(result.length, 1);
108 assert.equal(result[0].name, 'click');
109 assert.equal(result[0].once, true);
110 assert.equal(result[0].changed, true);
111 })
112
113 it("__parseTriggerSpecs parses event with options and boolean opts", function() {
114 const result = htmx.__parseTriggerSpecs('click delay:1s once changed');
115
116 assert.equal(result.length, 1);
117 assert.equal(result[0].name, 'click');
118 assert.equal(result[0].delay, '1s');
119 assert.equal(result[0].once, true);
120 assert.equal(result[0].changed, true);
121 })
122
123 it("__parseTriggerSpecs parses multiple events", function() {
124 const result = htmx.__parseTriggerSpecs('click, submit');
125
126 assert.equal(result.length, 2);
127 assert.equal(result[0].name, 'click');
128 assert.equal(result[1].name, 'submit');
129 })
130
131 it("__parseTriggerSpecs parses multiple events with options", function() {
132 const result = htmx.__parseTriggerSpecs('click delay:500, keyup changed');
133
134 assert.equal(result.length, 2);
135 assert.equal(result[0].name, 'click');
136 assert.equal(result[0].delay, '500');
137 assert.equal(result[1].name, 'keyup');
138 assert.equal(result[1].changed, true);
139 })
140
141 it("__parseTriggerSpecs parses event filter", function() {
142 const result = htmx.__parseTriggerSpecs('click[ctrlKey]');
143
144 assert.equal(result.length, 1);
145 assert.equal(result[0].name, 'click[ctrlKey]');
146 })
147
148 it("__parseTriggerSpecs parses event filter with spaces", function() {
149 const result = htmx.__parseTriggerSpecs('click[target.value == "test"]');
150
151 assert.equal(result.length, 1);
152 assert.equal(result[0].name, 'click[target.value == "test"]');
153 })
154
155 it("__parseTriggerSpecs parses event with from option", function() {
156 const result = htmx.__parseTriggerSpecs('click from:body');
157
158 assert.equal(result.length, 1);
159 assert.equal(result[0].name, 'click');
160 assert.equal(result[0].from, 'body');
161 })
162
163 it("__parseTriggerSpecs throws on unterminated filter", function() {
164 assert.throws(() => {
165 htmx.__parseTriggerSpecs('click[ctrlKey');
166 }, /unterminated/);
167 })
168
169 it("__parseTriggerSpecs handles complex real-world spec", function() {
170 const result = htmx.__parseTriggerSpecs('keyup[target.value.length > 3] changed delay:500ms from:input');
171
172 assert.equal(result.length, 1);
173 assert.equal(result[0].name, 'keyup[target.value.length > 3]');
174 assert.equal(result[0].changed, true);
175 assert.equal(result[0].delay, '500ms');
176 assert.equal(result[0].from, 'input');
177 })
178
179 it("__parseTriggerSpecs handles complex real-world spec w string and preserves spaces in string", function() {
180 const result = htmx.__parseTriggerSpecs('keyup[target.value == "hello world"] changed delay:500ms from:input');
181
182 assert.equal(result.length, 1);
183 assert.equal(result[0].name, 'keyup[target.value == "hello world"]');
184 assert.equal(result[0].changed, true);
185 assert.equal(result[0].delay, '500ms');
186 assert.equal(result[0].from, 'input');
187 })
188
189 it("public API surface remains stable", function() {
190 // This test ensures the public API doesn't accidentally change
191 const expectedPublicMethods = [
192 'ajax',
193 'find',
194 'findAll',
195 'on',
196 'onLoad',
197 'parseInterval',
198 'process',
199 'swap',
200 'timeout',
201 'registerExtension',
202 'trigger',
203 ].sort();
204
205 const expectedPublicProperties = [
206 'config',
207 'version',
208 ].sort();
209
210 // Get own properties (like config, eventSource)
211 const ownProperties = Object.keys(htmx).filter(name => !name.startsWith("_") && typeof htmx[name] !== 'function').sort();
212
213 // Get methods from the prototype
214 const proto = Object.getPrototypeOf(htmx);
215 const protoMethods = Object.getOwnPropertyNames(proto)
216 .filter(name => !name.startsWith("_") && name !== 'constructor' && typeof htmx[name] === 'function')
217 .sort();
218
219 // Check methods
220 assert.deepEqual(protoMethods, expectedPublicMethods,
221 'Public methods have changed. Expected: ' + JSON.stringify(expectedPublicMethods) +
222 ', Got: ' + JSON.stringify(protoMethods));
223
224 // Check properties
225 assert.deepEqual(ownProperties, expectedPublicProperties,
226 'Public properties have changed. Expected: ' + JSON.stringify(expectedPublicProperties) +
227 ', Got: ' + JSON.stringify(ownProperties));
228 })
229 })