2019-03-20 01:49:10 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var util = require('../lib/test-utils.js');
|
|
|
|
var expect = require('chai').expect;
|
|
|
|
|
|
|
|
describe('fs.watchFile', function() {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
|
|
|
|
|
|
|
it('should be a function', function() {
|
2019-03-29 04:53:45 +00:00
|
|
|
const fs = util.fs();
|
2019-03-20 01:49:10 +00:00
|
|
|
expect(typeof fs.watchFile).to.equal('function');
|
|
|
|
});
|
|
|
|
|
2019-03-29 04:53:45 +00:00
|
|
|
it('should throw an error if a file path is not defined', function() {
|
2019-03-20 01:49:10 +00:00
|
|
|
const fs = util.fs();
|
2019-03-29 04:53:45 +00:00
|
|
|
|
|
|
|
const fn = () => fs.watchFile(undefined);
|
|
|
|
expect(fn).to.throw();
|
|
|
|
});
|
2019-03-20 01:49:10 +00:00
|
|
|
|
2019-03-29 20:30:55 +00:00
|
|
|
it('should throw an error if a file is deleted', function() {
|
|
|
|
const fs = util.fs();
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', 'data', function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.watchFile('/myfile', function(prev, curr) {
|
|
|
|
expect(prev).to.exist;
|
|
|
|
expect(curr).to.exist;
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.unlink('/myfile');
|
|
|
|
|
|
|
|
const fn = () => fs.watchFile('/myfile');
|
|
|
|
expect(fn).to.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('prev and curr should be equal if nothing has been changed in the file', function() {
|
2019-03-29 04:53:45 +00:00
|
|
|
const fs = util.fs();
|
2019-03-20 01:49:10 +00:00
|
|
|
|
2019-03-29 04:53:45 +00:00
|
|
|
fs.writeFile('/myfile', 'data', function(error) {
|
2019-03-20 01:49:10 +00:00
|
|
|
if(error) throw error;
|
2019-03-29 04:53:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
fs.watchFile('/myfile', function(prev, curr) {
|
2019-03-29 20:30:55 +00:00
|
|
|
expect(prev).to.equal(curr);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('prev and curr should not be equal if something has been changed in the file', function() {
|
|
|
|
const fs = util.fs();
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', 'data', function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.watchFile('/myfile', function(prev, curr) {
|
|
|
|
expect(prev).to.equal(curr);
|
|
|
|
|
|
|
|
fs.writeFile('/myfile', 'data2', function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(prev).to.not.equal(curr);
|
2019-03-20 01:49:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|