Last active
March 26, 2018 19:03
-
-
Save bryanjknight/6a5550993df9e7c0d2fc68fc69e064ad to your computer and use it in GitHub Desktop.
MochaJS + SinonJS + Promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fs = require('fs'); | |
const path = require('path'); | |
const NodeZip = require('node-zip'); | |
class JsonZipper { | |
constructor(path) { | |
this.path = path; | |
} | |
zipJsonFiles() { | |
const self = this; | |
return new Promise( function(resolve, reject) { | |
const files = fs.readdirSync(self.path); | |
const zippedFiles = []; | |
for (let i = 0; i < files.length; i++) { | |
const file = files[i]; | |
if (file.endsWith('.json')) { | |
const fullPath = path.join(self.path, file); | |
const zipFullPath = fullPath + '.zip'; | |
const zip = new NodeZip(); | |
zip.file(file, fs.readFileSync(fullPath)); | |
const data = zip.generate({ base64: false, compression: 'DEFLATE' }); | |
fs.writeFileSync(zipFullPath, data, 'binary'); | |
zippedFiles.push(zipFullPath); | |
} | |
} | |
resolve(zippedFiles); | |
}); | |
} | |
} | |
module.exports = JsonZipper; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const assert = require('assert'); | |
const fs = require('fs'); | |
const sinon = require('sinon'); | |
const JsonZipper = require('../JsonZipper'); | |
describe('JsonZipper', function() { | |
afterEach(function() { | |
fs.readdirSync.restore(); | |
fs.readFileSync.restore(); | |
fs.writeFileSync.restore(); | |
}); | |
it('can zip files', function() { | |
const fsReadDir = sinon.stub(fs, 'readdirSync').returns([ 'file.json' ]); | |
const fsReadFile = sinon.stub(fs, 'readFileSync').returns( '{}'); | |
const fsWriteFile = sinon.stub(fs, 'writeFileSync'); | |
const jz = new JsonZipper('./'); | |
return jz.zipJsonFiles() | |
.then(function() { | |
assert(fsReadDir.called); | |
assert(fsReadFile.called); | |
assert(fsReadFile.calledAfter(fsReadDir)); | |
assert(fsWriteFile.called); | |
assert(fsWriteFile.calledAfter(fsReadFile)); | |
}); | |
}); | |
it('can zip 2 files', function() { | |
const fsReadDir = sinon.stub(fs, 'readdirSync').returns([ 'file.json', 'file2.json' ]); | |
const fsReadFile = sinon.stub(fs, 'readFileSync').returns( '{}'); | |
const fsWriteFile = sinon.stub(fs, 'writeFileSync'); | |
const jz = new JsonZipper('./'); | |
return jz.zipJsonFiles() | |
.then(function() { | |
assert(fsReadDir.called); | |
assert(fsReadFile.called); | |
assert(fsReadFile.calledAfter(fsReadDir)); | |
assert(fsWriteFile.called); | |
assert(fsWriteFile.calledAfter(fsReadFile)); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment