| 1 | describe('__shouldCancel() unit tests', function() { |
| 2 | |
| 3 | it('anchor with href should cancel click event', function() { |
| 4 | const anchor = createDisconnectedHTML('<a href="/foo"></a>'); |
| 5 | const evt = { type: 'click', currentTarget: anchor, button: 0 }; |
| 6 | assert.equal(htmx.__shouldCancel(evt), true); |
| 7 | }); |
| 8 | |
| 9 | it('anchor with # href should cancel click event', function() { |
| 10 | const anchor = createDisconnectedHTML('<a href="#"></a>'); |
| 11 | const evt = { type: 'click', currentTarget: anchor, button: 0 }; |
| 12 | assert.equal(htmx.__shouldCancel(evt), true); |
| 13 | }); |
| 14 | |
| 15 | it('anchor with #foo href should not cancel click event', function() { |
| 16 | const evt = { type: 'click', currentTarget: createDisconnectedHTML('<a href="#foo"></a>'), button: 0 }; |
| 17 | assert.equal(htmx.__shouldCancel(evt), false); |
| 18 | }); |
| 19 | |
| 20 | it('div should not cancel click event', function() { |
| 21 | const evt = { type: 'click', currentTarget: createDisconnectedHTML('<div></div>'), button: 0 }; |
| 22 | assert.equal(htmx.__shouldCancel(evt), false); |
| 23 | }); |
| 24 | |
| 25 | it('form should cancel submit event', function() { |
| 26 | const evt = { type: 'submit', currentTarget: createDisconnectedHTML('<form></form>') }; |
| 27 | assert.equal(htmx.__shouldCancel(evt), true); |
| 28 | }); |
| 29 | |
| 30 | it('button inside form should cancel click event', function() { |
| 31 | const form = createDisconnectedHTML('<form><button></button></form>'); |
| 32 | const evt = { type: 'click', currentTarget: form.firstElementChild, button: 0 }; |
| 33 | assert.equal(htmx.__shouldCancel(evt), true); |
| 34 | }); |
| 35 | |
| 36 | it('submit button inside form should cancel click event', function() { |
| 37 | const form = createDisconnectedHTML('<form><button type="submit"></button></form>'); |
| 38 | const evt = { type: 'click', currentTarget: form.firstElementChild, button: 0 }; |
| 39 | assert.equal(htmx.__shouldCancel(evt), true); |
| 40 | }); |
| 41 | |
| 42 | it('input type=submit inside form should cancel click event', function() { |
| 43 | const form = createDisconnectedHTML('<form><input type="submit"></form>'); |
| 44 | const evt = { type: 'click', currentTarget: form.firstElementChild, button: 0 }; |
| 45 | assert.equal(htmx.__shouldCancel(evt), true); |
| 46 | }); |
| 47 | |
| 48 | it('input type=image inside form should cancel click event', function() { |
| 49 | const form = createDisconnectedHTML('<form><input type="image"></form>'); |
| 50 | const evt = { type: 'click', currentTarget: form.firstElementChild, button: 0 }; |
| 51 | assert.equal(htmx.__shouldCancel(evt), true); |
| 52 | }); |
| 53 | |
| 54 | it('button type=reset inside form should not cancel click event', function() { |
| 55 | const form = createDisconnectedHTML('<form><button type="reset"></button></form>'); |
| 56 | const evt = { type: 'click', currentTarget: form.firstElementChild, button: 0 }; |
| 57 | assert.equal(htmx.__shouldCancel(evt), false); |
| 58 | }); |
| 59 | |
| 60 | it('button type=button inside form should not cancel click event', function() { |
| 61 | const form = createDisconnectedHTML('<form><button type="button"></button></form>'); |
| 62 | const evt = { type: 'click', currentTarget: form.firstElementChild, button: 0 }; |
| 63 | assert.equal(htmx.__shouldCancel(evt), false); |
| 64 | }); |
| 65 | |
| 66 | it('button with form attribute should cancel click event', function() { |
| 67 | const container = createHTMLNoProcessing('<div><form id="f1"></form><button form="f1"></button></div>'); |
| 68 | try { |
| 69 | const evt = { type: 'click', currentTarget: container.querySelector('button'), button: 0 }; |
| 70 | assert.equal(htmx.__shouldCancel(evt), true); |
| 71 | } finally { |
| 72 | container.remove(); |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | it('button with form attribute and type=submit should cancel click event', function() { |
| 77 | const container = createHTMLNoProcessing('<div><form id="f1"></form><button form="f1" type="submit"></button></div>'); |
| 78 | try { |
| 79 | const evt = { type: 'click', currentTarget: container.querySelector('button'), button: 0 }; |
| 80 | assert.equal(htmx.__shouldCancel(evt), true); |
| 81 | } finally { |
| 82 | container.remove(); |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | it('button with form attribute and type=button should not cancel click event', function() { |
| 87 | const container = createHTMLNoProcessing('<div><form id="f1"></form><button form="f1" type="button"></button></div>'); |
| 88 | try { |
| 89 | const evt = { type: 'click', currentTarget: container.querySelector('button'), button: 0 }; |
| 90 | assert.equal(htmx.__shouldCancel(evt), false); |
| 91 | } finally { |
| 92 | container.remove(); |
| 93 | } |
| 94 | }); |
| 95 | |
| 96 | it('button with form attribute and type=reset should not cancel click event', function() { |
| 97 | const container = createHTMLNoProcessing('<div><form id="f1"></form><button form="f1" type="reset"></button></div>'); |
| 98 | try { |
| 99 | const evt = { type: 'click', currentTarget: container.querySelector('button'), button: 0 }; |
| 100 | assert.equal(htmx.__shouldCancel(evt), false); |
| 101 | } finally { |
| 102 | container.remove(); |
| 103 | } |
| 104 | }); |
| 105 | |
| 106 | it('button without form should not cancel click event', function() { |
| 107 | const evt = { type: 'click', currentTarget: createDisconnectedHTML('<button></button>'), button: 0 }; |
| 108 | assert.equal(htmx.__shouldCancel(evt), false); |
| 109 | }); |
| 110 | |
| 111 | it('disabled button should not cancel click event', function() { |
| 112 | const form = createDisconnectedHTML('<form><button disabled></button></form>'); |
| 113 | const evt = { type: 'click', currentTarget: form.firstElementChild, button: 0 }; |
| 114 | assert.equal(htmx.__shouldCancel(evt), false); |
| 115 | }); |
| 116 | |
| 117 | it('right-click (button !== 0) should not cancel', function() { |
| 118 | const evt = { type: 'click', currentTarget: createDisconnectedHTML('<a href="/foo"></a>'), button: 2 }; |
| 119 | assert.equal(htmx.__shouldCancel(evt), false); |
| 120 | }); |
| 121 | |
| 122 | it('middle-click (button !== 0) should not cancel', function() { |
| 123 | const evt = { type: 'click', currentTarget: createDisconnectedHTML('<a href="/foo"></a>'), button: 1 }; |
| 124 | assert.equal(htmx.__shouldCancel(evt), false); |
| 125 | }); |
| 126 | |
| 127 | it('button inside htmx-enabled link should cancel', function() { |
| 128 | const link = createDisconnectedHTML('<a href="/foo"><button></button></a>'); |
| 129 | const evt = { type: 'click', currentTarget: link.firstElementChild, button: 0 }; |
| 130 | assert.equal(htmx.__shouldCancel(evt), true); |
| 131 | }); |
| 132 | |
| 133 | it('htmx-enabled button inside link should cancel', function() { |
| 134 | const link = createDisconnectedHTML('<a href="/foo"><button></button></a>'); |
| 135 | const evt = { type: 'click', currentTarget: link.firstElementChild, button: 0 }; |
| 136 | assert.equal(htmx.__shouldCancel(evt), true); |
| 137 | }); |
| 138 | |
| 139 | it('span inside button in form should cancel', function() { |
| 140 | const form = createDisconnectedHTML('<form><button><span></span></button></form>'); |
| 141 | const evt = { type: 'click', currentTarget: form.querySelector('span'), button: 0 }; |
| 142 | assert.equal(htmx.__shouldCancel(evt), true); |
| 143 | }); |
| 144 | |
| 145 | it('element inside form button should cancel', function() { |
| 146 | const form = createDisconnectedHTML('<form><button><span></span></button></form>'); |
| 147 | const evt = { type: 'click', currentTarget: form.querySelector('span'), button: 0 }; |
| 148 | assert.equal(htmx.__shouldCancel(evt), true); |
| 149 | }); |
| 150 | |
| 151 | it('submit button with form attribute outside form should cancel', function() { |
| 152 | const container = createHTMLNoProcessing('<div><form id="test-form"></form><button type="submit" form="test-form"></button></div>'); |
| 153 | try { |
| 154 | const evt = { type: 'click', currentTarget: container.querySelector('button'), button: 0 }; |
| 155 | assert.equal(htmx.__shouldCancel(evt), true); |
| 156 | } finally { |
| 157 | container.remove(); |
| 158 | } |
| 159 | }); |
| 160 | |
| 161 | it('input type=submit with form attribute outside form should cancel', function() { |
| 162 | const container = createHTMLNoProcessing('<div><form id="test-form"></form><input type="submit" form="test-form"></div>'); |
| 163 | try { |
| 164 | const evt = { type: 'click', currentTarget: container.querySelector('input'), button: 0 }; |
| 165 | assert.equal(htmx.__shouldCancel(evt), true); |
| 166 | } finally { |
| 167 | container.remove(); |
| 168 | } |
| 169 | }); |
| 170 | }); |