diff --git a/test/run.js b/test/run.js index 8dfe5fe..31f3692 100644 --- a/test/run.js +++ b/test/run.js @@ -2,97 +2,77 @@ /* 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 expected = require('./dataset/expected'); +const Document = require('../src/Document'); 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'); +describe('Without any namespace', () => { + const fileContent = fs.readFileSync(`${__dirname}/dataset/xml/correct.xml`, 'utf-8'); + const xmlDoc = new Document(fileContent); + + describe('Tests on .find()', () => { + it('.find() fetches the correct elements.', (done) => { + const elements = xmlDoc.find('/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 xpath is not a string.', (done) => { + const fn = () => xmlDoc.find(2); + expect(fn).to.throw('xpath must be a string.'); + 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'); + describe('Tests on .count()', () => { + it('.count() gets the right amount of elements.', (done) => { + const amount = xmlDoc.count('/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); + it('count() throws an exception when xpath is not a string.', (done) => { + const fn = () => xmlDoc.count(2); + expect(fn).to.throw('path must be a string.'); 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(); - }); +describe('With a namespace', () => { + const fileContent = fs.readFileSync(`${__dirname}/dataset/xml/correct-namespace.xml`, 'utf-8'); + const xmlDoc = new Document(fileContent); - 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(); - }); + describe('Tests on .setNamespace()', () => { + it('.setNamespace() sets the namespace properly.', (done) => { + xmlDoc.setNamespace('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(xmlDoc.count('/root//grandchild')).to.be.equal(2); + 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 alias is not a string.', (done) => { + const fn = () => xmlDoc.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 valid URL.', (done) => { - const fn = () => setNamespace({}, '/root', 'not a URL'); - expect(fn).to.throw('url is not a valid URL.'); - done(); + it('.setNamespace() throws an exception when url is not a string.', (done) => { + const fn = () => xmlDoc.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 = () => xmlDoc.setNamespace('/root', 'not a URL'); + expect(fn).to.throw('url is not a valid URL.'); + done(); + }); }); });