From fb4c4fb89f6970150169fa9b4a8e3d75babd5857 Mon Sep 17 00:00:00 2001 From: Romain Date: Fri, 15 Jun 2018 22:32:45 +0200 Subject: [PATCH] test(all): add unit tests for Album and Problem --- test/log.js | 9 +++++++ test/model/album.spec.js | 18 ++++++++++---- test/problems.spec.js | 52 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 test/problems.spec.js diff --git a/test/log.js b/test/log.js index 59dfee1..ca14573 100644 --- a/test/log.js +++ b/test/log.js @@ -28,3 +28,12 @@ debug.assertContains = function (expected) { throw new Error(`Expected log to contain: ${expected}`) } } + +debug.assertNotContains = function (expected) { + const matches = debug.recorded.filter(message => { + return message.includes(expected) + }) + if (matches.length > 0) { + throw new Error(`Expected log not to contain: ${expected}`) + } +} diff --git a/test/model/album.spec.js b/test/model/album.spec.js index c4e69f9..0971b19 100644 --- a/test/model/album.spec.js +++ b/test/model/album.spec.js @@ -1,7 +1,8 @@ -var should = require('should/as-function') -var Album = require('../../src/model/album') -var fixtures = require('../fixtures') -var path = require('path') +const moment = require('moment') +const path = require('path') +const should = require('should/as-function') +const Album = require('../../src/model/album') +const fixtures = require('../fixtures') describe('Album', function () { describe('options', function () { @@ -180,6 +181,15 @@ describe('Album', function () { should(album.files).eql([c, b, a]) }) + it('can sort media by date', function () { + const album = albumWithFileDates(['2010-10-15', '2010-01-01', '2010-03-24']) + album.finalize({sortMediaBy: 'date'}) + const datesInAlbum = album.files.map(f => { + return moment(f.meta.date).format('YYYY-MM-DD') + }) + should(datesInAlbum).eql(['2010-01-01', '2010-03-24', '2010-10-15']) + }) + it('sorts nested albums too', function () { var nested = new Album({title: 'nested', files: [ diff --git a/test/problems.spec.js b/test/problems.spec.js new file mode 100644 index 0000000..30c64a0 --- /dev/null +++ b/test/problems.spec.js @@ -0,0 +1,52 @@ +const should = require('should/as-function') +const Problems = require('../src/problems.js') +const debug = require('debug') +const sinon = require('sinon') + +describe('Problems', function () { + beforeEach(() => { + console.warnOld = console.log + console.warn = sinon.spy() + debug.reset() + }) + + afterEach(() => { + console.warn = console.warnOld + }) + + it('prints a summary with the number of errors', () => { + const problems = new Problems() + problems.addFile('holidays/IMG_0001.jpg') + problems.addFile('holidays/IMG_0002.jpg') + problems.print() + should(console.warn.args.length).above(0) + const message = console.warn.args[0][0] + should(message.indexOf('an issue with 2 files')).above(-1) + console.warn = console.warnOld + }) + + it('prints the list of files with errors', () => { + const problems = new Problems() + problems.addFile('holidays/IMG_0001.jpg') + problems.addFile('holidays/IMG_0002.jpg') + problems.print() + debug.assertContains('were not processed') + debug.assertContains('holidays/IMG_0001.jpg') + debug.assertContains('holidays/IMG_0002.jpg') + console.warn = console.warnOld + }) + + it('does not print the summary if there are no errors', () => { + const problems = new Problems() + problems.print() + should(console.warn.args).eql([]) + console.warn = console.warnOld + }) + + it('does not print the detailed log if there are no errors', () => { + const problems = new Problems() + problems.print() + debug.assertNotContains('were not processed') + console.warn = console.warnOld + }) +})