summaryrefslogtreecommitdiffstatshomepage
path: root/www/static/test/attributes/hx-push-url.js
blob: cac497aaeb2d8c0b026c7bf8fcdefc7e5f87a9a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
describe('hx-push-url attribute', function() {
  const chai = window.chai
  var HTMX_HISTORY_CACHE_NAME = 'htmx-history-cache'

  beforeEach(function() {
    this.server = makeServer()
    clearWorkArea()
    sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
  })
  afterEach(function() {
    this.server.restore()
    clearWorkArea()
    sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME)
  })

  it('navigation should push an element into the cache when true', function() {
    this.server.respondWith('GET', '/test', 'second')
    getWorkArea().innerHTML.should.be.equal('')
    var div = make('<div hx-push-url="true" hx-get="/test">first</div>')
    div.click()
    this.server.respond()
    div.click()
    this.server.respond()
    getWorkArea().textContent.should.equal('second')
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache[cache.length - 1].url.should.equal('/test')
  })

  it('navigation should not push an element into the cache when false', function() {
    this.server.respondWith('GET', '/test', 'second')
    getWorkArea().innerHTML.should.be.equal('')
    var div = make('<div hx-push-url="false" hx-get="/test">first</div>')
    div.click()
    this.server.respond()
    div.click()
    this.server.respond()
    getWorkArea().textContent.should.equal('second')
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    should.equal(cache, null)
  })

  it('navigation should push an element into the cache when string', function() {
    this.server.respondWith('GET', '/test', 'second')
    getWorkArea().innerHTML.should.be.equal('')
    var div = make('<div hx-push-url="abc123" hx-get="/test">first</div>')
    div.click()
    this.server.respond()
    div.click()
    this.server.respond()
    getWorkArea().textContent.should.equal('second')
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(2)
    cache[1].url.should.equal('/abc123')
  })

  it('restore should return old value', function() {
    this.server.respondWith('GET', '/test1', '<div id="d2" hx-push-url="true" hx-get="/test2" hx-swap="outerHTML settle:0">test1</div>')
    this.server.respondWith('GET', '/test2', '<div id="d3" hx-push-url="true" hx-get="/test3" hx-swap="outerHTML settle:0">test2</div>')

    make('<div id="d1" hx-push-url="true" hx-get="/test1" hx-swap="outerHTML settle:0">init</div>')

    byId('d1').click()
    this.server.respond()
    var workArea = getWorkArea()
    workArea.textContent.should.equal('test1')

    byId('d2').click()
    this.server.respond()
    workArea.textContent.should.equal('test2')

    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))

    cache.length.should.equal(2)
    htmx._('restoreHistory')('/test1')
    getWorkArea().textContent.should.equal('test1')
  })

  it('history restore should not have htmx support classes in content', function() {
    this.server.respondWith('GET', '/test1', '<div id="d2" hx-push-url="true" hx-get="/test2" hx-swap="outerHTML settle:0">test1</div>')
    this.server.respondWith('GET', '/test2', '<div id="d3" hx-push-url="true" hx-get="/test3" hx-swap="outerHTML settle:0">test2</div>')

    make('<div id="d1" hx-push-url="true" hx-get="/test1" hx-swap="outerHTML settle:0">init</div>')

    byId('d1').click()
    this.server.respond()
    var workArea = getWorkArea()
    workArea.textContent.should.equal('test1')

    byId('d2').click()
    this.server.respond()
    workArea.textContent.should.equal('test2')

    htmx._('restoreHistory')('/test1')
    getWorkArea().getElementsByClassName('htmx-request').length.should.equal(0)
  })

  it('cache should only store 10 entries', function() {
    var x = 0
    this.server.respondWith('GET', /test.*/, function(xhr) {
      x++
      xhr.respond(200, {}, '<div id="d1" hx-push-url="true" hx-get="/test' + x + '" hx-swap="outerHTML settle:0"></div>')
    })
    getWorkArea().innerHTML.should.be.equal('')
    make('<div id="d1" hx-push-url="true" hx-get="/test" hx-swap="outerHTML settle:0"></div>')
    for (var i = 0; i < 20; i++) { // issue 20 requests
      byId('d1').click()
      this.server.respond()
    }
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(10) // should only be 10 elements
  })

  it('cache miss should issue another GET', function() {
    this.server.respondWith('GET', '/test1', '<div id="d2" hx-push-url="true" hx-get="/test2" hx-swap="outerHTML settle:0">test1</div>')
    this.server.respondWith('GET', '/test2', '<div id="d3" hx-push-url="true" hx-get="/test3" hx-swap="outerHTML settle:0">test2</div>')

    make('<div id="d1" hx-push-url="true" hx-get="/test1" hx-swap="outerHTML settle:0">init</div>')

    byId('d1').click()
    this.server.respond()
    var workArea = getWorkArea()
    workArea.textContent.should.equal('test1')

    byId('d2').click()
    this.server.respond()
    workArea.textContent.should.equal('test2')

    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))

    cache.length.should.equal(2)
    sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
    htmx._('restoreHistory')('/test1')
    this.server.respond()
    getWorkArea().textContent.should.equal('test1')
  })

  it('cache miss should refresh when refreshOnHistoryMiss true', function() {
    htmx.config.refreshOnHistoryMiss = true
    var refresh = false
    htmx.location = { reload: function() { refresh = true } }
    sessionStorage.removeItem(HTMX_HISTORY_CACHE_NAME) // clear cache
    htmx._('restoreHistory')('/test3')
    refresh.should.equal(true)
    htmx.location = window.location
    htmx.config.refreshOnHistoryMiss = false
  })

  it('navigation should push an element into the cache  w/ data-* prefix', function() {
    this.server.respondWith('GET', '/test', 'second')
    getWorkArea().innerHTML.should.be.equal('')
    var div = make('<div data-hx-push-url="true" data-hx-get="/test">first</div>')
    div.click()
    this.server.respond()
    getWorkArea().textContent.should.equal('second')
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(1)
  })

  it('deals with malformed JSON in history cache when getting', function() {
    sessionStorage.setItem(HTMX_HISTORY_CACHE_NAME, 'Invalid JSON')
    var history = htmx._('getCachedHistory')('url')
    should.equal(history, null)
  })

  it('deals with malformed JSON in history cache when saving', function() {
    sessionStorage.setItem(HTMX_HISTORY_CACHE_NAME, 'Invalid JSON')
    htmx._('saveToHistoryCache')('url', make('<div>'))
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(1)
  })

  it('does not blow out cache when saving a URL twice', function() {
    htmx._('saveToHistoryCache')('url1', make('<div>'))
    htmx._('saveToHistoryCache')('url2', make('<div>'))
    htmx._('saveToHistoryCache')('url3', make('<div>'))
    htmx._('saveToHistoryCache')('url2', make('<div>'))
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(3)
  })

  it('setting history cache size to 0 clears cache', function() {
    htmx._('saveToHistoryCache')('url1', make('<div>'))
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(1)
    htmx.config.historyCacheSize = 0
    htmx._('saveToHistoryCache')('url2', make('<div>'))
    cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    should.equal(cache, null)
    htmx.config.historyCacheSize = 10
  })

  it('history cache is LRU', function() {
    htmx._('saveToHistoryCache')('url1', make('<div>'))
    htmx._('saveToHistoryCache')('url2', make('<div>'))
    htmx._('saveToHistoryCache')('url3', make('<div>'))
    htmx._('saveToHistoryCache')('url2', make('<div>'))
    htmx._('saveToHistoryCache')('url1', make('<div>'))
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(3)
    cache[0].url.should.equal('/url3')
    cache[1].url.should.equal('/url2')
    cache[2].url.should.equal('/url1')
  })

  it('htmx:afterSettle is called when replacing outerHTML', function() {
    var called = false
    var handler = htmx.on('htmx:afterSettle', function(evt) {
      called = true
    })
    try {
      this.server.respondWith('POST', '/test', function(xhr) {
        xhr.respond(200, {}, '<button>Bar</button>')
      })
      var div = make("<button hx-post='/test' hx-swap='outerHTML'>Foo</button>")
      div.click()
      this.server.respond()
      should.equal(called, true)
    } finally {
      htmx.off('htmx:afterSettle', handler)
    }
  })

  it('should include parameters on a get', function() {
    var path = ''
    var handler = htmx.on('htmx:pushedIntoHistory', function(evt) {
      path = evt.detail.path
    })
    try {
      this.server.respondWith('GET', /test.*/, function(xhr) {
        xhr.respond(200, {}, 'second')
      })
      var form = make('<form hx-trigger="click" hx-push-url="true" hx-get="/test"><input type="hidden" name="foo" value="bar"/>first</form>')
      form.click()
      this.server.respond()
      form.textContent.should.equal('second')
      path.should.equal('/test?foo=bar')
    } finally {
      htmx.off('htmx:pushedIntoHistory', handler)
    }
  })

  it('saveToHistoryCache should not throw', function() {
    this.timeout(4000)
    var bigContent = 'Dummy'
    for (var i = 0; i < 20; i++) {
      bigContent += bigContent
    }
    try {
      sessionStorage.removeItem('htmx-history-cache')
      htmx._('saveToHistoryCache')('/dummy', make('<div>' + bigContent + '</div>'), 'Foo', 0)
      should.equal(sessionStorage.getItem('htmx-history-cache'), null)
    } finally {
      // clear history cache afterwards
      sessionStorage.removeItem('htmx-history-cache')
    }
  })

  if (/chrome/i.test(navigator.userAgent)) {
    it('when sessionStorage disabled history not saved fine', function() {
      var setItem = sessionStorage.setItem
      sessionStorage.setItem = undefined
      this.server.respondWith('GET', '/test', 'second')
      getWorkArea().innerHTML.should.be.equal('')
      var div = make('<div hx-push-url="true" hx-get="/test">first</div>')
      div.click()
      this.server.respond()
      div.click()
      this.server.respond()
      getWorkArea().textContent.should.equal('second')
      var hist = htmx._('getCachedHistory')('/test')
      should.equal(hist, null)
      sessionStorage.setItem = setItem
    })
  }

  it.skip('normalizePath falls back to no normalization if path not valid URL', function() {
    // path normalization has a bug breaking it right now preventing this test
    htmx._('saveToHistoryCache')('http://', make('<div>'))
    htmx._('saveToHistoryCache')('http//', make('<div>'))
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(2)
    cache[0].url.should.equal('http://') // no normalization as invalid
    cache[1].url.should.equal('/http') // can normalize this one
  })

  it('history cache clears out disabled attribute', function() {
    htmx._('saveToHistoryCache')('/url1', make('<div><div data-disabled-by-htmx disabled></div></div>'))
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(1)
    cache[0].url.should.equal('/url1')
    cache[0].content.should.equal('<div data-disabled-by-htmx=""></div>')
  })

  it('ensure cache-busting parameter not pushed to history url', function() {
    this.server.respondWith('GET', /\/test.*/, function(xhr) {
      getParameters(xhr)['org.htmx.cache-buster'].should.equal('foo')
      xhr.respond(200, {}, 'Clicked!')
    })

    try {
      htmx.config.getCacheBusterParam = true
      var btn = make('<button hx-push-url="true" hx-get="/test" id="foo">Click Me!</button>')
      btn.click()
      this.server.respond()
      btn.innerHTML.should.equal('Clicked!')
    } finally {
      htmx.config.getCacheBusterParam = false
    }
    sessionStorage.getItem('htmx-current-path-for-history').should.equal('/test')
  })

  it('ensure history pushState called', function() {
    if (!byId('mocha')) { // This test does not work in browser using mocha
      this.server.respondWith('GET', /\/test.*/, function(xhr) {
        xhr.respond(200, {}, 'Clicked!')
      })

      try {
        htmx.config.historyEnabled = true
        var btn = make('<button hx-push-url="true" hx-get="/test" id="foo">Click Me!</button>')
        btn.click()
        this.server.respond()
        btn.innerHTML.should.equal('Clicked!')
      } finally {
        htmx.config.historyEnabled = false
      }
    }
  })

  it('should handle HX-Push response header', function() {
    var path
    var handler = htmx.on('htmx:pushedIntoHistory', function(event) {
      path = event.detail.path
    })
    this.server.respondWith('GET', '/test', [200, { 'HX-Push': '/pushpath' }, 'Result'])
    var div1 = make('<div id="d1" hx-get="/test"></div>')
    div1.click()
    this.server.respond()
    div1.innerHTML.should.equal('Result')
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(1)
    path.should.equal('/pushpath')
    htmx.off('htmx:pushedIntoHistory', handler)
  })

  it('should handle HX-Push-Url response header', function() {
    var path
    var handler = htmx.on('htmx:pushedIntoHistory', function(event) {
      path = event.detail.path
    })
    this.server.respondWith('GET', '/test', [200, { 'HX-Push-Url': '/pushpath' }, 'Result'])
    var div1 = make('<div id="d1" hx-get="/test"></div>')
    div1.click()
    this.server.respond()
    div1.innerHTML.should.equal('Result')
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    cache.length.should.equal(1)
    path.should.equal('/pushpath')
    htmx.off('htmx:pushedIntoHistory', handler)
  })

  it('should ignore HX-Push-Url=false response header', function() {
    var path = ''
    var handler = htmx.on('htmx:pushedIntoHistory', function(event) {
      path = event.detail.path
    })
    this.server.respondWith('GET', '/test', [200, { 'HX-Push-Url': 'false' }, 'Result'])
    var div1 = make('<div id="d1" hx-get="/test"></div>')
    div1.click()
    this.server.respond()
    div1.innerHTML.should.equal('Result')
    var cache = JSON.parse(sessionStorage.getItem(HTMX_HISTORY_CACHE_NAME))
    should.equal(cache, null)
    path.should.equal('')
    htmx.off('htmx:pushedIntoHistory', handler)
  })

  it('pushing url without anchor will retain the page anchor tag', function() {
    var handler = htmx.on('htmx:configRequest', function(evt) {
      evt.detail.path = evt.detail.path + '#test'
    })
    var path = ''
    var handler2 = htmx.on('htmx:pushedIntoHistory', function(evt) {
      path = evt.detail.path
    })
    try {
      this.server.respondWith('GET', '/test', 'Clicked!')
      var div = make("<div hx-get='/test' hx-push-url='/test'></div>")
      div.click()
      this.server.respond()
      div.innerHTML.should.equal('Clicked!')
      path.should.equal('/test#test')
    } finally {
      htmx.off('htmx:configRequest', handler)
      htmx.off('htmx:pushedIntoHistory', handler2)
    }
  })
})