UNPKG

3.56 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 it('dom-utils: createChildNode(doc, query)', () => {
60 assert.equal(
61 // cannot call this on #document, because Only one element on document allowed.
62 XMLLite.createChildNode(doc.documentElement, {
63 tagName: 'test'
64 }).tagName,
65 'test',
66 'XMLLite.createChildNode(doc, query) not working: failed.'
67 );
68 });
69
70 it('dom-utils: findOrCreateChildNode(doc, query)', () => {
71 assert.equal(
72 // cannot call this on #document, because Only one element on document allowed.
73 XMLLite.findOrCreateChildNode(doc.documentElement, {
74 tagName: 'test'
75 }).tagName,
76 'test',
77 'XMLLite.findOrCreateChildNode(doc, query) not working: failed.'
78 );
79 });
80
81 it('dom-utils: eachChildNode(doc, query, callback)', () => {
82 assert.doesNotThrow(
83 () => {
84 XMLLite.eachChildNode(doc, {}, (node) => {
85 console.log(node.nodeName);
86 });
87 },
88 Error,
89 'XMLLite.eachChildNode(doc, query, callback) not working: failed.'
90 );
91 });
92
93 it('dom-utils: eachNode(doc, query, callback)', () => {
94 assert.doesNotThrow(
95 () => {
96 XMLLite.eachNode(doc, {}, (node) => {
97 console.log(node.nodeName);
98 });
99 },
100 Error,
101 'XMLLite.eachChildNode(doc, query, callback) not working: failed.'
102 );
103 });
104
105 it('dom-utils: removeChildNode(doc, query)', () => {
106 const length = doc.childNodes.length;
107 console.log(doc.childNodes);
108
109 assert.doesNotThrow(
110 () => {
111 XMLLite.removeChildNode(doc, {
112 nodeName: 'PITarget'
113 });
114 },
115 Error,
116 'XMLLite.eachChildNode(doc, query, callback) not working: failed.'
117 );
118
119 assert.equal(
120 doc.childNodes.length,
121 length - 1,
122 'XMLLite.removeChildNode(doc, query) not working: failed.'
123 );
124
125 console.log(doc.childNodes);
126 });
127 }, 'text');
128});