|
| 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="intro">Introduction paragraph</p> |
| 23 | + <p class="body">Body paragraph</p> |
| 24 | + <p class="outro">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 standard DOM methods', () => { |
| 52 | + const title = document.querySelector('title'); |
| 53 | + |
| 54 | + expect(title.textContent).to.equal('Test Document'); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('#select', () => { |
| 58 | + it('selects elements using CSS selector', () => { |
| 59 | + const fragment = document.select('p.intro'); |
| 60 | + const paragraph = fragment.querySelector('p'); |
| 61 | + |
| 62 | + expect(paragraph.textContent).to.equal('Introduction paragraph'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('selects multiple elements using CSS selector', () => { |
| 66 | + const fragment = document.select('p'); |
| 67 | + const paragraphs = fragment.querySelectorAll('p'); |
| 68 | + |
| 69 | + expect(paragraphs.length).to.equal(4); |
| 70 | + }); |
| 71 | + |
| 72 | + it('selects elements using array of CSS selectors', () => { |
| 73 | + const fragment = document.select([ 'h1', '.intro' ]); |
| 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('selects content using range selector object', () => { |
| 82 | + const rangeSelector = { |
| 83 | + startAfter: '.intro', |
| 84 | + endBefore: '.outro', |
| 85 | + }; |
| 86 | + const fragment = document.select(rangeSelector); |
| 87 | + const paragraph = fragment.querySelector('p'); |
| 88 | + |
| 89 | + expect(paragraph.textContent).to.equal('Body paragraph'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns empty fragment when selector matches no elements', () => { |
| 93 | + const fragment = document.select('.nonexistent'); |
| 94 | + |
| 95 | + expect(fragment.childNodes.length).to.equal(0); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('#remove', () => { |
| 100 | + let testDocument; |
| 101 | + |
| 102 | + before(() => { |
| 103 | + testDocument = createWebPageDOM(sampleHTML, location); |
| 104 | + }); |
| 105 | + |
| 106 | + it('removes elements using CSS selector', () => { |
| 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 selector', () => { |
| 114 | + const freshDocument = createWebPageDOM(sampleHTML, location); |
| 115 | + |
| 116 | + freshDocument.remove('p'); |
| 117 | + const paragraphs = freshDocument.querySelectorAll('p'); |
| 118 | + |
| 119 | + expect(paragraphs.length).to.equal(0); |
| 120 | + }); |
| 121 | + |
| 122 | + it('removes elements using array of CSS selectors', () => { |
| 123 | + const freshDocument = createWebPageDOM(sampleHTML, location); |
| 124 | + |
| 125 | + freshDocument.remove([ 'nav', '.widget' ]); |
| 126 | + const nav = freshDocument.querySelector('nav'); |
| 127 | + const widget = freshDocument.querySelector('.widget'); |
| 128 | + |
| 129 | + expect(nav).to.be.null; |
| 130 | + expect(widget).to.be.null; |
| 131 | + }); |
| 132 | + |
| 133 | + it('removes content using range selector object', () => { |
| 134 | + const freshDocument = createWebPageDOM(sampleHTML, location); |
| 135 | + const rangeSelector = { |
| 136 | + startAfter: '.intro', |
| 137 | + endBefore: '.outro', |
| 138 | + }; |
| 139 | + |
| 140 | + freshDocument.remove(rangeSelector); |
| 141 | + const bodyParagraph = freshDocument.querySelector('.body'); |
| 142 | + |
| 143 | + expect(bodyParagraph).to.be.null; |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('#selectRange', () => { |
| 148 | + it('creates range using startAfter and endBefore', () => { |
| 149 | + const rangeSelector = { |
| 150 | + startAfter: '.intro', |
| 151 | + endBefore: '.outro', |
| 152 | + }; |
| 153 | + const range = document.selectRange(rangeSelector); |
| 154 | + const fragment = range.cloneContents(); |
| 155 | + const paragraph = fragment.querySelector('p'); |
| 156 | + |
| 157 | + expect(paragraph.textContent).to.equal('Body paragraph'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('creates range using startBefore and endAfter', () => { |
| 161 | + const rangeSelector = { |
| 162 | + startBefore: '.body', |
| 163 | + endAfter: '.body', |
| 164 | + }; |
| 165 | + const range = document.selectRange(rangeSelector); |
| 166 | + const fragment = range.cloneContents(); |
| 167 | + const paragraph = fragment.querySelector('p'); |
| 168 | + |
| 169 | + expect(paragraph.textContent).to.equal('Body paragraph'); |
| 170 | + }); |
| 171 | + |
| 172 | + it('throws error when start selector has no match', () => { |
| 173 | + const rangeSelector = { |
| 174 | + startAfter: '.nonexistent', |
| 175 | + endBefore: '.outro', |
| 176 | + }; |
| 177 | + |
| 178 | + expect(() => document.selectRange(rangeSelector)).to.throw('The "start" selector has no match in document'); |
| 179 | + }); |
| 180 | + |
| 181 | + it('throws error when end selector has no match', () => { |
| 182 | + const rangeSelector = { |
| 183 | + startAfter: '.intro', |
| 184 | + endBefore: '.nonexistent', |
| 185 | + }; |
| 186 | + |
| 187 | + expect(() => document.selectRange(rangeSelector)).to.throw('The "end" selector has no match in document'); |
| 188 | + }); |
| 189 | + |
| 190 | + it('includes range selector in error message when start selector fails', () => { |
| 191 | + const rangeSelector = { |
| 192 | + startAfter: '.missing', |
| 193 | + endBefore: '.outro', |
| 194 | + }; |
| 195 | + |
| 196 | + expect(() => document.selectRange(rangeSelector)).to.throw(JSON.stringify(rangeSelector)); |
| 197 | + }); |
| 198 | + |
| 199 | + it('includes range selector in error message when end selector fails', () => { |
| 200 | + const rangeSelector = { |
| 201 | + startAfter: '.intro', |
| 202 | + endBefore: '.missing', |
| 203 | + }; |
| 204 | + |
| 205 | + expect(() => document.selectRange(rangeSelector)).to.throw(JSON.stringify(rangeSelector)); |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
0 commit comments