| 1 | describe('hx-select-oob', function() { |
| 2 | |
| 3 | beforeEach(() => { |
| 4 | setupTest() |
| 5 | }) |
| 6 | |
| 7 | afterEach(() => { |
| 8 | cleanupTest() |
| 9 | }) |
| 10 | |
| 11 | it('basic hx-select-oob works', async function () { |
| 12 | mockResponse('GET', '/test', '<div id="d1">foo</div><div id="d2">bar</div>') |
| 13 | let div = createProcessedHTML('<div id="target" hx-get="/test" hx-select="#d1" hx-select-oob="#d2" hx-swap="innerHTML">Click</div><div id="d2"></div>') |
| 14 | div.click() |
| 15 | await forRequest() |
| 16 | assertTextContentIs('#target', 'foo') |
| 17 | assertTextContentIs('#d2', 'bar') |
| 18 | }) |
| 19 | |
| 20 | it('multiple hx-select-oobs works', async function () { |
| 21 | mockResponse('GET', '/test', '<div id="d1">foo</div><div id="d2">bar</div><div id="d3">baz</div>') |
| 22 | let div = createProcessedHTML('<div id="target" hx-get="/test" hx-select="#d1" hx-select-oob="#d2, #d3" hx-swap="innerHTML">Click</div><div id="d2"></div><div id="d3"></div>') |
| 23 | div.click() |
| 24 | await forRequest() |
| 25 | assertTextContentIs('#target', 'foo') |
| 26 | assertTextContentIs('#d2', 'bar') |
| 27 | assertTextContentIs('#d3', 'baz') |
| 28 | }) |
| 29 | |
| 30 | it('basic hx-select-oob ignores bad selector', async function () { |
| 31 | mockResponse('GET', '/test', '<div id="d1">foo</div><div id="d2">bar</div>') |
| 32 | let div = createProcessedHTML('<div id="target" hx-get="/test" hx-select="#d1" hx-select-oob="#bad" hx-swap="innerHTML">Click</div><div id="d2"></div>') |
| 33 | div.click() |
| 34 | await forRequest() |
| 35 | assertTextContentIs('#target', 'foo') |
| 36 | assertTextContentIs('#d2', '') |
| 37 | }) |
| 38 | |
| 39 | it('hx-select-oob works with advanced swap style like strip:false and target:', async function () { |
| 40 | mockResponse('GET', '/test', '<div id="d1">foo</div><div id="d2">bar</div>') |
| 41 | createProcessedHTML('<div id="d0" hx-get="/test" hx-select="#d1" hx-select-oob="#d2:afterend strip:false target:#d0" hx-swap="innerHTML">Click</div>') |
| 42 | find('[hx-get]').click() |
| 43 | await forRequest() |
| 44 | assertTextContentIs('#d0', 'foo') |
| 45 | assertTextContentIs('#d2', 'bar') |
| 46 | }) |
| 47 | |
| 48 | it('hx-select-oob works with basic targeting selector', async function () { |
| 49 | mockResponse('GET', '/test', '<div id="d1">foo</div><div id="d2">bar</div>') |
| 50 | let div = createProcessedHTML('<div id="target" hx-get="/test" hx-select="#d1" hx-select-oob="#d2:outerHTML:#d3" hx-swap="innerHTML">Click</div><div class="foo" id="d3"></div>') |
| 51 | div.click() |
| 52 | await forRequest() |
| 53 | assertTextContentIs('#target', 'foo') |
| 54 | assertTextContentIs('#d2', 'bar') |
| 55 | assert.isNull(document.querySelector('.foo')) |
| 56 | }) |
| 57 | |
| 58 | it('hx-select-oob can end with a blank swap style which is ignored', async function () { |
| 59 | mockResponse('GET', '/test', '<div id="d1">foo</div><div class="foo" id="d2">bar</div>') |
| 60 | let div = createProcessedHTML('<div id="target" hx-get="/test" hx-select="#d1" hx-select-oob="#d2:" hx-swap="innerHTML">Click</div><div id="d2"></div>') |
| 61 | div.click() |
| 62 | await forRequest() |
| 63 | assertTextContentIs('#target', 'foo') |
| 64 | let div2 = document.getElementById('d2') |
| 65 | assert.equal(div2.textContent, 'bar') |
| 66 | assert.isTrue(div2.classList.contains('foo')) |
| 67 | }) |
| 68 | |
| 69 | it('basic hx-select-oob works supports non text based selectors', async function () { |
| 70 | mockResponse('GET', '/test', '<div id="d1">foo</div><div class="foo" id="d2">bar</div>') |
| 71 | let div = createProcessedHTML('<div id="target" hx-get="/test" hx-select="#d1" hx-select-oob=".foo" hx-swap="innerHTML">Click</div><div id="d2"></div>') |
| 72 | div.click() |
| 73 | await forRequest() |
| 74 | assertTextContentIs('#target', 'foo') |
| 75 | let div2 = document.getElementById('d2') |
| 76 | assert.equal(div2.textContent, 'bar') |
| 77 | assert.isTrue(div2.classList.contains('foo')) |
| 78 | }) |
| 79 | |
| 80 | it('basic hx-select-oob works with CSS escaped id containing "."', async function () { |
| 81 | mockResponse('GET', '/test', '<div id="d1">foo</div><div id="my.div3">bar</div>') |
| 82 | let div = createProcessedHTML('<div id="target" hx-get="/test" hx-select="#d1" hx-select-oob="#my\\.div3" hx-swap="innerHTML">Click</div><div id="my.div3"></div>') |
| 83 | div.click() |
| 84 | await forRequest() |
| 85 | assertTextContentIs('#target', 'foo') |
| 86 | assertTextContentIs('#my\\.div3', 'bar') |
| 87 | }) |
| 88 | |
| 89 | it('hx-select-oob can select multiple elements with a selector', async function () { |
| 90 | mockResponse('GET', '/test', '<div id="d1">foo</div><div class="foo" id="d2">bar</div><div class="foo" id="d3">baz</div>') |
| 91 | let div = createProcessedHTML('<div id="target" hx-get="/test" hx-select="#d1" hx-select-oob=".foo" hx-swap="innerHTML">Click</div><div id="d2"></div><div id="d3"></div>') |
| 92 | div.click() |
| 93 | await forRequest() |
| 94 | assertTextContentIs('#target', 'foo') |
| 95 | let div2 = document.getElementById('d2') |
| 96 | assert.equal(div2.textContent, 'bar') |
| 97 | assert.isTrue(div2.classList.contains('foo')) |
| 98 | let div3 = document.getElementById('d3') |
| 99 | assert.equal(div3.textContent, 'baz') |
| 100 | assert.isTrue(div3.classList.contains('foo')) |
| 101 | }) |
| 102 | }) |