|
| 1 | +import { describe, beforeEach, test, expect } from 'vitest'; |
| 2 | + |
| 3 | +import TestAssertions from '../helpers/test-assertions'; |
| 4 | + |
| 5 | +describe('assert.dom(..., rootElement)', () => { |
| 6 | + let assert: TestAssertions; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + assert = new TestAssertions(); |
| 10 | + }); |
| 11 | + |
| 12 | + test('passing an Element as rootElement', () => { |
| 13 | + document.body.innerHTML = ` |
| 14 | + <span class="target">dedcoy<span> |
| 15 | +
|
| 16 | + <h1 class="parent"> |
| 17 | + <span class="target">real target<span> |
| 18 | + </h1> |
| 19 | + `; |
| 20 | + |
| 21 | + const rootElement = document.querySelector('.parent'); |
| 22 | + |
| 23 | + assert.dom('.target', rootElement).exists({ count: 1 }); |
| 24 | + assert.dom('.target', rootElement).hasText('real target'); |
| 25 | + |
| 26 | + expect(assert.results).toEqual([ |
| 27 | + { |
| 28 | + result: true, |
| 29 | + actual: 'Element .target exists once', |
| 30 | + expected: 'Element .target exists once', |
| 31 | + message: 'Element .target exists once', |
| 32 | + }, |
| 33 | + { |
| 34 | + result: true, |
| 35 | + actual: 'real target', |
| 36 | + expected: 'real target', |
| 37 | + message: 'Element .target has text "real target"', |
| 38 | + }, |
| 39 | + ]); |
| 40 | + }); |
| 41 | + |
| 42 | + test('not passing anything as rootElement', () => { |
| 43 | + document.body.innerHTML = ` |
| 44 | + <span class="target">decoy<span> |
| 45 | +
|
| 46 | + <h1 class="parent"> |
| 47 | + <span class="target">real target<span> |
| 48 | + </h1> |
| 49 | + `; |
| 50 | + |
| 51 | + assert.dom('.target').exists({ count: 2 }); |
| 52 | + |
| 53 | + expect(assert.results).toEqual([ |
| 54 | + { |
| 55 | + result: true, |
| 56 | + actual: 'Element .target exists twice', |
| 57 | + expected: 'Element .target exists twice', |
| 58 | + message: 'Element .target exists twice', |
| 59 | + }, |
| 60 | + ]); |
| 61 | + }); |
| 62 | + |
| 63 | + test('passing document as rootElement', () => { |
| 64 | + document.body.innerHTML = ` |
| 65 | + <span class="target">decoy<span> |
| 66 | +
|
| 67 | + <h1 class="parent"> |
| 68 | + <span class="target">real target<span> |
| 69 | + </h1> |
| 70 | + `; |
| 71 | + |
| 72 | + assert.dom('.target', document).exists({ count: 2 }); |
| 73 | + |
| 74 | + expect(assert.results).toEqual([ |
| 75 | + { |
| 76 | + result: true, |
| 77 | + actual: 'Element .target exists twice', |
| 78 | + expected: 'Element .target exists twice', |
| 79 | + message: 'Element .target exists twice', |
| 80 | + }, |
| 81 | + ]); |
| 82 | + }); |
| 83 | + |
| 84 | + test('passing null as rootElement', () => { |
| 85 | + document.body.innerHTML = ` |
| 86 | + <span class="target">decoy<span> |
| 87 | +
|
| 88 | + <h1 class="parent"> |
| 89 | + <span class="target">real target<span> |
| 90 | + </h1> |
| 91 | + `; |
| 92 | + |
| 93 | + assert.dom('.target', null).exists({ count: 2 }); |
| 94 | + |
| 95 | + expect(assert.results).toEqual([ |
| 96 | + { |
| 97 | + result: true, |
| 98 | + actual: 'Element .target exists twice', |
| 99 | + expected: 'Element .target exists twice', |
| 100 | + message: 'Element .target exists twice', |
| 101 | + }, |
| 102 | + ]); |
| 103 | + }); |
| 104 | + |
| 105 | + test('passing shadow root as rootElement', () => { |
| 106 | + document.body.innerHTML = ` |
| 107 | + <div id="container"> |
| 108 | + <span class="target">decoy<span> |
| 109 | + </div> |
| 110 | + `; |
| 111 | + |
| 112 | + const container = document.getElementById('container'); |
| 113 | + const shadowRoot = container.attachShadow({ mode: 'closed' }); |
| 114 | + |
| 115 | + shadowRoot.innerHTML = '<span class="target">real target<span>'; |
| 116 | + |
| 117 | + assert.dom('.target').exists({ count: 1 }, 'Only decoy element is found outside shadow root'); |
| 118 | + assert.dom('.target').hasText('real target', 'decoy element text'); |
| 119 | + |
| 120 | + assert.dom('.target', shadowRoot).exists({ count: 1 }, 'Only target found in shadow root'); |
| 121 | + assert.dom('.target', shadowRoot).hasText('real target', 'Target element text'); |
| 122 | + |
| 123 | + console.log(assert.results); |
| 124 | + |
| 125 | + expect(assert.results).toEqual([ |
| 126 | + { |
| 127 | + result: true, |
| 128 | + actual: 'Element .target exists once', |
| 129 | + expected: 'Element .target exists once', |
| 130 | + message: 'Only decoy element is found outside shadow root', |
| 131 | + }, |
| 132 | + { |
| 133 | + result: false, |
| 134 | + actual: 'decoy', |
| 135 | + expected: 'real target', |
| 136 | + message: 'decoy element text', |
| 137 | + }, |
| 138 | + { |
| 139 | + result: true, |
| 140 | + actual: 'Element .target exists once', |
| 141 | + expected: 'Element .target exists once', |
| 142 | + message: 'Only target found in shadow root', |
| 143 | + }, |
| 144 | + { |
| 145 | + result: true, |
| 146 | + actual: 'real target', |
| 147 | + expected: 'real target', |
| 148 | + message: 'Target element text', |
| 149 | + }, |
| 150 | + ]); |
| 151 | + }); |
| 152 | +}); |
0 commit comments