smartxml: fixing changing tag of a root node
[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
116
117         });
118     });
119
120     describe('Manipulations', function() {
121
122         it('appends element node to another element node', function() {
123             var node1 = elementNodeFromParams({tag: 'div'}),
124                 node2 = elementNodeFromParams({tag: 'a'});
125             node1.append(node2);
126             expect(node1.contents()[0].sameNode(node2)).to.be.true;
127         });
128
129         it('wraps element node with another element node', function() {
130             var node = elementNodeFromXML('<div></div>'),
131                 wrapper = elementNodeFromXML('<wrapper></wrapper>');
132
133             node.wrapWith(wrapper);
134             expect(node.parent().sameNode(wrapper)).to.be.true;
135         });
136
137         it('wraps text node with element node', function() {
138             var node = elementNodeFromXML('<div>Alice</div>'),
139                 textNode = node.contents()[0],
140                 wrapper = elementNodeFromXML('<wrapper></wrapper>');
141
142             textNode.wrapWith(wrapper);
143             expect(textNode.parent().sameNode(wrapper)).to.be.true;
144             expect(node.contents()).to.have.length(1);
145         });
146
147         it('unwraps element node contents', function() {
148             var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
149                 outerDiv = node.contents()[1];
150             
151             outerDiv.unwrapContent();
152
153             expect(node.contents().length).to.equal(3);
154             expect(node.contents()[0].getText()).to.equal('Alice has ');
155             expect(node.contents()[1].getTagName()).to.equal('span');
156             expect(node.contents()[2].getText()).to.equal(' a cat!');
157         });
158
159     });
160
161     describe('Serializing document to WLXML', function() {
162         it('keeps document intact when no changes have been made', function() {
163             var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
164                 doc = getDocumentFromXML(xmlIn),
165                 xmlOut = doc.toXML();
166
167             var parser = new DOMParser(),
168                 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
169                 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
170             
171             expect(input.isEqualNode(output)).to.be.true;
172         });
173     });
174
175 });
176
177 });