Debian Codegraph @

www/public/test/core/internals.js

raw symbols SCIP
1 describe('Core htmx internals Tests', function() {
2 const chai = window.chai
3 beforeEach(function() {
4 this.server = makeServer()
5 clearWorkArea()
6 })
7 afterEach(function() {
8 this.server.restore()
9 clearWorkArea()
10 })
11
12 it('makeFragment works with janky stuff', function() {
13 htmx._('makeFragment')('<html></html>').children.length.should.equal(0)
14 htmx._('makeFragment')('<html><body></body></html>').children.length.should.equal(0)
15
16 // NB - the tag name should be the *parent* element hosting the HTML since we use the fragment children
17 // for the swap
18 htmx._('makeFragment')('<td></td>').firstElementChild.tagName.should.equal('TD')
19 htmx._('makeFragment')('<thead></thead>').firstElementChild.tagName.should.equal('THEAD')
20 htmx._('makeFragment')('<col></col>').firstElementChild.tagName.should.equal('COL')
21 htmx._('makeFragment')('<tr></tr>').firstElementChild.tagName.should.equal('TR')
22 })
23
24 it('makeFragment works with template wrapping', function() {
25 htmx._('makeFragment')('<html></html>').children.length.should.equal(0)
26 htmx._('makeFragment')('<html><body></body></html>').children.length.should.equal(0)
27
28 var fragment = htmx._('makeFragment')('<td></td>')
29 fragment.firstElementChild.tagName.should.equal('TD')
30
31 fragment = htmx._('makeFragment')('<thead></thead>')
32 fragment.firstElementChild.tagName.should.equal('THEAD')
33
34 fragment = htmx._('makeFragment')('<col></col>')
35 fragment.firstElementChild.tagName.should.equal('COL')
36
37 fragment = htmx._('makeFragment')('<tr></tr>')
38 fragment.firstElementChild.tagName.should.equal('TR')
39 })
40
41 it('makeFragment works with template wrapping and funky combos', function() {
42 htmx.config.useTemplateFragments = true
43 try {
44 var fragment = htmx._('makeFragment')('<td></td><div></div>')
45 fragment.children[0].tagName.should.equal('TD')
46 fragment.children[1].tagName.should.equal('DIV')
47 } finally {
48 htmx.config.useTemplateFragments = false
49 }
50 })
51
52 it('set header works with non-ASCII values', function() {
53 var xhr = new XMLHttpRequest()
54 xhr.open('GET', '/dummy')
55 htmx._('safelySetHeaderValue')(xhr, 'Example', 'привет')
56 // unfortunately I can't test the value :/
57 // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
58 })
59
60 it('handles parseInterval correctly', function() {
61 chai.expect(htmx.parseInterval('1ms')).to.be.equal(1)
62 chai.expect(htmx.parseInterval('300ms')).to.be.equal(300)
63 chai.expect(htmx.parseInterval('1s')).to.be.equal(1000)
64 chai.expect(htmx.parseInterval('1.5s')).to.be.equal(1500)
65 chai.expect(htmx.parseInterval('2s')).to.be.equal(2000)
66 chai.expect(htmx.parseInterval('0ms')).to.be.equal(0)
67 chai.expect(htmx.parseInterval('0s')).to.be.equal(0)
68 chai.expect(htmx.parseInterval('0m')).to.be.equal(0)
69 chai.expect(htmx.parseInterval('0')).to.be.equal(0)
70 chai.expect(htmx.parseInterval('5')).to.be.equal(5)
71
72 chai.expect(htmx.parseInterval(null)).to.be.undefined
73 chai.expect(htmx.parseInterval('')).to.be.undefined
74 chai.expect(htmx.parseInterval('undefined')).to.be.undefined
75 chai.expect(htmx.parseInterval('true')).to.be.undefined
76 chai.expect(htmx.parseInterval('false')).to.be.undefined
77 })
78
79 it('tokenizes correctly', function() {
80 chai.expect(htmx._('tokenizeString')('a,')).to.be.deep.equal(['a', ','])
81 chai.expect(htmx._('tokenizeString')('aa,')).to.be.deep.equal(['aa', ','])
82 chai.expect(htmx._('tokenizeString')('aa,aa')).to.be.deep.equal(['aa', ',', 'aa'])
83 chai.expect(htmx._('tokenizeString')('aa.aa')).to.be.deep.equal(['aa', '.', 'aa'])
84 })
85
86 it('tags respond correctly to shouldCancel', function() {
87 var anchorThatShouldCancel = make("<a href='/foo'></a>")
88 htmx._('shouldCancel')({ type: 'click' }, anchorThatShouldCancel).should.equal(true)
89
90 anchorThatShouldCancel = make("<a href='#'></a>")
91 htmx._('shouldCancel')({ type: 'click' }, anchorThatShouldCancel).should.equal(true)
92
93 var anchorThatShouldNotCancel = make("<a href='#foo'></a>")
94 htmx._('shouldCancel')({ type: 'click' }, anchorThatShouldNotCancel).should.equal(false)
95
96 var divThatShouldNotCancel = make('<div></div>')
97 htmx._('shouldCancel')({ type: 'click' }, divThatShouldNotCancel).should.equal(false)
98
99 var form = make('<form></form>')
100 htmx._('shouldCancel')({ type: 'submit', target: form }, form).should.equal(true)
101
102 // check that events targeting elements that shouldn't cancel don't cancel
103 htmx._('shouldCancel')({ type: 'click', target: divThatShouldNotCancel }, form).should.equal(false)
104
105 // check elements inside links getting click events should cancel parent links
106 var anchorWithButton = make("<a href='/foo'><button></button></a>")
107 htmx._('shouldCancel')({ type: 'click', target: anchorWithButton.firstChild }, anchorWithButton).should.equal(true)
108 htmx._('shouldCancel')({ type: 'click', target: anchorWithButton.firstChild }, anchorWithButton.firstChild).should.equal(true)
109
110 // check that links inside htmx elements should not cancel
111 var divWithLink = make("<div hx-get='/data'><a href='/page'>Link</a></div>")
112 var link = divWithLink.querySelector('a')
113 htmx._('shouldCancel')({ type: 'click', target: link }, divWithLink).should.equal(false)
114
115 form = make('<form id="f1">' +
116 '<input id="insideInput" type="submit">' +
117 '<button id="insideFormBtn"></button>' +
118 '<button id="insideSubmitBtn" type="submit"></button>' +
119 '<button id="insideResetBtn" type="reset"></button>' +
120 '<button id="insideButtonBtn" type="button"></button>' +
121 '</form>' +
122 '<input id="outsideInput" form="f1" type="submit">' +
123 '<button id="outsideFormBtn" form="f1"></button>' +
124 '<button id="outsideSubmitBtn" form="f1" type="submit"></button>")' +
125 '<button id="outsideButtonBtn" form="f1" type="button"></button>")' +
126 '<button id="outsideResetBtn" form="f1" type="reset"></button>")' +
127 '<button id="outsideNoFormBtn"></button>")')
128 htmx._('shouldCancel')({ type: 'click' }, byId('insideInput')).should.equal(true)
129 htmx._('shouldCancel')({ type: 'click' }, byId('insideFormBtn')).should.equal(true)
130 htmx._('shouldCancel')({ type: 'click' }, byId('insideSubmitBtn')).should.equal(true)
131 htmx._('shouldCancel')({ type: 'click' }, byId('insideResetBtn')).should.equal(false)
132 htmx._('shouldCancel')({ type: 'click' }, byId('insideButtonBtn')).should.equal(false)
133
134 htmx._('shouldCancel')({ type: 'click' }, byId('outsideInput')).should.equal(true)
135 htmx._('shouldCancel')({ type: 'click' }, byId('outsideFormBtn')).should.equal(true)
136 htmx._('shouldCancel')({ type: 'click' }, byId('outsideSubmitBtn')).should.equal(true)
137 htmx._('shouldCancel')({ type: 'click' }, byId('outsideButtonBtn')).should.equal(false)
138 htmx._('shouldCancel')({ type: 'click' }, byId('outsideResetBtn')).should.equal(false)
139 htmx._('shouldCancel')({ type: 'click' }, byId('outsideNoFormBtn')).should.equal(false)
140 })
141
142 it('unset properly unsets a given attribute', function() {
143 make("<div foo='1'><div foo='2'><div foo='unset' id='d1'></div></div></div>")
144 var div = byId('d1')
145 should.equal(undefined, htmx._('getClosestAttributeValue')(div, 'foo'))
146 })
147
148 it('unset properly unsets a given attribute on a parent', function() {
149 make("<div foo='1'><div foo='unset'><div id='d1'></div></div></div>")
150 var div = byId('d1')
151 should.equal(undefined, htmx._('getClosestAttributeValue')(div, 'foo'))
152 })
153
154 it('unset does not unset a value below it in the hierarchy', function() {
155 make("<div foo='unset'><div foo='2'><div id='d1'></div></div></div>")
156 var div = byId('d1')
157 should.equal('2', htmx._('getClosestAttributeValue')(div, 'foo'))
158 })
159
160 it('encoding values respects enctype on forms', function() {
161 var form = make("<form enctype='multipart/form-data'></form>")
162 var value = htmx._('encodeParamsForBody')(null, form, {});
163 (value instanceof FormData).should.equal(true)
164 })
165
166 it('Calling onpopstate to trigger backup and restore of page triggers htmx:restored event', function() {
167 var restored
168 var handler = htmx.on('htmx:restored', function(event) {
169 restored = true
170 })
171 make('<div hx-get="/test" hx-trigger="restored">Not Called</div>')
172 window.onpopstate({ state: { htmx: true } })
173 restored.should.equal(true)
174 htmx.off('htmx:restored', handler)
175 })
176
177 it('scroll position is restored from history restore', function() {
178 make('<div style="height: 1000px;" hx-get="/test" hx-trigger="restored">Not Called</div>')
179 window.scrollTo(0, 50)
180 window.onpopstate({ state: { htmx: true } })
181 parseInt(window.scrollY).should.equal(50)
182 })
183
184 it('calling onpopstate with no htmx state not true calls original popstate', function() {
185 window.onpopstate({ state: { htmx: false } })
186 })
187
188 it('getPathFromResponse returns paths when valid', function() {
189 var path = htmx._('getPathFromResponse')({ responseURL: 'https://htmx.org/somepath?a=b#fragment' })
190 should.equal(path, '/somepath?a=b')
191 path = htmx._('getPathFromResponse')({ responseURL: 'notvalidurl' })
192 should.equal(path, undefined)
193 })
194
195 it('appendParam can process objects', function() {
196 var param = htmx._('appendParam')('a=b', 'jim', 'foo')
197 should.equal(param, 'a=b&jim=foo')
198 param = htmx._('appendParam')('a=b', 'jim', '{"foo":"bar"}')
199 should.equal(param, 'a=b&jim=%7B%22foo%22%3A%22bar%22%7D')
200 param = htmx._('appendParam')('a=b', 'jim', { foo: 'bar' })
201 should.equal(param, 'a=b&jim=%7B%22foo%22%3A%22bar%22%7D')
202 })
203
204 it('handleTitle falls back to setting document.title when no title head element', function() {
205 var oldTitle = window.document.title
206 document.querySelector('title').remove()
207 htmx._('handleTitle')('update title')
208 window.document.title.should.equal('update title')
209 window.document.title = oldTitle
210 })
211
212 it('without meta config getMetaConfig returns null', function() {
213 document.querySelector('meta[name="htmx-config"]').remove()
214 should.equal(htmx._('getMetaConfig')(), null)
215 })
216
217 it('internalAPI settleImmediately completes settle tasks', function() {
218 // settleImmediately is no longer used internally and may no longer be needed at all
219 // as swapping without settleing does not seem via internalAPI
220 const fragment = htmx._('makeFragment')('<div>Content</div>')
221 const historyElement = htmx._('getHistoryElement')()
222 const settleInfo = htmx._('makeSettleInfo')(historyElement)
223 htmx._('swapInnerHTML')(historyElement, fragment, settleInfo)
224 historyElement.firstChild.className.should.equal('htmx-added')
225 htmx._('settleImmediately')(settleInfo.tasks)
226 historyElement.firstChild.className.should.equal('')
227 })
228 })