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