-
Notifications
You must be signed in to change notification settings - Fork 754
/
files.watching.js
73 lines (58 loc) · 2.24 KB
/
files.watching.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use strict";
var browserSync = require("../../../");
var fileWatcher = require("../../../lib/file-watcher");
var hooks = require("../../../lib/hooks");
var merge = require("../../../lib/cli/cli-options").merge;
var events = require("events");
var path = require("path");
var fs = require("graceful-fs");
var assert = require("chai").assert;
var outpath = path.join(__dirname, "../../fixtures");
var tempFileContent = "A test generated this file and it is safe to delete";
var writeTimeout = 500; // Wait for it to get to the filesystem
var writeFileWait = function (name, content, cb) {
if (!cb) {
cb = function () {};
}
setTimeout(function () {
fs.writeFile(name, content, cb);
}, writeTimeout);
};
describe("File Watcher Module", function () {
it("Passes options for chokidar", function (done) {
var imm = merge({
files: "css/*.css",
watchOptions: {
debounceDelay: 4000
}
});
imm = imm.set("files", hooks["files:watch"]([], imm.get("files"), {}));
var emitter = new events.EventEmitter();
var watchers = fileWatcher.plugin(imm, emitter);
assert.equal(watchers.core.watchers.length, 1);
assert.equal(watchers.core.watchers[0].options.debounceDelay, 4000);
done();
});
it("should emit events about changed files in core namespace", function (done) {
var tempFile = path.join(outpath, "watch-func.txt");
fs.writeFile(tempFile, tempFileContent, function () {
// assert: it works if it calls done
browserSync.reset();
browserSync.create().init({
files: tempFile,
ui: false,
online: false,
logSnippet: false
}, function (err, bs) {
bs.events.on("file:changed", function (data) {
assert.equal(data.namespace, "core");
assert.equal(data.path, path.resolve(tempFile));
bs.cleanup();
done();
});
// act: change file
writeFileWait(tempFile, tempFileContent + " changed");
});
});
});
});