UNPKG

4.84 kBJavaScriptView Raw
1var fs = require('fs');
2var assert = require('assert');
3var Monocle = require('../monocle');
4
5//
6// setup
7//
8var monocle = null;
9var sample_dir = __dirname + '/sample_files';
10before(function(){ monocle = Monocle(); });
11after(function() {
12 fs.unlinkSync(__dirname+"/sample_files/creation.txt");
13 fs.unlinkSync(__dirname+"/sample_files/creation2.txt");
14 fs.unlinkSync(__dirname+"/sample_files/nestedDir/creation3.txt");
15});
16//
17// file change tests
18//
19
20describe("file changes", function() {
21
22 it("should detect a change", function(complete) {
23 monocle.watchDirectory({
24 root: sample_dir,
25 listener: function(f){ cb_helper('foo.txt', f, complete); },
26 complete: function(){ complete_helper("/sample_files/foo.txt"); }
27 });
28 });
29
30 it("should detect a change in a nested dir file", function(complete) {
31 monocle.watchDirectory({
32 root: sample_dir,
33 listener: function(f) { cb_helper('servent.txt', f, complete); },
34 complete: function() { complete_helper("/sample_files/nestedDir/servent.txt"); }
35 });
36 });
37
38 it("should detect a change", function(complete) {
39 monocle.watchDirectory({
40 root: sample_dir,
41 listener: function(f) { cb_helper('longbow.js', f, complete); },
42 complete: function() { complete_helper('/sample_files/longbow.js'); }
43 });
44 });
45
46});
47
48//
49// file add tests
50//
51
52describe("file added", function() {
53 it("should detect a file added", function(complete) {
54 monocle.watchDirectory({
55 root: sample_dir,
56 listener: function(f) {
57 cb_helper("creation.txt", f, complete)
58 },
59 complete: function() {
60 complete_helper('/sample_files/creation.txt');
61 }
62 });
63 });
64
65 it("should detect another file added", function(complete) {
66 monocle.watchDirectory({
67 root: sample_dir,
68 listener: function(f) {
69 cb_helper("creation2.txt", f, complete);
70 },
71 complete: function() {
72 complete_helper('/sample_files/creation2.txt');
73 }
74 });
75 });
76
77 it("should detect another file added in a nested folder", function(complete) {
78 monocle.watchDirectory({
79 root: sample_dir,
80 listener: function(f) {
81 cb_helper("creation3.txt", f, complete);
82 },
83 complete: function() {
84 complete_helper('/sample_files/nestedDir/creation3.txt');
85 }
86 });
87 });
88});
89
90
91//
92// watch an array of files
93//
94describe("files watched", function() {
95 it("should detect a file changed of multiple", function(complete) {
96 complete_helper('/sample_files/creation.txt');
97 complete_helper('/sample_files/creation2.txt');
98 complete_helper('/sample_files/creation3.txt');
99
100 monocle.watchFiles({
101 files: [__dirname+"/sample_files/creation.txt", __dirname+"/sample_files/creation2.txt"],
102 listener: function(f) {
103 cb_helper("creation2.txt", f, complete)
104 },
105 complete: function() {
106 complete_helper('/sample_files/creation2.txt');
107 }
108 });
109 });
110
111 it("should detect a file changed (delayed)", function(complete) {
112 complete_helper('/sample_files/creation3.txt');
113 monocle.watchFiles({
114 files: [__dirname+"/sample_files/creation3.txt"],
115 listener: function(f) {
116 setTimeout(function() {
117 cb_helper('creation3.txt', f, complete);
118 }, 400);
119 },
120 complete: function() {
121 complete_helper('/sample_files/creation3.txt');
122 }
123 });
124 });
125
126
127
128 it("should detect a file changed (short delayed)", function(complete) {
129 complete_helper('/sample_files/creation4.txt');
130 monocle.watchFiles({
131 files: [__dirname+"/sample_files/creation4.txt"],
132 listener: function(f) {
133 setTimeout(function() {
134 cb_helper('creation4.txt', f, complete);
135 }, 100);
136 },
137 complete: function() {
138 complete_helper('/sample_files/creation4.txt');
139 }
140 });
141 });
142
143 it("should detect a file changed", function(complete) {
144 complete_helper('/sample_files/creation.txt');
145 monocle.watchFiles({
146 files: [__dirname+"/sample_files/creation.txt"],
147 listener: function(f) {
148 cb_helper("creation.txt", f, complete)
149 },
150 complete: function() {
151 complete_helper('/sample_files/creation.txt');
152 }
153 });
154 });
155
156 it("should not bomb when no callback is passed", function(complete) {
157 complete_helper('/sample_files/creation5.txt');
158 monocle.watchFiles({
159 files: [__dirname+"/sample_files/creation5.txt"],
160 complete: function() {
161 complete_helper('/sample_files/creation5.txt');
162 }
163 });
164 setTimeout(function() {
165 complete();
166 }, 300)
167 });
168});
169
170//
171// helpers
172//
173
174function cb_helper(name, file, done){
175 if (file.name === name) { monocle.unwatchAll(); done(); }
176}
177
178function complete_helper(path){
179 fs.writeFile(__dirname + path, (new Date).getTime() + "\n");
180}