smartxml: Fixing changing node's tag
[fnpeditor.git] / src / smartxml / smartxml.test.js
1 define([
2     'libs/chai',
3     './smartxml.js'
4 ], function(chai, smartxml) {
5     
6 'use strict';
7 /*jshint expr:true */
8 /* global describe, it, beforeEach */
9
10 var expect = chai.expect;
11
12
13 var getDocumentFromXML = function(xml) {
14     return smartxml.documentFromXML(xml);
15 };
16
17 var elementNodeFromParams = function(params) {
18     return smartxml.elementNodeFromXML('<' + params.tag + '></' + params.tag + '>');
19 };
20
21 var elementNodeFromXML = function(xml) {
22     return smartxml.elementNodeFromXML(xml);
23 };
24
25
26 describe('smartxml', function() {
27
28     describe('Basic Document properties', function() {
29         it('exposes its root element', function() {
30             var doc = getDocumentFromXML('<div></div>');
31             expect(doc.root.getTagName()).to.equal('div');
32         });
33
34         it('can resets its content entirely', function() {
35             var doc = getDocumentFromXML('<div></div>');
36
37             expect(doc.root.getTagName()).to.equal('div');
38
39             doc.loadXML('<header></header>');
40             expect(doc.root.getTagName()).to.equal('header');
41         });
42     });
43
44     describe('Basic ElementNode properties', function() {
45         it('exposes node contents', function() {
46             var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
47                 contents = node.contents();
48
49             expect(contents).to.have.length(3);
50             expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
51             expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
52             expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
53         });
54
55         describe('Storing custom data', function() {
56             var node;
57
58             beforeEach(function() {
59                 node = elementNodeFromXML('<div></div>');
60             });
61
62             it('can append single value', function() {
63                 node.setData('key', 'value');
64                 expect(node.getData('key')).to.equal('value');
65             });
66
67             it('can overwrite the whole data', function() {
68                 node.setData('key1', 'value1');
69                 node.setData({key2: 'value2'});
70                 expect(node.getData('key2')).to.equal('value2');
71             });
72
73             it('can fetch the whole data at once', function() {
74                 node.setData({key1: 'value1', key2: 'value2'});
75                 expect(node.getData()).to.eql({key1: 'value1', key2: 'value2'});
76             });
77         });
78
79         describe('Changing node tag', function() {
80
81             it('can change tag name', function() {
82                 var node = elementNodeFromXML('<div></div>');
83                 node.setTag('span');
84                 expect(node.getTagName()).to.equal('span');
85             });
86
87             describe('Implementation specific expectations', function() {
88                 // DOM specifies ElementNode tag as a read-only property, so
89                 // changing it in a seamless way is a little bit tricky. For this reason
90                 // the folowing expectations are required, despite the fact that they actually are
91                 // motivated by implemetation details.
92
93                 it('keeps node in the document', function() {
94                     var doc = getDocumentFromXML('<div><header></header></div>'),
95                         header = doc.root.contents()[0];
96                     header.setTag('span');
97                     expect(header.parent().sameNode(doc.root)).to.be.true;
98                 });
99                 it('keeps custom data', function() {
100                     var node = elementNodeFromXML('<div></div>');
101
102                     node.setData('key', 'value');
103                     node.setTag('header');
104                     
105                     expect(node.getTagName()).to.equal('header');
106                     expect(node.getData()).to.eql({key: 'value'});
107                 });
108
109                 it('can change document root tag name', function() {
110                     var doc = getDocumentFromXML('<div></div>');
111                     doc.root.setTag('span');
112                     expect(doc.root.getTagName()).to.equal('span');
113                 });
114
115                 it('keeps contents', function() {
116                     var node = elementNodeFromXML('<div><div></div></div>');
117                     node.setTag('header');
118                     expect(node.contents()).to.have.length(1);
119                 });
120             });
121
122
123         });
124     });
125
126     describe('Manipulations', function() {
127
128         it('appends element node to another element node', function() {
129             var node1 = elementNodeFromParams({tag: 'div'}),
130                 node2 = elementNodeFromParams({tag: 'a'});
131             node1.append(node2);
132             expect(node1.contents()[0].sameNode(node2)).to.be.true;
133         });
134
135         it('wraps element node with another element node', function() {
136             var node = elementNodeFromXML('<div></div>'),
137                 wrapper = elementNodeFromXML('<wrapper></wrapper>');
138
139             node.wrapWith(wrapper);
140             expect(node.parent().sameNode(wrapper)).to.be.true;
141         });
142
143         it('wraps text node with element node', function() {
144             var node = elementNodeFromXML('<div>Alice</div>'),
145                 textNode = node.contents()[0],
146                 wrapper = elementNodeFromXML('<wrapper></wrapper>');
147
148             textNode.wrapWith(wrapper);
149             expect(textNode.parent().sameNode(wrapper)).to.be.true;
150             expect(node.contents()).to.have.length(1);
151         });
152
153         it('unwraps element node contents', function() {
154             var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
155                 outerDiv = node.contents()[1];
156             
157             outerDiv.unwrapContent();
158
159             expect(node.contents().length).to.equal(3);
160             expect(node.contents()[0].getText()).to.equal('Alice has ');
161             expect(node.contents()[1].getTagName()).to.equal('span');
162             expect(node.contents()[2].getText()).to.equal(' a cat!');
163         });
164
165     });
166
167     describe('Serializing document to WLXML', function() {
168         it('keeps document intact when no changes have been made', function() {
169             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
170                 doc = getDocumentFromXML(xmlIn),
171                 xmlOut = doc.toXML();
172
173             var parser = new DOMParser(),
174                 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
175                 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
176             
177             expect(input.isEqualNode(output)).to.be.true;
178         });
179     });
180
181 });
182
183 });