| 1 | describe('__resolveHistoryAction unit tests', function() { |
| 2 | |
| 3 | beforeEach(function() { |
| 4 | setupTest(); |
| 5 | }); |
| 6 | |
| 7 | afterEach(function() { |
| 8 | cleanupTest(); |
| 9 | }); |
| 10 | |
| 11 | it('returns null when no push or replace', function() { |
| 12 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 13 | let ctx = { sourceElement: div } |
| 14 | assert.isNull(htmx.__resolveHistoryAction(ctx)) |
| 15 | }) |
| 16 | |
| 17 | it('returns push with path from hx-push-url attribute', function() { |
| 18 | let div = createProcessedHTML('<div hx-get="/test" hx-push-url="/pushed"></div>') |
| 19 | let ctx = { sourceElement: div, push: '/pushed' } |
| 20 | let action = htmx.__resolveHistoryAction(ctx) |
| 21 | assert.equal(action.type, 'push') |
| 22 | assert.equal(action.path, '/pushed') |
| 23 | }) |
| 24 | |
| 25 | it('returns replace with path from hx-replace-url attribute', function() { |
| 26 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 27 | let ctx = { sourceElement: div, replace: '/replaced' } |
| 28 | let action = htmx.__resolveHistoryAction(ctx) |
| 29 | assert.equal(action.type, 'replace') |
| 30 | assert.equal(action.path, '/replaced') |
| 31 | }) |
| 32 | |
| 33 | it('server HX-Push-Url header overrides attribute', function() { |
| 34 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 35 | let ctx = { sourceElement: div, push: '/from-attr', hx: { pushurl: '/from-header' } } |
| 36 | let action = htmx.__resolveHistoryAction(ctx) |
| 37 | assert.equal(action.type, 'push') |
| 38 | assert.equal(action.path, '/from-header') |
| 39 | }) |
| 40 | |
| 41 | it('server HX-Replace-Url header overrides attribute', function() { |
| 42 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 43 | let ctx = { sourceElement: div, replace: '/from-attr', hx: { replaceurl: '/from-header' } } |
| 44 | let action = htmx.__resolveHistoryAction(ctx) |
| 45 | assert.equal(action.type, 'replace') |
| 46 | assert.equal(action.path, '/from-header') |
| 47 | }) |
| 48 | |
| 49 | it('push "false" returns null', function() { |
| 50 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 51 | let ctx = { sourceElement: div, push: 'false' } |
| 52 | assert.isNull(htmx.__resolveHistoryAction(ctx)) |
| 53 | }) |
| 54 | |
| 55 | it('replace "false" returns null', function() { |
| 56 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 57 | let ctx = { sourceElement: div, replace: 'false' } |
| 58 | assert.isNull(htmx.__resolveHistoryAction(ctx)) |
| 59 | }) |
| 60 | |
| 61 | it('HX-Push-Url: false does not block HX-Replace-Url', function() { |
| 62 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 63 | let ctx = { sourceElement: div, hx: { pushurl: 'false', replaceurl: '/new-path' } } |
| 64 | let action = htmx.__resolveHistoryAction(ctx) |
| 65 | assert.equal(action.type, 'replace') |
| 66 | assert.equal(action.path, '/new-path') |
| 67 | }) |
| 68 | |
| 69 | it('HX-Replace-Url: false does not block HX-Push-Url', function() { |
| 70 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 71 | let ctx = { sourceElement: div, hx: { pushurl: '/new-path', replaceurl: 'false' } } |
| 72 | let action = htmx.__resolveHistoryAction(ctx) |
| 73 | assert.equal(action.type, 'push') |
| 74 | assert.equal(action.path, '/new-path') |
| 75 | }) |
| 76 | |
| 77 | it('push "true" resolves path from response URL', function() { |
| 78 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 79 | let ctx = { |
| 80 | sourceElement: div, |
| 81 | push: 'true', |
| 82 | response: { raw: { url: 'http://localhost/resolved' } }, |
| 83 | request: { action: '/fallback' } |
| 84 | } |
| 85 | let action = htmx.__resolveHistoryAction(ctx) |
| 86 | assert.equal(action.type, 'push') |
| 87 | assert.equal(action.path, '/resolved') |
| 88 | }) |
| 89 | |
| 90 | it('push "true" falls back to request action', function() { |
| 91 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 92 | let ctx = { |
| 93 | sourceElement: div, |
| 94 | push: 'true', |
| 95 | response: { raw: {} }, |
| 96 | request: { action: '/fallback' } |
| 97 | } |
| 98 | let action = htmx.__resolveHistoryAction(ctx) |
| 99 | assert.equal(action.type, 'push') |
| 100 | assert.equal(action.path, '/fallback') |
| 101 | }) |
| 102 | |
| 103 | it('push takes precedence over replace', function() { |
| 104 | let div = createProcessedHTML('<div hx-get="/test"></div>') |
| 105 | let ctx = { sourceElement: div, push: '/push-path', replace: '/replace-path' } |
| 106 | let action = htmx.__resolveHistoryAction(ctx) |
| 107 | assert.equal(action.type, 'push') |
| 108 | assert.equal(action.path, '/push-path') |
| 109 | }) |
| 110 | |
| 111 | }) |