| 1 | describe('__showIndicators / __hideIndicators unit tests', function() { |
| 2 | |
| 3 | beforeEach(function() { |
| 4 | setupTest(); |
| 5 | }); |
| 6 | |
| 7 | afterEach(function() { |
| 8 | cleanupTest(); |
| 9 | }); |
| 10 | |
| 11 | it('shows indicator by adding request class', function () { |
| 12 | let container = createProcessedHTML('<div hx-indicator=".indicator"><span class="indicator"></span></div>') |
| 13 | let span = container.querySelector('span') |
| 14 | |
| 15 | htmx.__showIndicators(container) |
| 16 | |
| 17 | assert.isTrue(span.classList.contains('htmx-request')) |
| 18 | }) |
| 19 | |
| 20 | it('hides indicator by removing request class', function () { |
| 21 | let container = createProcessedHTML('<div hx-indicator=".indicator"><span class="indicator"></span></div>') |
| 22 | let span = container.querySelector('span') |
| 23 | |
| 24 | let indicators = htmx.__showIndicators(container) |
| 25 | htmx.__hideIndicators(indicators) |
| 26 | |
| 27 | assert.isFalse(span.classList.contains('htmx-request')) |
| 28 | }) |
| 29 | |
| 30 | it('increments counter on multiple shows', function () { |
| 31 | let container = createProcessedHTML('<div hx-indicator=".indicator"><span class="indicator"></span></div>') |
| 32 | let span = container.querySelector('span') |
| 33 | |
| 34 | htmx.__showIndicators(container) |
| 35 | htmx.__showIndicators(container) |
| 36 | |
| 37 | assert.equal(htmx.__htmxState(span).rc, 2) |
| 38 | assert.isTrue(span.classList.contains('htmx-request')) |
| 39 | }) |
| 40 | |
| 41 | it('decrements counter on hide', function () { |
| 42 | let container = createProcessedHTML('<div hx-indicator=".indicator"><span class="indicator"></span></div>') |
| 43 | let span = container.querySelector('span') |
| 44 | |
| 45 | let indicators1 = htmx.__showIndicators(container) |
| 46 | let indicators2 = htmx.__showIndicators(container) |
| 47 | htmx.__hideIndicators(indicators1) |
| 48 | |
| 49 | assert.equal(htmx.__htmxState(span).rc, 1) |
| 50 | assert.isTrue(span.classList.contains('htmx-request')) |
| 51 | }) |
| 52 | |
| 53 | it('removes class only when counter reaches zero', function () { |
| 54 | let container = createProcessedHTML('<div hx-indicator=".indicator"><span class="indicator"></span></div>') |
| 55 | let span = container.querySelector('span') |
| 56 | |
| 57 | let indicators1 = htmx.__showIndicators(container) |
| 58 | let indicators2 = htmx.__showIndicators(container) |
| 59 | htmx.__hideIndicators(indicators1) |
| 60 | htmx.__hideIndicators(indicators2) |
| 61 | |
| 62 | assert.isFalse(span.classList.contains('htmx-request')) |
| 63 | assert.isUndefined(htmx.__htmxState(span).rc) |
| 64 | }) |
| 65 | |
| 66 | it('handles multiple indicators', function () { |
| 67 | let container = createProcessedHTML('<div hx-indicator=".indicator"><span class="indicator"></span><div class="indicator"></div></div>') |
| 68 | let span = container.querySelector('span') |
| 69 | let div = container.querySelector('div') |
| 70 | |
| 71 | htmx.__showIndicators(container) |
| 72 | |
| 73 | assert.isTrue(span.classList.contains('htmx-request')) |
| 74 | assert.isTrue(div.classList.contains('htmx-request')) |
| 75 | }) |
| 76 | |
| 77 | it('does nothing when selector is null', function () { |
| 78 | let container = createProcessedHTML('<div><span class="indicator"></span></div>') |
| 79 | let span = container.querySelector('span') |
| 80 | |
| 81 | htmx.__showIndicators(container) |
| 82 | |
| 83 | assert.isFalse(span.classList.contains('htmx-request')) |
| 84 | }) |
| 85 | |
| 86 | it('includes element itself in indicators', function () { |
| 87 | let div = createProcessedHTML('<div hx-indicator=".indicator" class="indicator"></div>') |
| 88 | |
| 89 | htmx.__showIndicators(div) |
| 90 | |
| 91 | assert.isTrue(div.classList.contains('htmx-request')) |
| 92 | }) |
| 93 | |
| 94 | it('handles hide without prior show gracefully', function () { |
| 95 | let container = createProcessedHTML('<div><span class="indicator"></span></div>') |
| 96 | let span = container.querySelector('span') |
| 97 | |
| 98 | htmx.__hideIndicators([span]) |
| 99 | |
| 100 | assert.isFalse(span.classList.contains('htmx-request')) |
| 101 | assert.isUndefined(htmx.__htmxState(span).rc) |
| 102 | }) |
| 103 | |
| 104 | it('works with nested indicators', function () { |
| 105 | let container = createProcessedHTML('<div hx-indicator=".indicator" class="indicator"><span class="indicator"></span></div>') |
| 106 | let outer = container |
| 107 | let inner = container.querySelector('span') |
| 108 | |
| 109 | let indicators = htmx.__showIndicators(container) |
| 110 | |
| 111 | assert.isTrue(outer.classList.contains('htmx-request')) |
| 112 | assert.isTrue(inner.classList.contains('htmx-request')) |
| 113 | |
| 114 | htmx.__hideIndicators(indicators) |
| 115 | |
| 116 | assert.isFalse(outer.classList.contains('htmx-request')) |
| 117 | assert.isFalse(inner.classList.contains('htmx-request')) |
| 118 | }) |
| 119 | |
| 120 | it('maintains separate counts for separate indicators', function () { |
| 121 | let container = createProcessedHTML('<div hx-indicator=".indicator"><span class="indicator"></span><div class="indicator"></div></div>') |
| 122 | let span = container.querySelector('span') |
| 123 | let div = container.querySelector('div') |
| 124 | |
| 125 | htmx.__showIndicators(container) |
| 126 | container.setAttribute('hx-indicator', 'span.indicator') |
| 127 | htmx.__showIndicators(container) |
| 128 | |
| 129 | assert.equal(htmx.__htmxState(span).rc, 2) |
| 130 | assert.equal(htmx.__htmxState(div).rc, 1) |
| 131 | }) |
| 132 | |
| 133 | it('resolves this selector for indicators', function () { |
| 134 | let container = createProcessedHTML('<div hx-indicator="this"><button hx-get="/test" hx-indicator="this"></button></div>'); |
| 135 | let button = container.querySelector('button'); |
| 136 | |
| 137 | let indicators = htmx.__showIndicators(button); |
| 138 | |
| 139 | assert.isTrue(button.classList.contains('htmx-request')); |
| 140 | assert.equal(indicators.length, 1); |
| 141 | assert.equal(indicators[0], button); |
| 142 | }) |
| 143 | |
| 144 | it('resolves this selector with inherited indicator', function () { |
| 145 | let outer = createProcessedHTML('<div hx-indicator:inherited="this"><button hx-get="/test"></button></div>'); |
| 146 | let button = outer.querySelector('button'); |
| 147 | |
| 148 | let indicators = htmx.__showIndicators(button); |
| 149 | |
| 150 | assert.isTrue(outer.classList.contains('htmx-request')); |
| 151 | assert.equal(indicators.length, 1); |
| 152 | assert.equal(indicators[0], outer); |
| 153 | }) |
| 154 | |
| 155 | it('resolves this selector respecting indicator override', function () { |
| 156 | let html = '<div hx-indicator="this"><button hx-get="/test" hx-indicator=".other" class="other"></button></div>'; |
| 157 | let outer = createProcessedHTML(html); |
| 158 | let button = outer.querySelector('button'); |
| 159 | |
| 160 | let indicators = htmx.__showIndicators(button); |
| 161 | |
| 162 | assert.isFalse(outer.classList.contains('htmx-request')); |
| 163 | assert.isTrue(button.classList.contains('htmx-request')); |
| 164 | }) |
| 165 | |
| 166 | it('resolves this selector with append for indicators', function () { |
| 167 | let html = '<div hx-indicator:inherited="this"><button hx-get="/test" hx-indicator:append="this"></button></div>'; |
| 168 | let outer = createProcessedHTML(html); |
| 169 | let button = outer.querySelector('button'); |
| 170 | |
| 171 | let indicators = htmx.__showIndicators(button); |
| 172 | |
| 173 | assert.isTrue(outer.classList.contains('htmx-request')); |
| 174 | assert.isTrue(button.classList.contains('htmx-request')); |
| 175 | assert.equal(indicators.length, 2); |
| 176 | }) |
| 177 | |
| 178 | it('resolves this selector with comma-separated indicator values', function () { |
| 179 | let html = '<div class="other"><button hx-get="/test" hx-indicator="this, .other"></button></div>'; |
| 180 | let container = createProcessedHTML(html); |
| 181 | let button = container.querySelector('button'); |
| 182 | |
| 183 | let indicators = htmx.__showIndicators(button); |
| 184 | |
| 185 | assert.isTrue(button.classList.contains('htmx-request')); |
| 186 | assert.isTrue(container.classList.contains('htmx-request')); |
| 187 | assert.equal(indicators.length, 2); |
| 188 | }) |
| 189 | |
| 190 | }); |