| 1 |
describe('hx-indicator attribute', function() { |
| 2 |
beforeEach(function() { |
| 3 |
this.server = sinon.fakeServer.create() |
| 4 |
clearWorkArea() |
| 5 |
}) |
| 6 |
afterEach(function() { |
| 7 |
this.server.restore() |
| 8 |
clearWorkArea() |
| 9 |
}) |
| 10 |
|
| 11 |
it('Indicator classes are properly put on element with no explicit indicator', function() { |
| 12 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 13 |
var btn = make('<button hx-get="/test">Click Me!</button>') |
| 14 |
btn.click() |
| 15 |
btn.classList.contains('htmx-request').should.equal(true) |
| 16 |
this.server.respond() |
| 17 |
btn.classList.contains('htmx-request').should.equal(false) |
| 18 |
}) |
| 19 |
|
| 20 |
it('Indicator classes are properly put on element with explicit indicator', function() { |
| 21 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 22 |
var btn = make('<button hx-get="/test" hx-indicator="#a1, #a2">Click Me!</button>') |
| 23 |
var a1 = make('<a id="a1"></a>') |
| 24 |
var a2 = make('<a id="a2"></a>') |
| 25 |
btn.click() |
| 26 |
btn.classList.contains('htmx-request').should.equal(false) |
| 27 |
a1.classList.contains('htmx-request').should.equal(true) |
| 28 |
a2.classList.contains('htmx-request').should.equal(true) |
| 29 |
this.server.respond() |
| 30 |
btn.classList.contains('htmx-request').should.equal(false) |
| 31 |
a1.classList.contains('htmx-request').should.equal(false) |
| 32 |
a2.classList.contains('htmx-request').should.equal(false) |
| 33 |
}) |
| 34 |
|
| 35 |
it('Indicator classes are properly put on element with relative indicator', function() { |
| 36 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 37 |
var btn = make('<button hx-get="/test" hx-indicator="next a">Click Me!</button>') |
| 38 |
var a1 = make('<a id="a1"></a>') |
| 39 |
btn.click() |
| 40 |
btn.classList.contains('htmx-request').should.equal(false) |
| 41 |
a1.classList.contains('htmx-request').should.equal(true) |
| 42 |
this.server.respond() |
| 43 |
btn.classList.contains('htmx-request').should.equal(false) |
| 44 |
a1.classList.contains('htmx-request').should.equal(false) |
| 45 |
}) |
| 46 |
|
| 47 |
it('Indicator classes are properly put on element with explicit indicator w/ data-* prefix', function() { |
| 48 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 49 |
var btn = make('<button hx-get="/test" data-hx-indicator="#a1, #a2">Click Me!</button>') |
| 50 |
var a1 = make('<a id="a1"></a>') |
| 51 |
var a2 = make('<a id="a2"></a>') |
| 52 |
btn.click() |
| 53 |
btn.classList.contains('htmx-request').should.equal(false) |
| 54 |
a1.classList.contains('htmx-request').should.equal(true) |
| 55 |
a2.classList.contains('htmx-request').should.equal(true) |
| 56 |
this.server.respond() |
| 57 |
btn.classList.contains('htmx-request').should.equal(false) |
| 58 |
a1.classList.contains('htmx-request').should.equal(false) |
| 59 |
a2.classList.contains('htmx-request').should.equal(false) |
| 60 |
}) |
| 61 |
|
| 62 |
it('allows closest syntax in hx-indicator', function() { |
| 63 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 64 |
var div = make('<div id="d1"><button id="b1" hx-get="/test" hx-indicator="closest div">Click Me!</button></div>') |
| 65 |
var btn = byId('b1') |
| 66 |
btn.click() |
| 67 |
btn.classList.contains('htmx-request').should.equal(false) |
| 68 |
div.classList.contains('htmx-request').should.equal(true) |
| 69 |
this.server.respond() |
| 70 |
btn.classList.contains('htmx-request').should.equal(false) |
| 71 |
div.classList.contains('htmx-request').should.equal(false) |
| 72 |
}) |
| 73 |
|
| 74 |
it('is removed when initiating element is removed from the DOM', function() { |
| 75 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 76 |
var indicator = make('<div id="ind1">Indicator</div>') |
| 77 |
var div = make('<div id="d1" hx-target="this" hx-indicator="#ind1"><button id="b1" hx-get="/test">Click Me!</button></div>') |
| 78 |
var btn = byId('b1') |
| 79 |
btn.click() |
| 80 |
indicator.classList.contains('htmx-request').should.equal(true) |
| 81 |
this.server.respond() |
| 82 |
indicator.classList.contains('htmx-request').should.equal(false) |
| 83 |
}) |
| 84 |
|
| 85 |
it('allows this syntax in hx-indicator', function() { |
| 86 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 87 |
var div = make('<div id="d1" hx-indicator="this"><button id="b1" hx-get="/test">Click Me!</button></div>') |
| 88 |
var btn = byId('b1') |
| 89 |
btn.click() |
| 90 |
btn.classList.contains('htmx-request').should.equal(false) |
| 91 |
div.classList.contains('htmx-request').should.equal(true) |
| 92 |
this.server.respond() |
| 93 |
btn.classList.contains('htmx-request').should.equal(false) |
| 94 |
div.classList.contains('htmx-request').should.equal(false) |
| 95 |
}) |
| 96 |
|
| 97 |
it('multiple requests with same indicator are handled properly', function() { |
| 98 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 99 |
var b1 = make('<button hx-get="/test" hx-indicator=".a1">Click Me!</button>') |
| 100 |
var b2 = make('<button hx-get="/test" hx-indicator=".a1">Click Me!</button>') |
| 101 |
var a1 = make('<a class="a1"></a>') |
| 102 |
|
| 103 |
b1.click() |
| 104 |
b1.classList.contains('htmx-request').should.equal(false) |
| 105 |
b2.classList.contains('htmx-request').should.equal(false) |
| 106 |
a1.classList.contains('htmx-request').should.equal(true) |
| 107 |
|
| 108 |
b2.click() |
| 109 |
b1.classList.contains('htmx-request').should.equal(false) |
| 110 |
b2.classList.contains('htmx-request').should.equal(false) |
| 111 |
a1.classList.contains('htmx-request').should.equal(true) |
| 112 |
|
| 113 |
|
| 114 |
this.server.processRequest(this.server.queue.shift()) |
| 115 |
|
| 116 |
b1.classList.contains('htmx-request').should.equal(false) |
| 117 |
b2.classList.contains('htmx-request').should.equal(false) |
| 118 |
a1.classList.contains('htmx-request').should.equal(true) |
| 119 |
|
| 120 |
this.server.respond() |
| 121 |
|
| 122 |
b1.classList.contains('htmx-request').should.equal(false) |
| 123 |
b2.classList.contains('htmx-request').should.equal(false) |
| 124 |
a1.classList.contains('htmx-request').should.equal(false) |
| 125 |
}) |
| 126 |
|
| 127 |
it('`inherit` can be used to expand parent hx-indicator', function() { |
| 128 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 129 |
make('<div hx-indicator="#a1">' + |
| 130 |
' <button id="btn" hx-get="/test" hx-indicator="inherit, #a2">Click Me!</button>' + |
| 131 |
'</div>') |
| 132 |
var btn = byId('btn') |
| 133 |
var a1 = make('<a id="a1"></a>') |
| 134 |
var a2 = make('<a id="a2"></a>') |
| 135 |
btn.click() |
| 136 |
btn.classList.contains('htmx-request').should.equal(false) |
| 137 |
a1.classList.contains('htmx-request').should.equal(true) |
| 138 |
a2.classList.contains('htmx-request').should.equal(true) |
| 139 |
this.server.respond() |
| 140 |
btn.classList.contains('htmx-request').should.equal(false) |
| 141 |
a1.classList.contains('htmx-request').should.equal(false) |
| 142 |
a2.classList.contains('htmx-request').should.equal(false) |
| 143 |
}) |
| 144 |
|
| 145 |
it('`inherit` can be used to expand multiple parents hx-indicator', function() { |
| 146 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 147 |
make('<div hx-indicator="#a1">' + |
| 148 |
' <div hx-indicator="inherit, #a2">' + |
| 149 |
' <button id="btn" hx-get="/test" hx-indicator="inherit, #a3">Click Me!</button>' + |
| 150 |
' </div>' + |
| 151 |
'</div>') |
| 152 |
var btn = byId('btn') |
| 153 |
var a1 = make('<a id="a1"></a>') |
| 154 |
var a2 = make('<a id="a2"></a>') |
| 155 |
var a3 = make('<a id="a3"></a>') |
| 156 |
btn.click() |
| 157 |
btn.classList.contains('htmx-request').should.equal(false) |
| 158 |
a1.classList.contains('htmx-request').should.equal(true) |
| 159 |
a2.classList.contains('htmx-request').should.equal(true) |
| 160 |
a3.classList.contains('htmx-request').should.equal(true) |
| 161 |
this.server.respond() |
| 162 |
btn.classList.contains('htmx-request').should.equal(false) |
| 163 |
a1.classList.contains('htmx-request').should.equal(false) |
| 164 |
a2.classList.contains('htmx-request').should.equal(false) |
| 165 |
a3.classList.contains('htmx-request').should.equal(false) |
| 166 |
}) |
| 167 |
|
| 168 |
it('`inherit` chain breaks properly', function() { |
| 169 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 170 |
make('<div hx-indicator="#a1">' + |
| 171 |
' <div hx-indicator="#a2">' + |
| 172 |
' <button id="btn" hx-get="/test" hx-indicator="inherit, #a3">Click Me!</button>' + |
| 173 |
' </div>' + |
| 174 |
'</div>') |
| 175 |
var btn = byId('btn') |
| 176 |
var a1 = make('<a id="a1"></a>') |
| 177 |
var a2 = make('<a id="a2"></a>') |
| 178 |
var a3 = make('<a id="a3"></a>') |
| 179 |
btn.click() |
| 180 |
btn.classList.contains('htmx-request').should.equal(false) |
| 181 |
a1.classList.contains('htmx-request').should.equal(false) |
| 182 |
a2.classList.contains('htmx-request').should.equal(true) |
| 183 |
a3.classList.contains('htmx-request').should.equal(true) |
| 184 |
this.server.respond() |
| 185 |
btn.classList.contains('htmx-request').should.equal(false) |
| 186 |
a1.classList.contains('htmx-request').should.equal(false) |
| 187 |
a2.classList.contains('htmx-request').should.equal(false) |
| 188 |
a3.classList.contains('htmx-request').should.equal(false) |
| 189 |
}) |
| 190 |
}) |