| 1 | describe('__extractHxHeaders unit tests', function() { |
| 2 | |
| 3 | beforeEach(function() { |
| 4 | setupTest(); |
| 5 | }); |
| 6 | |
| 7 | afterEach(function() { |
| 8 | cleanupTest(); |
| 9 | }); |
| 10 | |
| 11 | it('extracts HX headers from response', function () { |
| 12 | let ctx = { |
| 13 | response: { |
| 14 | raw: { |
| 15 | headers: new Headers({ |
| 16 | 'HX-Trigger': 'myEvent', |
| 17 | 'HX-Redirect': '/new-page', |
| 18 | 'Content-Type': 'text/html' |
| 19 | }) |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | htmx.__extractHxHeaders(ctx) |
| 25 | |
| 26 | assert.equal(ctx.hx.trigger, 'myEvent') |
| 27 | assert.equal(ctx.hx.redirect, '/new-page') |
| 28 | assert.isUndefined(ctx.hx.contenttype) |
| 29 | }) |
| 30 | |
| 31 | it('converts header names to lowercase and removes hyphens', function () { |
| 32 | let ctx = { |
| 33 | response: { |
| 34 | raw: { |
| 35 | headers: new Headers({ |
| 36 | 'HX-Push-Url': '/new-url', |
| 37 | 'HX-Replace-Url': '/replace-url', |
| 38 | 'HX-Re-Swap': 'outerHTML' |
| 39 | }) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | htmx.__extractHxHeaders(ctx) |
| 45 | |
| 46 | assert.equal(ctx.hx.pushurl, '/new-url') |
| 47 | assert.equal(ctx.hx.replaceurl, '/replace-url') |
| 48 | assert.equal(ctx.hx.reswap, 'outerHTML') |
| 49 | }) |
| 50 | |
| 51 | it('handles empty headers', function () { |
| 52 | let ctx = { |
| 53 | response: { |
| 54 | raw: { |
| 55 | headers: new Headers() |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | htmx.__extractHxHeaders(ctx) |
| 61 | |
| 62 | assert.deepEqual(ctx.hx, {}) |
| 63 | }) |
| 64 | |
| 65 | it('only extracts headers that start with HX-', function () { |
| 66 | let ctx = { |
| 67 | response: { |
| 68 | raw: { |
| 69 | headers: new Headers({ |
| 70 | 'HX-Trigger': 'myEvent', |
| 71 | 'X-Custom-Header': 'value', |
| 72 | 'Content-Type': 'text/html', |
| 73 | 'HX-Refresh': 'true' |
| 74 | }) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | htmx.__extractHxHeaders(ctx) |
| 80 | |
| 81 | assert.equal(ctx.hx.trigger, 'myEvent') |
| 82 | assert.equal(ctx.hx.refresh, 'true') |
| 83 | assert.isUndefined(ctx.hx.customheader) |
| 84 | assert.isUndefined(ctx.hx.contenttype) |
| 85 | }) |
| 86 | |
| 87 | it('handles case-insensitive HX- prefix', function () { |
| 88 | let ctx = { |
| 89 | response: { |
| 90 | raw: { |
| 91 | headers: new Headers({ |
| 92 | 'hx-trigger': 'lowercase', |
| 93 | 'Hx-Redirect': 'mixedcase', |
| 94 | 'HX-REFRESH': 'uppercase' |
| 95 | }) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | htmx.__extractHxHeaders(ctx) |
| 101 | |
| 102 | assert.equal(ctx.hx.trigger, 'lowercase') |
| 103 | assert.equal(ctx.hx.redirect, 'mixedcase') |
| 104 | assert.equal(ctx.hx.refresh, 'uppercase') |
| 105 | }) |
| 106 | |
| 107 | it('overwrites existing ctx.hx object', function () { |
| 108 | let ctx = { |
| 109 | hx: { |
| 110 | oldValue: 'should be removed' |
| 111 | }, |
| 112 | response: { |
| 113 | raw: { |
| 114 | headers: new Headers({ |
| 115 | 'HX-Trigger': 'newEvent' |
| 116 | }) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | htmx.__extractHxHeaders(ctx) |
| 122 | |
| 123 | assert.equal(ctx.hx.trigger, 'newEvent') |
| 124 | assert.isUndefined(ctx.hx.oldValue) |
| 125 | }) |
| 126 | |
| 127 | }); |