From ce030b91fa14a4797965948d7666f78882c91f71 Mon Sep 17 00:00:00 2001 From: Alexander Ponomaroff <33966871+alexander-ponomaroff@users.noreply.github.com> Date: Sat, 20 Oct 2018 16:59:26 -0400 Subject: [PATCH] Fix issue #398 - Add testing for promises to all test cases in fs.writeFile-readFile.spec.js file (#402) * Fix issue #398 - Add testing for promises to all test cases in fs.writeFile-readFile.spec.js file * Fix issue #398 - Add testing for promises to all test cases in fs.writeFile-readFile.spec.js file * Fixed requested issues * Further Improvement --- tests/spec/fs.writeFile-readFile.spec.js | 88 ++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/spec/fs.writeFile-readFile.spec.js b/tests/spec/fs.writeFile-readFile.spec.js index 07c7f56..4f03fe9 100644 --- a/tests/spec/fs.writeFile-readFile.spec.js +++ b/tests/spec/fs.writeFile-readFile.spec.js @@ -112,3 +112,91 @@ describe('fs.writeFile, fs.readFile', function() { }); }); }); + +/** + * fsPromises tests + */ + +describe('fsPromises.writeFile, fsPromises.readFile', function() { + beforeEach(util.setup); + afterEach(util.cleanup); + + it('should be a function', function() { + var fsPromises = util.fs().promises; + expect(fsPromises.writeFile).to.be.a('function'); + expect(fsPromises.readFile).to.be.a('function'); + }); + + it('should return a promise', function() { + var fsPromises = util.fs().promises; + var contents = 'This is a file.'; + + var p = fsPromises.writeFile('/myfile', contents); + expect(p).to.be.a('Promise'); + + p.then(() => { + expect(fsPromises.readFile('/myfile', 'utf8')).to.be.a('Promise'); + }); + return p; + }); + + it('should error when path is wrong to readFile', function() { + var fsPromises = util.fs().promises; + + return fsPromises.readFile('/no-such-file', 'utf8') + .catch(error => { + expect(error).to.exist; + expect(error.code).to.equal('ENOENT'); + }); + }); + + it('should write, read a utf8 file without specifying utf8 in writeFile', function() { + var fsPromises = util.fs().promises; + var contents = 'This is a file.'; + + return fsPromises.writeFile('/myfile', contents) + .then( () => fsPromises.readFile('/myfile', 'utf8')) + .then(data => { expect(data).to.equal(contents); }); + }); + + it('should write, read a utf8 file with "utf8" option to writeFile', function() { + var fsPromises = util.fs().promises; + var contents = 'This is a file.'; + + return fsPromises.writeFile('/myfile', contents, 'utf8') + .then( () => fsPromises.readFile('/myfile', 'utf8')) + .then(data => { expect(data).to.equal(contents); }); + }); + + it('should write, read a utf8 file with {encoding: "utf8"} option to writeFile', function() { + var fsPromises = util.fs().promises; + var contents = 'This is a file.'; + + return fsPromises.writeFile('/myfile', contents, { encoding: 'utf8' }) + .then( () => fsPromises.readFile('/myfile', 'utf8')) + .then(data => { expect(data).to.equal(contents); }); + }); + + it('should write, read a binary file', function() { + var fsPromises = util.fs().promises; + // String and utf8 binary encoded versions of the same thing: 'This is a file.' + var binary = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]); + + return fsPromises.writeFile('/myfile', binary) + .then( () => fsPromises.readFile('/myfile')) + .then(data => { expect(data).to.deep.equal(binary); }); + }); + + it('should follow symbolic links', function() { + var fsPromises = util.fs().promises; + var contents = 'This is a file.'; + + return fsPromises.writeFile('/myfile', '', { encoding: 'utf8' }) + .then( () => fsPromises.symlink('/myfile', '/myFileLink')) + .then( () => fsPromises.writeFile('/myFileLink', contents, 'utf8')) + .then( () => fsPromises.readFile('/myFileLink', 'utf8')) + .then(data => { expect(data).to.equal(contents); }); + }); +}); + +