Debian Codegraph @

test/tests/unit/__handleHistoryUpdate.js

raw symbols SCIP
1 describe('__handleHistoryUpdate unit tests', function() {
2
3 let originalUrl
4 let originalState
5
6 beforeEach(function() {
7 setupTest();
8 // Save current URL and state
9 originalUrl = window.location.href
10 originalState = history.state
11 });
12
13 afterEach(function() {
14 cleanupTest();
15 // Restore original URL and state
16 history.replaceState(originalState, '', originalUrl)
17 });
18
19 it('does nothing when push and replace are false', function () {
20 let div = createProcessedHTML('<div hx-get="/test"></div>')
21 let ctx = {
22 sourceElement: div,
23 push: 'false',
24 replace: 'false',
25 response: { headers: new Headers() },
26 request: { action: '/test' }
27 }
28
29 htmx.__handleHistoryUpdate(ctx)
30
31 assert.equal(window.location.href, originalUrl)
32 })
33
34 it('pushes URL when push is set to true', function () {
35 let div = createProcessedHTML('<div hx-get="/test"></div>')
36 let ctx = {
37 sourceElement: div,
38 push: 'true',
39 response: { headers: new Headers() },
40 request: { action: '/test-path' }
41 }
42
43 htmx.__handleHistoryUpdate(ctx)
44
45 assert.include(window.location.href, '/test-path')
46 })
47
48 it('replaces URL when replace is set to true', function () {
49 let div = createProcessedHTML('<div hx-get="/test"></div>')
50 let ctx = {
51 sourceElement: div,
52 replace: 'true',
53 response: { headers: new Headers() },
54 request: { action: '/replace-path' }
55 }
56
57 htmx.__handleHistoryUpdate(ctx)
58
59 assert.include(window.location.href, '/replace-path')
60 })
61
62 it('pushes specific URL when push is set to path', function () {
63 let div = createProcessedHTML('<div hx-get="/test"></div>')
64 let ctx = {
65 sourceElement: div,
66 push: '/custom-path',
67 response: { headers: new Headers() },
68 request: { action: '/test' }
69 }
70
71 htmx.__handleHistoryUpdate(ctx)
72
73 assert.include(window.location.href, '/custom-path')
74 })
75
76 it('pushes redirected URL when push is true and response has raw url', function () {
77 let div = createProcessedHTML('<div hx-get="/test"></div>')
78 let ctx = {
79 sourceElement: div,
80 push: 'true',
81 response: {
82 headers: new Headers(),
83 raw: { url: 'http://localhost/redirected-path?foo=bar' }
84 },
85 request: { action: '/test' }
86 }
87
88 htmx.__handleHistoryUpdate(ctx)
89
90 assert.include(window.location.href, '/redirected-path?foo=bar')
91 })
92
93 });