| 1 | describe('hx-get 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('issues a GET request on click and swaps content', function() { |
| 12 | this.server.respondWith('GET', '/test', 'Clicked!') |
| 13 | |
| 14 | var btn = make('<button hx-get="/test">Click Me!</button>') |
| 15 | btn.click() |
| 16 | this.server.respond() |
| 17 | btn.innerHTML.should.equal('Clicked!') |
| 18 | }) |
| 19 | |
| 20 | it('GET does not include surrounding data by default', function() { |
| 21 | this.server.respondWith('GET', '/test', function(xhr) { |
| 22 | should.equal(getParameters(xhr).i1, undefined) |
| 23 | xhr.respond(200, {}, 'Clicked!') |
| 24 | }) |
| 25 | make('<form><input name="i1" value="value"/><button id="b1" hx-get="/test">Click Me!</button></form>') |
| 26 | var btn = byId('b1') |
| 27 | btn.click() |
| 28 | this.server.respond() |
| 29 | btn.innerHTML.should.equal('Clicked!') |
| 30 | }) |
| 31 | |
| 32 | it('GET on form includes its own data by default', function() { |
| 33 | this.server.respondWith('GET', /\/test.*/, function(xhr) { |
| 34 | getParameters(xhr).i1.should.equal('value') |
| 35 | xhr.respond(200, {}, 'Clicked!') |
| 36 | }) |
| 37 | var form = make('<form hx-trigger="click" hx-get="/test"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>') |
| 38 | form.click() |
| 39 | this.server.respond() |
| 40 | form.innerHTML.should.equal('Clicked!') |
| 41 | }) |
| 42 | |
| 43 | it('GET on form with existing parameters works properly', function() { |
| 44 | this.server.respondWith('GET', /\/test.*/, function(xhr) { |
| 45 | getParameters(xhr).foo.should.equal('bar') |
| 46 | getParameters(xhr).i1.should.equal('value') |
| 47 | xhr.respond(200, {}, 'Clicked!') |
| 48 | }) |
| 49 | var form = make('<form hx-trigger="click" hx-get="/test?foo=bar"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>') |
| 50 | form.click() |
| 51 | this.server.respond() |
| 52 | form.innerHTML.should.equal('Clicked!') |
| 53 | }) |
| 54 | |
| 55 | it('GET on form with anchor works properly', function() { |
| 56 | this.server.respondWith('GET', /\/test.*/, function(xhr) { |
| 57 | getParameters(xhr).foo.should.equal('bar') |
| 58 | getParameters(xhr).i1.should.equal('value') |
| 59 | xhr.respond(200, {}, 'Clicked!') |
| 60 | }) |
| 61 | var form = make('<form hx-trigger="click" hx-get="/test?foo=bar#foo"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>') |
| 62 | form.click() |
| 63 | this.server.respond() |
| 64 | form.innerHTML.should.equal('Clicked!') |
| 65 | }) |
| 66 | |
| 67 | it('GET on form with anchor works properly and scrolls to anchor id', function() { |
| 68 | this.server.respondWith('GET', /\/test.*/, function(xhr) { |
| 69 | getParameters(xhr).foo.should.equal('bar') |
| 70 | getParameters(xhr).i1.should.equal('value') |
| 71 | xhr.respond(200, {}, '<div id="foo">Clicked</div>') |
| 72 | }) |
| 73 | var form = make('<form hx-trigger="click" hx-get="/test?foo=bar#foo"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>') |
| 74 | form.click() |
| 75 | this.server.respond() |
| 76 | form.innerHTML.should.equal('<div id="foo">Clicked</div>') |
| 77 | }) |
| 78 | |
| 79 | it('issues a GET request on click and swaps content w/ data-* prefix', function() { |
| 80 | this.server.respondWith('GET', '/test', 'Clicked!') |
| 81 | |
| 82 | var btn = make('<button data-hx-get="/test">Click Me!</button>') |
| 83 | btn.click() |
| 84 | this.server.respond() |
| 85 | btn.innerHTML.should.equal('Clicked!') |
| 86 | }) |
| 87 | |
| 88 | it('does not include a cache-busting parameter when not enabled', function() { |
| 89 | this.server.respondWith('GET', /\/test.*/, function(xhr) { |
| 90 | should.not.exist(getParameters(xhr)['org.htmx.cache-buster']) |
| 91 | xhr.respond(200, {}, 'Clicked!') |
| 92 | }) |
| 93 | |
| 94 | try { |
| 95 | htmx.config.getCacheBusterParam = false |
| 96 | var btn = make('<button hx-get="/test">Click Me!</button>') |
| 97 | btn.click() |
| 98 | this.server.respond() |
| 99 | btn.innerHTML.should.equal('Clicked!') |
| 100 | } finally { |
| 101 | htmx.config.getCacheBusterParam = false |
| 102 | } |
| 103 | }) |
| 104 | |
| 105 | it('includes a cache-busting parameter when enabled w/ value "true" if no id on target', function() { |
| 106 | this.server.respondWith('GET', /\/test.*/, function(xhr) { |
| 107 | getParameters(xhr)['org.htmx.cache-buster'].should.equal('true') |
| 108 | xhr.respond(200, {}, 'Clicked!') |
| 109 | }) |
| 110 | |
| 111 | try { |
| 112 | htmx.config.getCacheBusterParam = true |
| 113 | var btn = make('<button hx-get="/test">Click Me!</button>') |
| 114 | btn.click() |
| 115 | this.server.respond() |
| 116 | btn.innerHTML.should.equal('Clicked!') |
| 117 | } finally { |
| 118 | htmx.config.getCacheBusterParam = false |
| 119 | } |
| 120 | }) |
| 121 | |
| 122 | it('includes a cache-busting parameter when enabled w/ the id of the target if there is one', function() { |
| 123 | this.server.respondWith('GET', /\/test.*/, function(xhr) { |
| 124 | getParameters(xhr)['org.htmx.cache-buster'].should.equal('foo') |
| 125 | xhr.respond(200, {}, 'Clicked!') |
| 126 | }) |
| 127 | |
| 128 | try { |
| 129 | htmx.config.getCacheBusterParam = true |
| 130 | var btn = make('<button hx-get="/test" id="foo">Click Me!</button>') |
| 131 | btn.click() |
| 132 | this.server.respond() |
| 133 | btn.innerHTML.should.equal('Clicked!') |
| 134 | } finally { |
| 135 | htmx.config.getCacheBusterParam = false |
| 136 | } |
| 137 | }) |
| 138 | }) |