| 1 | describe('htmx.config.prefix functionality', function() { |
| 2 | |
| 3 | beforeEach(function() { |
| 4 | setupTest(this); |
| 5 | }); |
| 6 | |
| 7 | afterEach(function() { |
| 8 | cleanupTest(); |
| 9 | }); |
| 10 | |
| 11 | it('default prefix (empty string) works normally', async function() { |
| 12 | htmx.config.prefix = ""; |
| 13 | mockResponse('GET', '/test', 'Success'); |
| 14 | |
| 15 | createProcessedHTML('<button id="btn" hx-get="/test">Click</button>'); |
| 16 | find('#btn').click() |
| 17 | await forRequest(); |
| 18 | |
| 19 | let lastCall = lastFetch(); |
| 20 | assert.equal(lastCall.url, '/test'); |
| 21 | }); |
| 22 | |
| 23 | it('custom prefix replaces hx- in attributes', function() { |
| 24 | htmx.config.prefix = "data-hx-"; |
| 25 | |
| 26 | let btn = createDisconnectedHTML('<button data-hx-get="/custom"></button>'); |
| 27 | assert.equal(htmx.__attr(btn, "hx-get"), "/custom"); |
| 28 | assert.equal(htmx.__attr(btn, "hx-target"), null); |
| 29 | |
| 30 | htmx.config.prefix = ""; |
| 31 | }); |
| 32 | |
| 33 | it('custom prefix is used in __attributeValue', function() { |
| 34 | htmx.config.prefix = "data-hx-"; |
| 35 | |
| 36 | let btn = createDisconnectedHTML('<button data-hx-get="/custom" data-hx-target="#result">Click</button>'); |
| 37 | |
| 38 | let getValue = htmx.__attributeValue(btn, "hx-get"); |
| 39 | assert.equal(getValue, "/custom"); |
| 40 | |
| 41 | let targetValue = htmx.__attributeValue(btn, "hx-target"); |
| 42 | assert.equal(targetValue, "#result"); |
| 43 | |
| 44 | htmx.config.prefix = ""; |
| 45 | }); |
| 46 | |
| 47 | it('custom prefix works with trigger attribute', function() { |
| 48 | htmx.config.prefix = "data-hx-"; |
| 49 | |
| 50 | let called = 0; |
| 51 | let btn = createDisconnectedHTML('<button>Click</button>'); |
| 52 | btn.setAttribute('data-hx-trigger', 'click'); |
| 53 | htmx.__htmxProp(btn); |
| 54 | htmx.__initializeTriggers(btn, () => called++); |
| 55 | |
| 56 | btn.click(); |
| 57 | assert.equal(called, 1); |
| 58 | |
| 59 | htmx.config.prefix = ""; |
| 60 | }); |
| 61 | |
| 62 | it('empty prefix disables secondary prefix', function() { |
| 63 | htmx.config.prefix = ""; |
| 64 | |
| 65 | let btn = createDisconnectedHTML('<button hx-get="/primary"></button>'); |
| 66 | assert.equal(htmx.__attr(btn, "hx-get"), "/primary"); |
| 67 | }); |
| 68 | |
| 69 | it('hx- always works regardless of prefix setting', function() { |
| 70 | htmx.config.prefix = "data-hx-"; |
| 71 | |
| 72 | let btn = createDisconnectedHTML('<button hx-get="/primary"></button>'); |
| 73 | assert.equal(htmx.__attributeValue(btn, "hx-get"), "/primary"); |
| 74 | |
| 75 | htmx.config.prefix = ""; |
| 76 | }); |
| 77 | |
| 78 | it('secondary prefix found when hx- is absent', function() { |
| 79 | htmx.config.prefix = "data-hx-"; |
| 80 | |
| 81 | let btn = createDisconnectedHTML('<button data-hx-get="/secondary"></button>'); |
| 82 | assert.equal(htmx.__attributeValue(btn, "hx-get"), "/secondary"); |
| 83 | |
| 84 | htmx.config.prefix = ""; |
| 85 | }); |
| 86 | |
| 87 | it('hx- wins over secondary prefix when both present', function() { |
| 88 | htmx.config.prefix = "data-hx-"; |
| 89 | |
| 90 | let btn = createDisconnectedHTML('<button hx-get="/first" data-hx-get="/second"></button>'); |
| 91 | assert.equal(htmx.__attributeValue(btn, "hx-get"), "/first"); |
| 92 | |
| 93 | htmx.config.prefix = ""; |
| 94 | }); |
| 95 | |
| 96 | it('inheritance works with secondary prefix on parent', function() { |
| 97 | htmx.config.prefix = "data-hx-"; |
| 98 | |
| 99 | let container = createDisconnectedHTML( |
| 100 | '<div data-hx-target:inherited="#output">' + |
| 101 | ' <button hx-get="/test"></button>' + |
| 102 | '</div>' |
| 103 | ); |
| 104 | let button = container.querySelector('button'); |
| 105 | assert.equal(htmx.__attributeValue(button, "hx-target"), "#output"); |
| 106 | |
| 107 | htmx.config.prefix = ""; |
| 108 | }); |
| 109 | |
| 110 | it('custom prefix works with attribute inheritance', function() { |
| 111 | htmx.config.prefix = "data-hx-"; |
| 112 | |
| 113 | let container = createDisconnectedHTML( |
| 114 | '<div data-hx-target:inherited="#output">' + |
| 115 | ' <button data-hx-get="/test">Click</button>' + |
| 116 | '</div>' |
| 117 | ); |
| 118 | let button = container.querySelector('button'); |
| 119 | let targetValue = htmx.__attributeValue(button, "hx-target"); |
| 120 | assert.equal(targetValue, "#output"); |
| 121 | |
| 122 | htmx.config.prefix = ""; |
| 123 | }); |
| 124 | }); |