Debian Codegraph @

test/tests/unit/__shouldBoost.js

raw symbols SCIP
1 describe('__shouldBoost() unit tests', function() {
2
3 beforeEach(function() {
4 setupTest();
5 });
6
7 afterEach(function() {
8 cleanupTest();
9 });
10
11 // Anchor tag tests
12 describe('Anchor tags', function() {
13
14 it('should boost same-origin link with no target', function() {
15 const link = createDisconnectedHTML('<a href="/test">Link</a>');
16 const result = htmx.__shouldBoost(link);
17 assert.isTrue(result);
18 });
19
20 it('should boost same-origin link with target="_self"', function() {
21 const link = createDisconnectedHTML('<a href="/test" target="_self">Link</a>');
22 const result = htmx.__shouldBoost(link);
23 assert.isTrue(result);
24 });
25
26 it('should boost relative URLs', function() {
27 const link = createDisconnectedHTML('<a href="test.html">Link</a>');
28 const result = htmx.__shouldBoost(link);
29 assert.isTrue(result);
30 });
31
32 it('should boost absolute same-origin URLs', function() {
33 const origin = window.location.origin;
34 const link = createDisconnectedHTML(`<a href="${origin}/test">Link</a>`);
35 const result = htmx.__shouldBoost(link);
36 assert.isTrue(result);
37 });
38
39 it('should not boost hash-only links', function() {
40 const link = createDisconnectedHTML('<a href="#section">Link</a>');
41 const result = htmx.__shouldBoost(link);
42 assert.isFalse(result);
43 });
44
45 it('should not boost links with download', function() {
46 const link = createDisconnectedHTML('<a href="/report.pdf" download>Download</a>');
47 const result = htmx.__shouldBoost(link);
48 assert.isFalse(result);
49 });
50
51 it('should not boost links with target="_blank"', function() {
52 const link = createDisconnectedHTML('<a href="/test" target="_blank">Link</a>');
53 const result = htmx.__shouldBoost(link);
54 assert.isUndefined(result);
55 });
56
57 it('should not boost links with target="_parent"', function() {
58 const link = createDisconnectedHTML('<a href="/test" target="_parent">Link</a>');
59 const result = htmx.__shouldBoost(link);
60 assert.isUndefined(result);
61 });
62
63 it('should not boost links with target="_top"', function() {
64 const link = createDisconnectedHTML('<a href="/test" target="_top">Link</a>');
65 const result = htmx.__shouldBoost(link);
66 assert.isUndefined(result);
67 });
68
69 it('should not boost links with named target', function() {
70 const link = createDisconnectedHTML('<a href="/test" target="myframe">Link</a>');
71 const result = htmx.__shouldBoost(link);
72 assert.isUndefined(result);
73 });
74
75 it('should not boost cross-origin links', function() {
76 const link = createDisconnectedHTML('<a href="https://example.com/test">Link</a>');
77 const result = htmx.__shouldBoost(link);
78 assert.isFalse(result);
79 });
80
81 it('should not boost protocol-relative cross-origin links', function() {
82 const link = createDisconnectedHTML('<a href="//example.com/test">Link</a>');
83 const result = htmx.__shouldBoost(link);
84 assert.isFalse(result);
85 });
86
87 it('should boost links with hash after path', function() {
88 const link = createDisconnectedHTML('<a href="/test#section">Link</a>');
89 const result = htmx.__shouldBoost(link);
90 assert.isTrue(result);
91 });
92
93 it('should not boost javascript: URLs', function() {
94 const link = createDisconnectedHTML('<a href="javascript:void(0)">Link</a>');
95 // javascript: URLs will likely fail the same-origin check
96 const result = htmx.__shouldBoost(link);
97 assert.isFalse(result);
98 });
99 });
100
101 // Form tag tests
102 describe('Form tags', function() {
103
104 it('should boost same-origin form with no action', function() {
105 const form = createDisconnectedHTML('<form></form>');
106 const result = htmx.__shouldBoost(form);
107 assert.isTrue(result);
108 });
109
110 it('should boost same-origin form with relative action', function() {
111 const form = createDisconnectedHTML('<form action="/submit"></form>');
112 const result = htmx.__shouldBoost(form);
113 assert.isTrue(result);
114 });
115
116 it('should boost form with absolute same-origin action', function() {
117 const origin = window.location.origin;
118 const form = createDisconnectedHTML(`<form action="${origin}/submit"></form>`);
119 const result = htmx.__shouldBoost(form);
120 assert.isTrue(result);
121 });
122
123 it('should boost form with method="post"', function() {
124 const form = createDisconnectedHTML('<form method="post" action="/submit"></form>');
125 const result = htmx.__shouldBoost(form);
126 assert.isTrue(result);
127 });
128
129 it('should boost form with method="get"', function() {
130 const form = createDisconnectedHTML('<form method="get" action="/search"></form>');
131 const result = htmx.__shouldBoost(form);
132 assert.isTrue(result);
133 });
134
135 it('should not boost form with method="dialog"', function() {
136 const form = createDisconnectedHTML('<form method="dialog"></form>');
137 const result = htmx.__shouldBoost(form);
138 assert.isFalse(result);
139 });
140
141 it('should not boost form with cross-origin action', function() {
142 const form = createDisconnectedHTML('<form action="https://example.com/submit"></form>');
143 const result = htmx.__shouldBoost(form);
144 assert.isFalse(result);
145 });
146
147 it('should not boost form with protocol-relative cross-origin action', function() {
148 const form = createDisconnectedHTML('<form action="//example.com/submit"></form>');
149 const result = htmx.__shouldBoost(form);
150 assert.isFalse(result);
151 });
152 });
153
154 // Other element tests
155 describe('Other elements', function() {
156
157 it('should return undefined for non-anchor, non-form elements', function() {
158 const div = createDisconnectedHTML('<div></div>');
159 const result = htmx.__shouldBoost(div);
160 assert.isUndefined(result);
161 });
162
163 it('should return undefined for button elements', function() {
164 const button = createDisconnectedHTML('<button></button>');
165 const result = htmx.__shouldBoost(button);
166 assert.isUndefined(result);
167 });
168
169 it('should return undefined for input elements', function() {
170 const input = createDisconnectedHTML('<input type="submit">');
171 const result = htmx.__shouldBoost(input);
172 assert.isUndefined(result);
173 });
174 });
175 });