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() {
80 it('can change tag name', function() {
81 var doc = getDocumentFromXML('<div><header></header></div>'),
82 header = doc.root.contents()[0];
83 header.setTag('span');
84 expect(header.getTagName()).to.equal('span');
86 // DOM specifies ElementNode tag as a read-only property, so
87 // changing it in a seamless way is a little bit tricky. For this reason
88 // the folowing expectations are required, despite the fact that they actually are
89 // motivated by implemetation detail.
90 expect(header.parent().sameNode(doc.root)).to.equal(true, 'ensure we stayed in a document');
93 it('keeps custom data', function() {
94 var node = elementNodeFromXML('<div></div>');
96 node.setData('key', 'value');
97 node.setTag('header');
99 expect(node.getTagName()).to.equal('header');
100 expect(node.getData()).to.eql({key: 'value'});
105 describe('Manipulations', function() {
107 it('appends element node to another element node', function() {
108 var node1 = elementNodeFromParams({tag: 'div'}),
109 node2 = elementNodeFromParams({tag: 'a'});
111 expect(node1.contents()[0].sameNode(node2)).to.be.true;
114 it('wraps element node with another element node', function() {
115 var node = elementNodeFromXML('<div></div>'),
116 wrapper = elementNodeFromXML('<wrapper></wrapper>');
118 node.wrapWith(wrapper);
119 expect(node.parent().sameNode(wrapper)).to.be.true;
122 it('wraps text node with element node', function() {
123 var node = elementNodeFromXML('<div>Alice</div>'),
124 textNode = node.contents()[0],
125 wrapper = elementNodeFromXML('<wrapper></wrapper>');
127 textNode.wrapWith(wrapper);
128 expect(textNode.parent().sameNode(wrapper)).to.be.true;
129 expect(node.contents()).to.have.length(1);
132 it('unwraps element node contents', function() {
133 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
134 outerDiv = node.contents()[1];
136 outerDiv.unwrapContent();
138 expect(node.contents().length).to.equal(3);
139 expect(node.contents()[0].getText()).to.equal('Alice has ');
140 expect(node.contents()[1].getTagName()).to.equal('span');
141 expect(node.contents()[2].getText()).to.equal(' a cat!');
146 describe('Serializing document to WLXML', function() {
147 it('keeps document intact when no changes have been made', function() {
148 var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
149 doc = getDocumentFromXML(xmlIn),
150 xmlOut = doc.toXML();
152 var parser = new DOMParser(),
153 input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
154 output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
156 expect(input.isEqualNode(output)).to.be.true;