|
| 1 | +const SimpleSource = require('../../lib/sources/simple-source.js') |
| 2 | +const HtmlSource = require('../../lib/sources/html-source.js') |
| 3 | + |
| 4 | +describe('sources/html-source.js', () => { |
| 5 | + it('should return the type', () => { |
| 6 | + expect(new HtmlSource('<html></html>')).to.have.property('type', 'html') |
| 7 | + }) |
| 8 | + |
| 9 | + describe('#join', () => { |
| 10 | + it('should join to another HtmlSource', () => { |
| 11 | + const sourceA = new HtmlSource('<html></html>') |
| 12 | + const sourceB = new HtmlSource('<html></html>') |
| 13 | + expect(sourceA.join(sourceB)).to.have.property('type', 'html') |
| 14 | + expect(sourceB.join(sourceA)).to.have.property('type', 'html') |
| 15 | + }) |
| 16 | + |
| 17 | + it('should join to a malformed HtmlSource', () => { |
| 18 | + const sourceA = new HtmlSource('<html></html>') |
| 19 | + const sourceB = new HtmlSource('<html') |
| 20 | + expect(sourceA.join(sourceB)).to.have.property('type', 'html') |
| 21 | + expect(sourceB.join(sourceA)).to.have.property('type', 'html') |
| 22 | + }) |
| 23 | + |
| 24 | + it('should not join to another non-HtmlSource', () => { |
| 25 | + const sourceA = new HtmlSource('<html></html>') |
| 26 | + const sourceB = new SimpleSource('other content') |
| 27 | + expect(() => sourceA.join(sourceB)).to.throw() |
| 28 | + expect(() => sourceB.join(sourceA)).to.throw() |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + describe('#contains', () => { |
| 33 | + context('when html is simple', () => { |
| 34 | + const html = ` |
| 35 | + <!DOCTYPE html> |
| 36 | + <html lang="en"> |
| 37 | + <head> |
| 38 | + <title>Basic HTML Example</title> |
| 39 | + <link href="app.css" rel="stylesheet"> |
| 40 | + <script src="app.js" type="text/javascript"></script> |
| 41 | + </head> |
| 42 | + <body> |
| 43 | + <div id="my-hero-element" class="container"> |
| 44 | + <div class="several classes in-a-row"> |
| 45 | + <h1>My Header</h1> |
| 46 | + <p class="lead">Examplelongtext</p> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + <script> |
| 50 | + const myJsVar = "my-javascript-class" |
| 51 | + </script> |
| 52 | + </body> |
| 53 | + </html> |
| 54 | + ` |
| 55 | + |
| 56 | + const source = new HtmlSource(html) |
| 57 | + |
| 58 | + it('should find tokens as elements', () => { |
| 59 | + expect(source.contains('div')).to.equal(true) |
| 60 | + expect(source.contains('h1')).to.equal(true) |
| 61 | + expect(source.contains('p')).to.equal(true) |
| 62 | + expect(source.contains('script')).to.equal(true) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should find tokens as classes', () => { |
| 66 | + expect(source.contains('container')).to.equal(true) |
| 67 | + expect(source.contains('lead')).to.equal(true) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should find tokens as multiple classes', () => { |
| 71 | + expect(source.contains('several')).to.equal(true) |
| 72 | + expect(source.contains('classes')).to.equal(true) |
| 73 | + expect(source.contains('in-a-row')).to.equal(true) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should find tokens as identifiers', () => { |
| 77 | + expect(source.contains('my-hero-element')).to.equal(true) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should not find tokens as other attribtues', () => { |
| 81 | + expect(source.contains('stylesheet')).to.equal(false) |
| 82 | + expect(source.contains('javascript')).to.equal(false) |
| 83 | + }) |
| 84 | + |
| 85 | + it('should not find tokens as text', () => { |
| 86 | + expect(source.contains('Header')).to.equal(false) |
| 87 | + expect(source.contains('examplelongtext')).to.equal(false) |
| 88 | + }) |
| 89 | + }) |
| 90 | + }) |
| 91 | +}) |
0 commit comments