| 1 | describe('hx-sync attribute', function() { |
| 2 | beforeEach(function() { |
| 3 | this.server = makeServer() |
| 4 | clearWorkArea() |
| 5 | }) |
| 6 | afterEach(function() { |
| 7 | this.server.restore() |
| 8 | clearWorkArea() |
| 9 | }) |
| 10 | |
| 11 | it('can use drop strategy', function() { |
| 12 | var count = 0 |
| 13 | this.server.respondWith('GET', '/test', function(xhr) { |
| 14 | xhr.respond(200, {}, 'Click ' + count++) |
| 15 | }) |
| 16 | make('<div hx-sync="this:drop"><button id="b1" hx-get="/test">Initial</button>' + |
| 17 | ' <button id="b2" hx-get="/test">Initial</button></div>') |
| 18 | var b1 = byId('b1') |
| 19 | var b2 = byId('b2') |
| 20 | b1.click() |
| 21 | b2.click() |
| 22 | this.server.respond() |
| 23 | this.server.respond() |
| 24 | b1.innerHTML.should.equal('Click 0') |
| 25 | b2.innerHTML.should.equal('Initial') |
| 26 | }) |
| 27 | |
| 28 | it('defaults to the drop strategy', function() { |
| 29 | var count = 0 |
| 30 | this.server.respondWith('GET', '/test', function(xhr) { |
| 31 | xhr.respond(200, {}, 'Click ' + count++) |
| 32 | }) |
| 33 | make('<div hx-sync="this"><button id="b1" hx-get="/test">Initial</button>' + |
| 34 | ' <button id="b2" hx-get="/test">Initial</button></div>') |
| 35 | var b1 = byId('b1') |
| 36 | var b2 = byId('b2') |
| 37 | b1.click() |
| 38 | b2.click() |
| 39 | this.server.respond() |
| 40 | this.server.respond() |
| 41 | b1.innerHTML.should.equal('Click 0') |
| 42 | b2.innerHTML.should.equal('Initial') |
| 43 | }) |
| 44 | |
| 45 | it('can use replace strategy', function() { |
| 46 | var count = 0 |
| 47 | this.server.respondWith('GET', '/test', function(xhr) { |
| 48 | xhr.respond(200, {}, 'Click ' + count++) |
| 49 | }) |
| 50 | make('<div hx-sync="this:replace"><button id="b1" hx-get="/test">Initial</button>' + |
| 51 | ' <button id="b2" hx-get="/test">Initial</button></div>') |
| 52 | var b1 = byId('b1') |
| 53 | var b2 = byId('b2') |
| 54 | b1.click() |
| 55 | b2.click() |
| 56 | this.server.respond() |
| 57 | this.server.respond() |
| 58 | b1.innerHTML.should.equal('Initial') |
| 59 | b2.innerHTML.should.equal('Click 0') |
| 60 | }) |
| 61 | |
| 62 | it('can use queue all strategy', function() { |
| 63 | var count = 0 |
| 64 | this.server.respondWith('GET', '/test', function(xhr) { |
| 65 | xhr.respond(200, {}, 'Click ' + count++) |
| 66 | }) |
| 67 | make('<div hx-sync="this:queue all"><button id="b1" hx-get="/test">Initial</button>' + |
| 68 | ' <button id="b2" hx-get="/test">Initial</button>' + |
| 69 | ' <button id="b3" hx-get="/test">Initial</button></div>') |
| 70 | var b1 = byId('b1') |
| 71 | b1.click() |
| 72 | |
| 73 | var b2 = byId('b2') |
| 74 | b2.click() |
| 75 | |
| 76 | var b3 = byId('b3') |
| 77 | b3.click() |
| 78 | |
| 79 | this.server.respond() |
| 80 | b1.innerHTML.should.equal('Click 0') |
| 81 | b2.innerHTML.should.equal('Initial') |
| 82 | b3.innerHTML.should.equal('Initial') |
| 83 | |
| 84 | this.server.respond() |
| 85 | b1.innerHTML.should.equal('Click 0') |
| 86 | b2.innerHTML.should.equal('Click 1') |
| 87 | b3.innerHTML.should.equal('Initial') |
| 88 | |
| 89 | this.server.respond() |
| 90 | b1.innerHTML.should.equal('Click 0') |
| 91 | b2.innerHTML.should.equal('Click 1') |
| 92 | b3.innerHTML.should.equal('Click 2') |
| 93 | }) |
| 94 | |
| 95 | it('can use queue last strategy', function() { |
| 96 | var count = 0 |
| 97 | this.server.respondWith('GET', '/test', function(xhr) { |
| 98 | xhr.respond(200, {}, 'Click ' + count++) |
| 99 | }) |
| 100 | make('<div hx-sync="this:queue last"><button id="b1" hx-get="/test">Initial</button>' + |
| 101 | ' <button id="b2" hx-get="/test">Initial</button>' + |
| 102 | ' <button id="b3" hx-get="/test">Initial</button></div>') |
| 103 | var b1 = byId('b1') |
| 104 | b1.click() |
| 105 | |
| 106 | var b2 = byId('b2') |
| 107 | b2.click() |
| 108 | |
| 109 | var b3 = byId('b3') |
| 110 | b3.click() |
| 111 | |
| 112 | this.server.respond() |
| 113 | b1.innerHTML.should.equal('Click 0') |
| 114 | b2.innerHTML.should.equal('Initial') |
| 115 | b3.innerHTML.should.equal('Initial') |
| 116 | |
| 117 | this.server.respond() |
| 118 | b1.innerHTML.should.equal('Click 0') |
| 119 | b2.innerHTML.should.equal('Initial') |
| 120 | b3.innerHTML.should.equal('Click 1') |
| 121 | |
| 122 | this.server.respond() |
| 123 | b1.innerHTML.should.equal('Click 0') |
| 124 | b2.innerHTML.should.equal('Initial') |
| 125 | b3.innerHTML.should.equal('Click 1') |
| 126 | }) |
| 127 | |
| 128 | it('can use queue first strategy', function() { |
| 129 | var count = 0 |
| 130 | this.server.respondWith('GET', '/test', function(xhr) { |
| 131 | xhr.respond(200, {}, 'Click ' + count++) |
| 132 | }) |
| 133 | make('<div hx-sync="this:queue first"><button id="b1" hx-get="/test">Initial</button>' + |
| 134 | ' <button id="b2" hx-get="/test">Initial</button>' + |
| 135 | ' <button id="b3" hx-get="/test">Initial</button></div>') |
| 136 | var b1 = byId('b1') |
| 137 | b1.click() |
| 138 | |
| 139 | var b2 = byId('b2') |
| 140 | b2.click() |
| 141 | |
| 142 | var b3 = byId('b3') |
| 143 | b3.click() |
| 144 | |
| 145 | this.server.respond() |
| 146 | b1.innerHTML.should.equal('Click 0') |
| 147 | b2.innerHTML.should.equal('Initial') |
| 148 | b3.innerHTML.should.equal('Initial') |
| 149 | |
| 150 | this.server.respond() |
| 151 | b1.innerHTML.should.equal('Click 0') |
| 152 | b2.innerHTML.should.equal('Click 1') |
| 153 | b3.innerHTML.should.equal('Initial') |
| 154 | |
| 155 | this.server.respond() |
| 156 | b1.innerHTML.should.equal('Click 0') |
| 157 | b2.innerHTML.should.equal('Click 1') |
| 158 | b3.innerHTML.should.equal('Initial') |
| 159 | }) |
| 160 | |
| 161 | it('can use abort strategy to end existing abortable request', function() { |
| 162 | var count = 0 |
| 163 | this.server.respondWith('GET', '/test', function(xhr) { |
| 164 | xhr.respond(200, {}, 'Click ' + count++) |
| 165 | }) |
| 166 | make('<div hx-sync="this"><button hx-sync="closest div:abort" id="b1" hx-get="/test">Initial</button>' + |
| 167 | ' <button id="b2" hx-get="/test">Initial</button></div>') |
| 168 | var b1 = byId('b1') |
| 169 | var b2 = byId('b2') |
| 170 | b1.click() |
| 171 | b2.click() |
| 172 | this.server.respond() |
| 173 | this.server.respond() |
| 174 | b1.innerHTML.should.equal('Initial') |
| 175 | b2.innerHTML.should.equal('Click 0') |
| 176 | }) |
| 177 | |
| 178 | it('can use abort strategy to drop abortable request when one is in flight', function() { |
| 179 | var count = 0 |
| 180 | this.server.respondWith('GET', '/test', function(xhr) { |
| 181 | xhr.respond(200, {}, 'Click ' + count++) |
| 182 | }) |
| 183 | make('<div hx-sync="this"><button hx-sync="closest div:abort" id="b1" hx-get="/test">Initial</button>' + |
| 184 | ' <button id="b2" hx-get="/test">Initial</button></div>') |
| 185 | var b1 = byId('b1') |
| 186 | var b2 = byId('b2') |
| 187 | b2.click() |
| 188 | b1.click() |
| 189 | this.server.respond() |
| 190 | this.server.respond() |
| 191 | b1.innerHTML.should.equal('Initial') |
| 192 | b2.innerHTML.should.equal('Click 0') |
| 193 | }) |
| 194 | |
| 195 | it('can abort a request programmatically', function() { |
| 196 | var count = 0 |
| 197 | this.server.respondWith('GET', '/test', function(xhr) { |
| 198 | xhr.respond(200, {}, 'Click ' + count++) |
| 199 | }) |
| 200 | make('<div><button id="b1" hx-get="/test">Initial</button>' + |
| 201 | ' <button id="b2" hx-get="/test">Initial</button></div>') |
| 202 | var b1 = byId('b1') |
| 203 | var b2 = byId('b2') |
| 204 | b1.click() |
| 205 | b2.click() |
| 206 | |
| 207 | htmx.trigger(b1, 'htmx:abort') |
| 208 | |
| 209 | this.server.respond() |
| 210 | this.server.respond() |
| 211 | b1.innerHTML.should.equal('Initial') |
| 212 | b2.innerHTML.should.equal('Click 0') |
| 213 | }) |
| 214 | }) |