| 1 | describe('__parseSwapSpec unit tests', function() { |
| 2 | |
| 3 | it('parses basic swap styles', function () { |
| 4 | assert.equal(htmx.__parseSwapSpec('innerHTML').style, 'innerHTML') |
| 5 | assert.equal(htmx.__parseSwapSpec('outerHTML').style, 'outerHTML') |
| 6 | assert.equal(htmx.__parseSwapSpec('beforebegin').style, 'beforebegin') |
| 7 | assert.equal(htmx.__parseSwapSpec('afterbegin').style, 'afterbegin') |
| 8 | assert.equal(htmx.__parseSwapSpec('beforeend').style, 'beforeend') |
| 9 | assert.equal(htmx.__parseSwapSpec('afterend').style, 'afterend') |
| 10 | }) |
| 11 | |
| 12 | it('normalizes legacy swap styles', function () { |
| 13 | assert.equal(htmx.__parseSwapSpec('prepend').style, 'afterbegin') |
| 14 | assert.equal(htmx.__parseSwapSpec('append').style, 'beforeend') |
| 15 | assert.equal(htmx.__parseSwapSpec('before').style, 'beforebegin') |
| 16 | assert.equal(htmx.__parseSwapSpec('after').style, 'afterend') |
| 17 | }) |
| 18 | |
| 19 | it('parses swap delay modifier', function () { |
| 20 | let spec = htmx.__parseSwapSpec('innerHTML swap:100ms') |
| 21 | assert.equal(spec.style, 'innerHTML') |
| 22 | assert.equal(spec.swap, '100ms') |
| 23 | }) |
| 24 | |
| 25 | it('parses transition modifier', function () { |
| 26 | assert.equal(htmx.__parseSwapSpec('innerHTML transition:true').transition, true) |
| 27 | assert.equal(htmx.__parseSwapSpec('innerHTML transition:false').transition, false) |
| 28 | }) |
| 29 | |
| 30 | it('parses ignoreTitle modifier', function () { |
| 31 | assert.equal(htmx.__parseSwapSpec('innerHTML ignoreTitle:true').ignoreTitle, true) |
| 32 | assert.equal(htmx.__parseSwapSpec('innerHTML ignoreTitle:false').ignoreTitle, false) |
| 33 | }) |
| 34 | |
| 35 | it('parses strip modifier', function () { |
| 36 | assert.equal(htmx.__parseSwapSpec('innerHTML strip:true').strip, true) |
| 37 | assert.equal(htmx.__parseSwapSpec('innerHTML strip:false').strip, false) |
| 38 | }) |
| 39 | |
| 40 | it('parses focus-scroll modifier', function () { |
| 41 | assert.equal(htmx.__parseSwapSpec('innerHTML focus-scroll:true')['focus-scroll'], true) |
| 42 | assert.equal(htmx.__parseSwapSpec('innerHTML focus-scroll:false')['focus-scroll'], false) |
| 43 | }) |
| 44 | |
| 45 | it('parses scroll modifier', function () { |
| 46 | assert.equal(htmx.__parseSwapSpec('innerHTML scroll:top').scroll, 'top') |
| 47 | assert.equal(htmx.__parseSwapSpec('innerHTML scroll:bottom').scroll, 'bottom') |
| 48 | }) |
| 49 | |
| 50 | it('parses show modifier', function () { |
| 51 | assert.equal(htmx.__parseSwapSpec('innerHTML show:top').show, 'top') |
| 52 | assert.equal(htmx.__parseSwapSpec('innerHTML show:bottom').show, 'bottom') |
| 53 | }) |
| 54 | |
| 55 | it('parses target modifier', function () { |
| 56 | assert.equal(htmx.__parseSwapSpec('innerHTML target:#foo').target, '#foo') |
| 57 | }) |
| 58 | |
| 59 | it('parses target with spaces', function () { |
| 60 | assert.equal(htmx.__parseSwapSpec('innerHTML target:"#foo .bar"').target, '#foo .bar') |
| 61 | }) |
| 62 | |
| 63 | it('parses scroll with scrollTarget', function () { |
| 64 | let spec = htmx.__parseSwapSpec('innerHTML scroll:top scrollTarget:#container') |
| 65 | assert.equal(spec.scroll, 'top') |
| 66 | assert.equal(spec.scrollTarget, '#container') |
| 67 | }) |
| 68 | |
| 69 | it('parses show with showTarget', function () { |
| 70 | let spec = htmx.__parseSwapSpec('innerHTML show:bottom showTarget:.content') |
| 71 | assert.equal(spec.show, 'bottom') |
| 72 | assert.equal(spec.showTarget, '.content') |
| 73 | }) |
| 74 | |
| 75 | it('parses multiple modifiers', function () { |
| 76 | let spec = htmx.__parseSwapSpec('innerHTML swap:100ms transition:true') |
| 77 | assert.equal(spec.style, 'innerHTML') |
| 78 | assert.equal(spec.swap, '100ms') |
| 79 | assert.equal(spec.transition, true) |
| 80 | }) |
| 81 | |
| 82 | it('uses default swap when empty', function () { |
| 83 | let spec = htmx.__parseSwapSpec('') |
| 84 | assert.equal(spec.style, htmx.config.defaultSwap) |
| 85 | }) |
| 86 | |
| 87 | it('parses legacy style names with modifiers', function () { |
| 88 | let spec = htmx.__parseSwapSpec('prepend swap:10ms') |
| 89 | assert.equal(spec.style, 'afterbegin') |
| 90 | assert.equal(spec.swap, '10ms') |
| 91 | }) |
| 92 | |
| 93 | }); |