4 ], function(chai, smartxml) {
8 /* global describe, it, beforeEach */
10 var expect = chai.expect;
13 var getDocumentFromXML = function(xml) {
14 return smartxml.documentFromXML(xml);
17 var elementNodeFromParams = function(params) {
18 return smartxml.elementNodeFromXML('<' + params.tag + '></' + params.tag + '>');
21 var elementNodeFromXML = function(xml) {
22 return smartxml.elementNodeFromXML(xml);
26 describe('smartxml', function() {
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');
34 it('can resets its content entirely', function() {
35 var doc = getDocumentFromXML('<div></div>');
37 expect(doc.root.getTagName()).to.equal('div');
39 doc.loadXML('<header></header>');
40 expect(doc.root.getTagName()).to.equal('header');
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();
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');
55 describe('Storing custom data', function() {
58 beforeEach(function() {
59 node = elementNodeFromXML('<div></div>');
62 it('can append single value', function() {
63 node.setData('key', 'value');
64 expect(node.getData('key')).to.equal('value');
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');
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'});
79 describe('Changing node tag', function() {
81 it('can change tag name', function() {
82 var node = elementNodeFromXML('<div></div>');
84 expect(node.getTagName()).to.equal('span');
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.
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;
99 it('keeps custom data', function() {
100 var node = elementNodeFromXML('<div></div>');
102 node.setData('key', 'value');
103 node.setTag('header');
105 expect(node.getTagName()).to.equal('header');
106 expect(node.getData()).to.eql({key: 'value'});
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');
120 describe('Manipulations', function() {
122 it('appends element node to another element node', function() {
123 var node1 = elementNodeFromParams({tag: 'div'}),
124 node2 = elementNodeFromParams({tag: 'a'});
126 expect(node1.contents()[0].sameNode(node2)).to.be.true;
129 it('wraps element node with another element node', function() {
130 var node = elementNodeFromXML('<div></div>'),
131 wrapper = elementNodeFromXML('<wrapper></wrapper>');
133 node.wrapWith(wrapper);
134 expect(node.parent().sameNode(wrapper)).to.be.true;
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>');
142 textNode.wrapWith(wrapper);
143 expect(textNode.parent().sameNode(wrapper)).to.be.true;
144 expect(node.contents()).to.have.length(1);
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];
151 outerDiv.unwrapContent();
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!');
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();
167 var parser = new DOMParser(),
168 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
169 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
171 expect(input.isEqualNode(output)).to.be.true;