/* eslint-disable no-unused-expressions */ /* eslint-disable no-undef */ const chai = require('chai'); const fs = require('fs'); const path = require('path'); const expected = require('./dataset/expected'); const Document = require('../src/Document'); const { expect } = chai; describe('Without any namespace', () => { const fileContent = fs.readFileSync(path.join(__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(); }); }); 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 xpath is not a string.', (done) => { const fn = () => xmlDoc.count(2); expect(fn).to.throw('path must be a string.'); done(); }); }); }); describe('With a namespace', () => { const fileContent = fs.readFileSync(path.join(__dirname, 'dataset', 'xml', 'correct-namespace.xml'), 'utf-8'); const xmlDoc = new Document(fileContent); 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 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 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(); }); }); });