4 ], function(chai, smartxml) {
9 var expect = chai.expect;
12 var getDocumentFromXML = function(xml) {
13 return smartxml.documentFromXML(xml);
16 var elementNodeFromParams = function(params) {
17 return smartxml.elementNodeFromXML('<' + params.tag + '></' + params.tag + '>');
20 var elementNodeFromXML = function(xml) {
21 return smartxml.elementNodeFromXML(xml);
25 describe('smartxml', function() {
27 describe('Basic Document properties', function() {
28 it('exposes its root element', function() {
29 var doc = getDocumentFromXML('<div></div>');
30 expect(doc.root.getTagName()).to.equal('div');
34 describe('Basic ElementNode properties', function() {
35 it('exposes node contents', function() {
36 var node = elementNodeFromXML('<node>Some<node>text</node>is here</node>'),
37 contents = node.contents();
39 expect(contents).to.have.length(3);
40 expect(contents[0].nodeType).to.equal(Node.TEXT_NODE, 'text node 1');
41 expect(contents[1].nodeType).to.equal(Node.ELEMENT_NODE, 'element node 1');
42 expect(contents[2].nodeType).to.equal(Node.TEXT_NODE, 'text node 2');
46 describe('Manipulations', function() {
48 it('appends element node to another element node', function() {
49 var node1 = elementNodeFromParams({tag: 'div'}),
50 node2 = elementNodeFromParams({tag: 'a'});
52 expect(node1.contents()[0].sameNode(node2)).to.be.true;
55 it('wraps element node with another element node', function() {
56 var node = elementNodeFromXML('<div></div>'),
57 wrapper = elementNodeFromXML('<wrapper></wrapper>');
59 node.wrapWith(wrapper);
60 expect(node.parent().sameNode(wrapper)).to.be.true;
63 it('wraps text node with element node', function() {
64 var node = elementNodeFromXML('<div>Alice</div>'),
65 textNode = node.contents()[0],
66 wrapper = elementNodeFromXML('<wrapper></wrapper>');
68 textNode.wrapWith(wrapper);
69 expect(textNode.parent().sameNode(wrapper)).to.be.true;
70 expect(node.contents()).to.have.length(1);
73 it('unwraps element node contents', function() {
74 var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
75 outerDiv = node.contents()[1];
77 outerDiv.unwrapContent();
79 expect(node.contents().length).to.equal(3);
80 expect(node.contents()[0].getText()).to.equal('Alice has ');
81 expect(node.contents()[1].getTagName()).to.equal('span');
82 expect(node.contents()[2].getText()).to.equal(' a cat!');