UNPKG

3.9 kBJavaScriptView Raw
1import XMLLite from 'xml-lite';
2import $ from 'jquery';
3
4const assert = chai.assert;
5
6describe('dom-utils', () => {
7 $.get('./fixtures/bookstore.xml', (xmlContent) => {
8 const doc = XMLLite.parse(xmlContent);
9
10 it('dom-utils: attributesAsObject(doc)', () => {
11 assert.deepEqual(
12 XMLLite.attributesAsObject(XMLLite.findNodes(doc, {
13 tagName: 'book',
14 attributes: {
15 category: 'COOKING',
16 },
17 })[0]),
18 {
19 category: 'COOKING',
20 },
21 'XMLLite.attributesAsObject(doc) not working: something wrong.'
22 );
23 });
24
25 it('dom-utils: findChildNode(doc, query)', () => {
26 assert.equal(
27 XMLLite.findChildNode(doc, {
28 nodeName: 'PITarget'
29 }).nodeName,
30 'PITarget',
31 'XMLLite.findChildNode(doc, query) not working: not exists.'
32 );
33 });
34
35 it('dom-utils: findChildNodes(doc, query)', () => {
36 assert.equal(
37 XMLLite.findChildNodes(XMLLite.findChildNode(doc, {
38 tagName: 'bookstore'
39 }), {
40 tagName: 'book'
41 }).length,
42 3,
43 'XMLLite.findChildNodes(doc, query) not working: result nodes count not matching.'
44 );
45 });
46
47 it('dom-utils: findAllNodes(doc, query)', () => {
48 assert.equal(
49 XMLLite.findAllNodes(XMLLite.findChildNode(doc, {
50 tagName: 'bookstore'
51 }), {
52 tagName: 'price'
53 }).length,
54 3,
55 'XMLLite.findAllNodes(doc, query) not working: result nodes count not matching.'
56 );
57 });
58
59 // #16
60 it('dom-utils: findChildNodes(doc, query) on an element that has no childNodes', () => {
61 assert.equal(
62 XMLLite.findChildNodes(doc.childNodes[0], {
63 tagName: 'book'
64 }).length,
65 0,
66 'XMLLite.findChildNodes(doc, query) not working: result nodes count not matching.'
67 );
68 });
69
70 it('dom-utils: createChildNode(doc, query)', () => {
71 assert.equal(
72 // cannot call this on #document, because Only one element on document allowed.
73 XMLLite.createChildNode(doc.documentElement, {
74 tagName: 'test'
75 }).tagName,
76 'test',
77 'XMLLite.createChildNode(doc, query) not working: failed.'
78 );
79 });
80
81 it('dom-utils: findOrCreateChildNode(doc, query)', () => {
82 assert.equal(
83 // cannot call this on #document, because Only one element on document allowed.
84 XMLLite.findOrCreateChildNode(doc.documentElement, {
85 tagName: 'test'
86 }).tagName,
87 'test',
88 'XMLLite.findOrCreateChildNode(doc, query) not working: failed.'
89 );
90 });
91
92 it('dom-utils: eachChildNode(doc, query, callback)', () => {
93 assert.doesNotThrow(
94 () => {
95 XMLLite.eachChildNode(doc, {}, (node) => {
96 console.log(node.nodeName);
97 });
98 },
99 Error,
100 'XMLLite.eachChildNode(doc, query, callback) not working: failed.'
101 );
102 });
103
104 it('dom-utils: eachNode(doc, query, callback)', () => {
105 assert.doesNotThrow(
106 () => {
107 XMLLite.eachNode(doc, {}, (node) => {
108 console.log(node.nodeName);
109 });
110 },
111 Error,
112 'XMLLite.eachChildNode(doc, query, callback) not working: failed.'
113 );
114 });
115
116 it('dom-utils: removeChildNode(doc, query)', () => {
117 const length = doc.childNodes.length;
118 console.log(doc.childNodes);
119
120 assert.doesNotThrow(
121 () => {
122 XMLLite.removeChildNode(doc, {
123 nodeName: 'PITarget'
124 });
125 },
126 Error,
127 'XMLLite.eachChildNode(doc, query, callback) not working: failed.'
128 );
129
130 assert.equal(
131 doc.childNodes.length,
132 length - 1,
133 'XMLLite.removeChildNode(doc, query) not working: failed.'
134 );
135
136 console.log(doc.childNodes);
137 });
138 }, 'text');
139});