Skip to content

Commit

Permalink
test(reports): add unit tests for parsers
Browse files Browse the repository at this point in the history
Adds unit tests to the parser functions in the BaseReports file.
  • Loading branch information
jniles committed Feb 3, 2020
1 parent ffbf0c2 commit 7a99978
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/src/modules/reports/baseReports.service.js
Expand Up @@ -100,6 +100,7 @@ function BaseReportService($http, Modal, util, Languages) {
}

function parseFileUrlToExtension(url) {
if (!url) { return ''; }
const parts = url.split('.');
const extension = parts[parts.length - 1];
return extension;
Expand Down
66 changes: 66 additions & 0 deletions test/client-unit/services/BaseReportService.spec.js
@@ -0,0 +1,66 @@
/* global inject, expect */
/* eslint no-unused-expressions:off */
describe('BaseReportService', () => {
// shared services
let SavedReports;

// load bhima.services
beforeEach(module(
'pascalprecht.translate',
'tmh.dynamicLocale',
'ngStorage',
'angularMoment',
'ui.bootstrap',
'bhima.services',
));


let parseExtension;
let parseIcon;
beforeEach(inject((_BaseReportService_) => {
SavedReports = _BaseReportService_;
parseExtension = SavedReports.parseFileUrlToExtension;
parseIcon = SavedReports.parseFileUrlToIcon;
}));

describe('#parseUrlToExtension()', () => {
it('correctly parses simple paths', () => {
expect(parseExtension('image.jpg')).to.equal('jpg');
expect(parseExtension('image.svg')).to.equal('svg');
expect(parseExtension('doc.docx')).to.equal('docx');

expect(parseExtension('/this/is/some/path.pdf')).to.equal('pdf');
expect(parseExtension('https://github.com/cool.gif')).to.equal('gif');

});

it('handles error cases', () => {
expect(parseExtension('')).to.equal('');
expect(parseExtension()).to.equal('');
});

it('correctly parses complex paths', () => {
expect(parseExtension('a.file.with.decimals.pdf')).to.equal('pdf');
expect(parseExtension('/more/complex/file.with/decimals.gz')).to.equal('gz');
});
});

describe('#parseUrlToIcon()', () => {
it('correctly parses paths to guess their icons', () => {
expect(parseIcon('icon.png')).to.equal('fa-file-image-o');
expect(parseIcon('icon.svg')).to.equal('fa-file-image-o');

expect(parseIcon('document.doc')).to.equal('fa-file-word-o');
expect(parseIcon('document.docx')).to.equal('fa-file-word-o');

expect(parseIcon('msexcel.xls')).to.equal('fa-file-excel-o');
expect(parseIcon('msexcel.xlsx')).to.equal('fa-file-excel-o');

expect(parseIcon('unknown.what')).to.equal('fa-file-o');


expect(parseIcon('report.pdf')).to.equal('fa-file-pdf-o');
});
});

});

0 comments on commit 7a99978

Please sign in to comment.