|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +import createWebPageDOM from './dom.js'; |
| 4 | + |
| 5 | +describe('createWebPageDOM', () => { |
| 6 | + const sampleHTML = ` |
| 7 | + <!DOCTYPE html> |
| 8 | + <html> |
| 9 | + <head> |
| 10 | + <title>Test Document</title> |
| 11 | + </head> |
| 12 | + <body> |
| 13 | + <header id="header"> |
| 14 | + <h1>Main Title</h1> |
| 15 | + <nav class="navigation"> |
| 16 | + <a href="/home">Home</a> |
| 17 | + <a href="/about">About</a> |
| 18 | + </nav> |
| 19 | + </header> |
| 20 | + <main> |
| 21 | + <article id="content"> |
| 22 | + <p class="introduction">Introduction paragraph</p> |
| 23 | + <p class="central">Central paragraph</p> |
| 24 | + <p class="conclusion">Conclusion paragraph</p> |
| 25 | + </article> |
| 26 | + <aside class="sidebar"> |
| 27 | + <div class="widget">Widget content</div> |
| 28 | + </aside> |
| 29 | + </main> |
| 30 | + <footer id="footer"> |
| 31 | + <p>Footer content</p> |
| 32 | + </footer> |
| 33 | + </body> |
| 34 | + </html> |
| 35 | + `; |
| 36 | + const location = 'https://example.com/test'; |
| 37 | + let document; |
| 38 | + |
| 39 | + before(() => { |
| 40 | + document = createWebPageDOM(sampleHTML, location); |
| 41 | + }); |
| 42 | + |
| 43 | + it('creates a DOM document from HTML content', () => { |
| 44 | + expect(document.documentElement.tagName).to.equal('HTML'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('sets the document location', () => { |
| 48 | + expect(document.location.href).to.equal(location); |
| 49 | + }); |
| 50 | + |
| 51 | + it('provides access to the DOM API', () => { |
| 52 | + const title = document.querySelector('title'); |
| 53 | + |
| 54 | + expect(title.textContent).to.equal('Test Document'); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('#select', () => { |
| 58 | + it('returns elements using CSS selectors', () => { |
| 59 | + const fragment = document.select('p.introduction'); |
| 60 | + const paragraph = fragment.querySelector('p'); |
| 61 | + |
| 62 | + expect(paragraph.textContent).to.equal('Introduction paragraph'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('returns multiple elements using CSS selectors', () => { |
| 66 | + const fragment = document.select('p'); |
| 67 | + const paragraphs = fragment.querySelectorAll('p'); |
| 68 | + |
| 69 | + expect(paragraphs.length).to.equal(4); |
| 70 | + }); |
| 71 | + |
| 72 | + it('returns elements using an array of CSS selectors', () => { |
| 73 | + const fragment = document.select([ 'h1', '.introduction' ]); |
| 74 | + const heading = fragment.querySelector('h1'); |
| 75 | + const paragraph = fragment.querySelector('p'); |
| 76 | + |
| 77 | + expect(heading.textContent).to.equal('Main Title'); |
| 78 | + expect(paragraph.textContent).to.equal('Introduction paragraph'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns content using a range selector object', () => { |
| 82 | + const rangeSelector = { |
| 83 | + startAfter: '.introduction', |
| 84 | + endBefore: '.conclusion', |
| 85 | + }; |
| 86 | + const fragment = document.select(rangeSelector); |
| 87 | + const paragraph = fragment.querySelector('p'); |
| 88 | + |
| 89 | + expect(paragraph.textContent).to.equal('Central paragraph'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns null when the selector matches no element', () => { |
| 93 | + const result = document.select('.nonexistent'); |
| 94 | + |
| 95 | + expect(result).to.be.null; |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('#remove', () => { |
| 100 | + let testDocument; |
| 101 | + |
| 102 | + beforeEach(() => { |
| 103 | + testDocument = createWebPageDOM(sampleHTML, location); |
| 104 | + }); |
| 105 | + |
| 106 | + it('removes elements using CSS selectors', () => { |
| 107 | + testDocument.remove('.sidebar'); |
| 108 | + const sidebar = testDocument.querySelector('.sidebar'); |
| 109 | + |
| 110 | + expect(sidebar).to.be.null; |
| 111 | + }); |
| 112 | + |
| 113 | + it('removes multiple elements using CSS selectors', () => { |
| 114 | + testDocument.remove('p'); |
| 115 | + const paragraphs = testDocument.querySelectorAll('p'); |
| 116 | + |
| 117 | + expect(paragraphs.length).to.equal(0); |
| 118 | + }); |
| 119 | + |
| 120 | + it('removes elements using an array of CSS selectors', () => { |
| 121 | + testDocument.remove([ 'nav', '.widget' ]); |
| 122 | + const nav = testDocument.querySelector('nav'); |
| 123 | + const widget = testDocument.querySelector('.widget'); |
| 124 | + |
| 125 | + expect(nav).to.be.null; |
| 126 | + expect(widget).to.be.null; |
| 127 | + }); |
| 128 | + |
| 129 | + it('removes content using a range selector object', () => { |
| 130 | + const rangeSelector = { |
| 131 | + startAfter: '.introduction', |
| 132 | + endBefore: '.conclusion', |
| 133 | + }; |
| 134 | + |
| 135 | + testDocument.remove(rangeSelector); |
| 136 | + const bodyParagraph = testDocument.querySelector('.central'); |
| 137 | + |
| 138 | + expect(bodyParagraph).to.be.null; |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe('#selectRange', () => { |
| 143 | + it('creates a range using startAfter and endBefore', () => { |
| 144 | + const rangeSelector = { |
| 145 | + startAfter: '.introduction', |
| 146 | + endBefore: '.conclusion', |
| 147 | + }; |
| 148 | + const range = document.selectRange(rangeSelector); |
| 149 | + const fragment = range.cloneContents(); |
| 150 | + const paragraph = fragment.querySelector('p'); |
| 151 | + |
| 152 | + expect(paragraph.textContent).to.equal('Central paragraph'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('creates a range using startBefore and endAfter', () => { |
| 156 | + const rangeSelector = { |
| 157 | + startBefore: '.central', |
| 158 | + endAfter: '.central', |
| 159 | + }; |
| 160 | + const range = document.selectRange(rangeSelector); |
| 161 | + const fragment = range.cloneContents(); |
| 162 | + const paragraph = fragment.querySelector('p'); |
| 163 | + |
| 164 | + expect(paragraph.textContent).to.equal('Central paragraph'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('throws a clear error when the startBefore selector has no match', () => { |
| 168 | + const rangeSelector = { |
| 169 | + startBefore: '.nonexistent', |
| 170 | + endBefore: '.conclusion', |
| 171 | + }; |
| 172 | + |
| 173 | + expect(() => document.selectRange(rangeSelector)).to.throw('"start" selector has no match'); |
| 174 | + expect(() => document.selectRange(rangeSelector)).to.throw(JSON.stringify(rangeSelector)); |
| 175 | + }); |
| 176 | + |
| 177 | + it('throws a clear error when the startAfter selector has no match', () => { |
| 178 | + const rangeSelector = { |
| 179 | + startAfter: '.nonexistent', |
| 180 | + endBefore: '.conclusion', |
| 181 | + }; |
| 182 | + |
| 183 | + expect(() => document.selectRange(rangeSelector)).to.throw('"start" selector has no match'); |
| 184 | + expect(() => document.selectRange(rangeSelector)).to.throw(JSON.stringify(rangeSelector)); |
| 185 | + }); |
| 186 | + |
| 187 | + it('throws a clear error when the endBefore selector has no match', () => { |
| 188 | + const rangeSelector = { |
| 189 | + startAfter: '.introduction', |
| 190 | + endBefore: '.nonexistent', |
| 191 | + }; |
| 192 | + |
| 193 | + expect(() => document.selectRange(rangeSelector)).to.throw('"end" selector has no match'); |
| 194 | + expect(() => document.selectRange(rangeSelector)).to.throw(JSON.stringify(rangeSelector)); |
| 195 | + }); |
| 196 | + |
| 197 | + it('throws a clear error when the endAfter selector has no match', () => { |
| 198 | + const rangeSelector = { |
| 199 | + startAfter: '.introduction', |
| 200 | + endAfter: '.nonexistent', |
| 201 | + }; |
| 202 | + |
| 203 | + expect(() => document.selectRange(rangeSelector)).to.throw('"end" selector has no match'); |
| 204 | + expect(() => document.selectRange(rangeSelector)).to.throw(JSON.stringify(rangeSelector)); |
| 205 | + }); |
| 206 | + }); |
| 207 | +}); |
0 commit comments