/* eslint-disable no-unused-expressions */ /* eslint-disable no-undef */ const chai = require('chai'); const libxmljs = require('libxmljs'); const fs = require('fs'); const expected = require('./dataset/expected.js'); const find = require('../src/find.js'); const count = require('../src/count.js'); const setNamespace = require('../src/namespace.js'); const { expect } = chai; describe('find.js', () => { it('find() fetches the correct elements.', (done) => { fs.readFile(`${__dirname}/dataset/xml/correct.xml`, 'utf8', (err, fileContent) => { const xmlDoc = libxmljs.parseXmlString(fileContent); const elements = find(xmlDoc, '/root//grandchild'); expect(elements).to.not.be.undefined; const content = []; elements.forEach((e) => content.push(e.text())); expect(content.join(' ')).to.be.equal(expected.correct.grandchildrenText); done(); }); }); it('find() throws an exception when no Document is provided.', (done) => { const fn = () => find(null, ''); expect(fn).to.throw('A Document must be provided.'); done(); }); it('find() throws an exception when path is not a string.', (done) => { const fn = () => find({}, 2); expect(fn).to.throw('path must be a string.'); done(); }); }); describe('count.js', () => { it('count() gets the right amount of elements.', (done) => { fs.readFile(`${__dirname}/dataset/xml/correct.xml`, 'utf8', (err, fileContent) => { const xmlDoc = libxmljs.parseXmlString(fileContent); const amount = count(xmlDoc, '/root//grandchild'); expect(amount).to.be.equal(2); done(); }); }); it('count() throws an exception when no Document is provided.', (done) => { const fn = () => count(null, ''); expect(fn).to.throw('A Document must be provided.'); done(); }); it('count() throws an exception when path is not a string.', (done) => { const fn = () => count({}, 2); expect(fn).to.throw('path must be a string.'); done(); }); }); describe('namespace.js', () => { it('setNamespace() sets the namespace properly.', (done) => { fs.readFile(`${__dirname}/dataset/xml/correct-namespace.xml`, 'utf8', (err, fileContent) => { const xmlDoc = libxmljs.parseXmlString(fileContent); setNamespace(xmlDoc, 'namespace', 'http://www.my-namespace.org/'); expect(xmlDoc.namespace.alias).to.be.equal('namespace'); expect(xmlDoc.namespace.url).to.be.equal('http://www.my-namespace.org/'); expect(count(xmlDoc, '/root//grandchild')).to.be.equal(2); done(); }); }); it('setNamespace() throws an exception when no Document is provided.', (done) => { const fn = () => setNamespace(null, '/root', 'http://www.my-namespace.org/'); expect(fn).to.throw('A Document must be provided.'); done(); }); it('setNamespace() throws an exception when alias is not a string.', (done) => { const fn = () => setNamespace({}, 2, 'http://www.my-namespace.org/'); expect(fn).to.throw('alias must be a string.'); done(); }); it('setNamespace() throws an exception when url is not a string.', (done) => { const fn = () => setNamespace({}, '/root', 2); expect(fn).to.throw('url must be a string.'); done(); }); it('setNamespace() throws an exception when url is not a valid URL.', (done) => { const fn = () => setNamespace({}, '/root', 'not a URL'); expect(fn).to.throw('url is not a valid URL.'); done(); }); });