| 1 | describe('Request Headers', function() { |
| 2 | |
| 3 | beforeEach(function() { |
| 4 | setupTest(); |
| 5 | }); |
| 6 | |
| 7 | afterEach(function() { |
| 8 | cleanupTest(); |
| 9 | }); |
| 10 | |
| 11 | describe('HX-Source header', function() { |
| 12 | |
| 13 | it('formats element with id', function() { |
| 14 | let btn = createProcessedHTML('<button id="test-btn" hx-get="/test"></button>'); |
| 15 | let ctx = htmx.__createRequestContext(btn, new Event('click')); |
| 16 | ctx.request.headers['HX-Source'].should.equal('button#test-btn'); |
| 17 | }); |
| 18 | |
| 19 | it('formats element with neither id nor name', function() { |
| 20 | let btn = createProcessedHTML('<button hx-get="/test"></button>'); |
| 21 | let ctx = htmx.__createRequestContext(btn, new Event('click')); |
| 22 | ctx.request.headers['HX-Source'].should.equal('button'); |
| 23 | }); |
| 24 | |
| 25 | it('formats div element', function() { |
| 26 | let div = createProcessedHTML('<div id="content" hx-get="/test"></div>'); |
| 27 | let ctx = htmx.__createRequestContext(div, new Event('click')); |
| 28 | ctx.request.headers['HX-Source'].should.equal('div#content'); |
| 29 | }); |
| 30 | |
| 31 | it('formats form element', function() { |
| 32 | let form = createProcessedHTML('<form id="my-form" name="contact" hx-post="/test"></form>'); |
| 33 | let ctx = htmx.__createRequestContext(form, new Event('submit')); |
| 34 | ctx.request.headers['HX-Source'].should.equal('form#my-form'); |
| 35 | }); |
| 36 | |
| 37 | }); |
| 38 | |
| 39 | describe('HX-Target header', function() { |
| 40 | |
| 41 | it('formats target element with id', async function() { |
| 42 | createProcessedHTML('<div id="result"></div><button hx-get="js:" hx-target="#result"></button>'); |
| 43 | let btn = document.querySelector('button'); |
| 44 | let ctx = htmx.__createRequestContext(btn, new Event('click')); |
| 45 | await htmx.__handleTriggerEvent(ctx); |
| 46 | ctx.request.headers['HX-Target'].should.equal('div#result'); |
| 47 | }); |
| 48 | |
| 49 | it('formats target when targeting self', async function() { |
| 50 | let btn = createProcessedHTML('<button id="self-btn" hx-get="js:"></button>'); |
| 51 | let ctx = htmx.__createRequestContext(btn, new Event('click')); |
| 52 | await htmx.__handleTriggerEvent(ctx); |
| 53 | ctx.request.headers['HX-Target'].should.equal('button#self-btn'); |
| 54 | }); |
| 55 | |
| 56 | it('formats body target', async function() { |
| 57 | let btn = createProcessedHTML('<button hx-get="js:" hx-target="body"></button>'); |
| 58 | let ctx = htmx.__createRequestContext(btn, new Event('click')); |
| 59 | await htmx.__handleTriggerEvent(ctx); |
| 60 | ctx.request.headers['HX-Target'].should.equal('body'); |
| 61 | }); |
| 62 | |
| 63 | }); |
| 64 | |
| 65 | describe('HX-Request-Type header', function() { |
| 66 | |
| 67 | it('sets to partial for regular element target', async function() { |
| 68 | createProcessedHTML('<div id="result"></div><button hx-get="js:" hx-target="#result"></button>'); |
| 69 | let btn = document.querySelector('button'); |
| 70 | let ctx = htmx.__createRequestContext(btn, new Event('click')); |
| 71 | await htmx.__handleTriggerEvent(ctx); |
| 72 | ctx.request.headers['HX-Request-Type'].should.equal('partial'); |
| 73 | }); |
| 74 | |
| 75 | it('sets to partial when targeting self', async function() { |
| 76 | let btn = createProcessedHTML('<button hx-get="js:"></button>'); |
| 77 | let ctx = htmx.__createRequestContext(btn, new Event('click')); |
| 78 | await htmx.__handleTriggerEvent(ctx); |
| 79 | ctx.request.headers['HX-Request-Type'].should.equal('partial'); |
| 80 | }); |
| 81 | |
| 82 | it('sets to full when targeting body', async function() { |
| 83 | let btn = createProcessedHTML('<button hx-get="js:" hx-target="body"></button>'); |
| 84 | let ctx = htmx.__createRequestContext(btn, new Event('click')); |
| 85 | await htmx.__handleTriggerEvent(ctx); |
| 86 | ctx.request.headers['HX-Request-Type'].should.equal('full'); |
| 87 | }); |
| 88 | |
| 89 | it('sets to full when hx-select is present', async function() { |
| 90 | let btn = createProcessedHTML('<button hx-get="js:" hx-select="#content"></button>'); |
| 91 | let ctx = htmx.__createRequestContext(btn, new Event('click')); |
| 92 | await htmx.__handleTriggerEvent(ctx); |
| 93 | ctx.request.headers['HX-Request-Type'].should.equal('full'); |
| 94 | }); |
| 95 | |
| 96 | it('sets to full when hx-select and body target both present', async function() { |
| 97 | let btn = createProcessedHTML('<button hx-get="js:" hx-target="body" hx-select="#content"></button>'); |
| 98 | let ctx = htmx.__createRequestContext(btn, new Event('click')); |
| 99 | await htmx.__handleTriggerEvent(ctx); |
| 100 | ctx.request.headers['HX-Request-Type'].should.equal('full'); |
| 101 | }); |
| 102 | |
| 103 | }); |
| 104 | |
| 105 | describe('__buildIdentifier method', function() { |
| 106 | |
| 107 | it('builds identifier with id only', function() { |
| 108 | let div = document.createElement('div'); |
| 109 | div.id = 'test'; |
| 110 | htmx.__buildIdentifier(div).should.equal('div#test'); |
| 111 | }); |
| 112 | |
| 113 | it('builds identifier with neither id nor name', function() { |
| 114 | let span = document.createElement('span'); |
| 115 | htmx.__buildIdentifier(span).should.equal('span'); |
| 116 | }); |
| 117 | |
| 118 | it('handles body element', function() { |
| 119 | htmx.__buildIdentifier(document.body).should.equal('body'); |
| 120 | }); |
| 121 | |
| 122 | it('encodes unicode characters in id for safe header usage', function() { |
| 123 | let div = document.createElement('div'); |
| 124 | div.id = '🚀'; |
| 125 | htmx.__buildIdentifier(div).should.equal('div#%F0%9F%9A%80'); |
| 126 | }); |
| 127 | |
| 128 | }); |
| 129 | |
| 130 | }); |