All files / src/dom domnode.spec.ts

100% Statements 152/152
100% Branches 0/0
100% Functions 41/41
100% Lines 145/145
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 2451x 1x 1x   1x   1x     1x 23x 23x                   1x 1x 1x 1x     1x   1x 1x 1x 1x     1x 1x 1x         1x   1x 1x 1x 1x 1x     1x 6x 1x 1x         1x   1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x         1x   1x 1x 1x 1x 1x 1x 1x         1x   1x 1x   1x 1x 1x   1x 3x 1x         1x   1x 1x   1x 1x 1x   3x 1x     1x 1x   1x 1x 1x   3x 1x     1x 1x   1x 1x 1x   1x 1x 1x 1x   1x         1x   1x 1x   1x 1x 1x   3x 1x     1x 1x   1x 1x 1x   2x 1x         1x   1x 1x   1x 1x 1x   3x 1x            
import { DOMNode, DOMTree }  from 'dom';
import { Parser } from '../parser';
import { Config } from '../config';
 
describe('DOMNode', function(){
 
	const expect = require('chai').expect;
	let root: DOMTree;
 
	beforeEach(() => {
		const parser = new Parser(Config.empty());
		root = parser.parseHtml(`<div id="parent">
			<ul>
				<li class="foo">foo</li>
				<li class="bar baz" id="spam" title="ham">bar</li>
			</ul>
			<p class="bar">spam</p>
			<span class="baz">flux</span>
		</div>`);
	});
 
	it('id property should return element id', function(){
		const el = new DOMNode('foo');
		el.setAttribute('id', 'bar');
		expect(el.id).to.equal('bar');
	});
 
	describe('is()', function(){
 
		it('should match tagname', function(){
			const el = new DOMNode('foo');
			expect(el.is('foo')).to.be.true;
			expect(el.is('bar')).to.be.false;
		});
 
		it('should match any tag when using asterisk', function(){
			const el = new DOMNode('foo');
			expect(el.is('*')).to.be.true;
		});
 
	});
 
	describe('getElementsByTagName()', function(){
 
		it('should find elements', function(){
			const nodes = root.getElementsByTagName('li');
			expect(nodes).to.have.lengthOf(2);
			expect(nodes[0].getAttribute('class')).to.equal('foo');
			expect(nodes[1].getAttribute('class')).to.equal('bar baz');
		});
 
		it('should support universal selector', function(){
			const tagNames = root.getElementsByTagName('*').map((cur: DOMNode) => cur.tagName);
			expect(tagNames).to.have.lengthOf(6);
			expect(tagNames).to.deep.equal(['div', 'ul', 'li', 'li', 'p', 'span']);
		});
 
	});
 
	describe('querySelector()', () => {
 
		it('should find element by tagname', () => {
			const el = root.querySelector('ul');
			expect(el).to.be.an.instanceof(DOMNode);
			expect(el.tagName).to.equal('ul');
		});
 
		it('should find element by #id', () => {
			const el = root.querySelector('#parent');
			expect(el).to.be.an.instanceof(DOMNode);
			expect(el.tagName).to.equal('div');
			expect(el.getAttribute('id')).to.equal('parent');
		});
 
		it('should find element by .class', () => {
			const el = root.querySelector('.foo');
			expect(el).to.be.an.instanceof(DOMNode);
			expect(el.tagName).to.equal('li');
			expect(el.getAttribute('class')).to.equal('foo');
		});
 
		it('should find element by [attr]', () => {
			const el = root.querySelector('[title]');
			expect(el).to.be.an.instanceof(DOMNode);
			expect(el.tagName).to.equal('li');
			expect(el.getAttribute('class')).to.equal('bar baz');
		});
 
		it('should find element by [attr=".."]', () => {
			const el = root.querySelector('[class="foo"]');
			expect(el).to.be.an.instanceof(DOMNode);
			expect(el.tagName).to.equal('li');
			expect(el.getAttribute('class')).to.equal('foo');
		});
 
		it('should find element by multiple selectors', () => {
			const el = root.querySelector('.bar.baz#spam');
			expect(el).to.be.an.instanceof(DOMNode);
			expect(el.tagName).to.equal('li');
			expect(el.getAttribute('class')).to.equal('bar baz');
		});
 
		it('should find element with descendant combinator', () => {
			const el = root.querySelector('ul .bar');
			expect(el).to.be.an.instanceof(DOMNode);
			expect(el.tagName).to.equal('li');
			expect(el.getAttribute('class')).to.equal('bar baz');
		});
 
		it('should find element with child combinator', () => {
			const el = root.querySelector('div > .bar');
			expect(el).to.be.an.instanceof(DOMNode);
			expect(el.tagName).to.equal('p');
			expect(el.getAttribute('class')).to.equal('bar');
		});
 
		it('should find element with adjacent sibling combinator', () => {
			const el = root.querySelector('li + li');
			expect(el).to.be.an.instanceof(DOMNode);
			expect(el.tagName).to.equal('li');
			expect(el.getAttribute('class')).to.equal('bar baz');
		});
 
		it('should find element with general sibling combinator', () => {
			const el = root.querySelector('ul ~ .baz');
			expect(el).to.be.an.instanceof(DOMNode);
			expect(el.tagName).to.equal('span');
			expect(el.getAttribute('class')).to.equal('baz');
		});
 
	});
 
	describe('querySelectorAll()', () => {
 
		it('should find multiple elements', () => {
			const el = root.querySelectorAll('.bar');
			expect(el).to.have.lengthOf(2);
			expect(el[0]).to.be.an.instanceof(DOMNode);
			expect(el[1]).to.be.an.instanceof(DOMNode);
			expect(el[0].tagName).to.equal('li');
			expect(el[1].tagName).to.equal('p');
		});
 
	});
 
	describe('visitDepthFirst()', function(){
 
		it('should visit all nodes in correct order', function(){
			const root = DOMNode.rootNode();
			/* eslint-disable no-unused-vars */
			const a = new DOMNode('a', root);
			const b = new DOMNode('b', root);
			const c = new DOMNode('c', b);
			/* eslint-enable no-unused-vars */
			const order: string[] = [];
			root.visitDepthFirst((node: DOMNode) => order.push(node.tagName));
			expect(order).to.deep.equal(['a', 'c', 'b']);
		});
 
	});
 
	describe('someChildren()', function(){
 
		it('should return true if any child node evaluates to true', function(){
			const root = new DOMNode('root');
			/* eslint-disable no-unused-vars */
			const a = new DOMNode('a', root);
			const b = new DOMNode('b', root);
			const c = new DOMNode('c', b);
			/* eslint-enable no-unused-vars */
			const result = root.someChildren((node: DOMNode) => node.tagName === 'c');
			expect(result).to.be.true;
		});
 
		it('should return false if no child node evaluates to true', function(){
			const root = new DOMNode('root');
			/* eslint-disable no-unused-vars */
			const a = new DOMNode('a', root);
			const b = new DOMNode('b', root);
			const c = new DOMNode('c', b);
			/* eslint-enable no-unused-vars */
			const result = root.someChildren(() => false);
			expect(result).to.be.false;
		});
 
		it('should short-circuit when first node evalutes to true', function(){
			const root = new DOMNode('root');
			/* eslint-disable no-unused-vars */
			const a = new DOMNode('a', root);
			const b = new DOMNode('b', root);
			const c = new DOMNode('c', b);
			/* eslint-enable no-unused-vars */
			const order: string[] = [];
			root.someChildren((node: DOMNode) => {
				order.push(node.tagName);
				return node.tagName === 'a';
			});
			expect(order).to.deep.equal(['a']);
		});
 
	});
 
	describe('everyChildren()', function(){
 
		it('should return true if all nodes evaluates to true', function(){
			const root = new DOMNode('root');
			/* eslint-disable no-unused-vars */
			const a = new DOMNode('a', root);
			const b = new DOMNode('b', root);
			const c = new DOMNode('c', b);
			/* eslint-enable no-unused-vars */
			const result = root.everyChildren(() => true);
			expect(result).to.be.true;
		});
 
		it('should return false if any nodes evaluates to false', function(){
			const root = new DOMNode('root');
			/* eslint-disable no-unused-vars */
			const a = new DOMNode('a', root);
			const b = new DOMNode('b', root);
			const c = new DOMNode('c', b);
			/* eslint-enable no-unused-vars */
			const result = root.everyChildren((node: DOMNode) => node.tagName !== 'b');
			expect(result).to.be.false;
		});
 
	});
 
	describe('find()', function(){
 
		it('should visit all nodes until callback evaluates to true', function(){
			const root = new DOMNode('root');
			/* eslint-disable no-unused-vars */
			const a = new DOMNode('a', root);
			const b = new DOMNode('b', root);
			const c = new DOMNode('c', b);
			/* eslint-enable no-unused-vars */
			const result = root.find((node: DOMNode) => node.tagName === 'b');
			expect(result.tagName).to.equal('b');
		});
 
	});
 
});