1 | import {getRect, getStyles, isMounted} from './dom';
|
2 |
|
3 | const nodes = [];
|
4 |
|
5 | function create(tag = 'div') {
|
6 | return document.createElement(tag);
|
7 | }
|
8 |
|
9 | function attach(node) {
|
10 | nodes.push(node);
|
11 | return document.body.insertBefore(node, document.body.firstChild);
|
12 | }
|
13 |
|
14 | afterEach(() => {
|
15 | let node;
|
16 | while ((node = nodes.pop())) {
|
17 | if (node.parentNode) {
|
18 | node.parentNode.removeChild(node);
|
19 | }
|
20 | }
|
21 | });
|
22 |
|
23 | describe('DOM', () => {
|
24 | describe('isMounted', () => {
|
25 | it('should return true for the document', () => {
|
26 | isMounted(document).should.equal(true);
|
27 | });
|
28 |
|
29 | it('should return true for an element attached to DOM', () => {
|
30 | const element = attach(create());
|
31 | isMounted(element).should.equal(true);
|
32 | });
|
33 |
|
34 | it('should return false for an element that\'s not attached to DOM', () => {
|
35 | const element = create();
|
36 | isMounted(element).should.equal(false);
|
37 | });
|
38 |
|
39 | it('should return true for textNode attached to DOM', () => {
|
40 | const textNode = attach(document.createTextNode('Lorem ipsum dolor sit amet.'));
|
41 | isMounted(textNode).should.equal(true);
|
42 | });
|
43 |
|
44 | it('should return false for textNode that\'s not attached to DOM', () => {
|
45 | const textNode = document.createTextNode('Lorem ipsum dolor sit amet.');
|
46 | isMounted(textNode).should.equal(false);
|
47 | });
|
48 | });
|
49 |
|
50 | describe('getStyles', () => {
|
51 | it('should return css-property that has been set via setAttribute(\'style\')', () => {
|
52 | const element = attach(create());
|
53 | element.setAttribute('style', 'width: 100px;');
|
54 |
|
55 | getStyles(element).width.should.equal('100px');
|
56 | });
|
57 |
|
58 | it('should return css-property that has been set via style-attribute', () => {
|
59 | const element = attach(create());
|
60 | element.style.width = '100px';
|
61 |
|
62 | getStyles(element).width.should.equal('100px');
|
63 | });
|
64 |
|
65 | it('should return css-property that has been set before mounting the node', () => {
|
66 | const element = attach(create());
|
67 | element.style.width = '100px';
|
68 |
|
69 | getStyles(element).width.should.equal('100px');
|
70 | });
|
71 | });
|
72 |
|
73 | describe('getRect', () => {
|
74 | const style = 'position: absolute; width: 100px; height: 100px; top: 10px; left: 10px; padding: 3px; margin: 4px; border: 2px solid;';
|
75 |
|
76 | it('should return DOMRect-like object for an element', () => {
|
77 | const element = attach(create());
|
78 | element.setAttribute('style', style);
|
79 |
|
80 | getRect(element).should.
|
81 | deep.equal({top: 14, right: 124, bottom: 124, left: 14, width: 110, height: 110});
|
82 | });
|
83 |
|
84 | it('should return DOMRect-like stub for unmounted element', () => {
|
85 | const element = create();
|
86 | element.setAttribute('style', style);
|
87 |
|
88 | getRect(element).should.
|
89 | deep.equal({top: 0, right: 0, bottom: 0, left: 0, width: 0, height: 0});
|
90 | });
|
91 |
|
92 | it('should return DOMRect-like object for a range', () => {
|
93 | const range = document.createRange();
|
94 | range.selectNode(document.body);
|
95 |
|
96 | getRect(range).should.have.all.keys(['top', 'right', 'bottom', 'left', 'width', 'height']);
|
97 | });
|
98 | });
|
99 | });
|