Initial commit
[yaffs-website] / node_modules / glob-watcher / test / main.js
1 var watch = require('../');
2 var should = require('should');
3 var path = require('path');
4 var fs = require('fs');
5 var rimraf = require('rimraf');
6 var mkdirp = require('mkdirp');
7
8 require('mocha');
9
10 describe('glob-watcher', function() {
11   it('should return a valid file struct via EE', function(done) {
12     var expectedName = path.join(__dirname, "./fixtures/stuff/temp.coffee");
13     var fname = path.join(__dirname, "./fixtures/**/temp.coffee");
14     mkdirp.sync(path.dirname(expectedName));
15     fs.writeFileSync(expectedName, "testing");
16
17     var watcher = watch(fname);
18     watcher.on('change', function(evt) {
19       should.exist(evt);
20       should.exist(evt.path);
21       should.exist(evt.type);
22       evt.type.should.equal('changed');
23       evt.path.should.equal(expectedName);
24       watcher.end();
25     });
26     watcher.on('end', function(){
27       rimraf.sync(expectedName);
28       done();
29     });
30     setTimeout(function(){
31       fs.writeFileSync(expectedName, "test test");
32     }, 125);
33   });
34
35   it('should emit nomatch via EE', function(done) {
36     var fname = path.join(__dirname, "./doesnt_exist_lol/temp.coffee");
37
38     var watcher = watch(fname);
39     watcher.on('nomatch', function() {
40       done();
41     });
42   });
43
44   it('should return a valid file struct via callback', function(done) {
45     var expectedName = path.join(__dirname, "./fixtures/stuff/test.coffee");
46     var fname = path.join(__dirname, "./fixtures/**/test.coffee");
47     mkdirp.sync(path.dirname(expectedName));
48     fs.writeFileSync(expectedName, "testing");
49
50     var watcher = watch(fname, function(evt) {
51       should.exist(evt);
52       should.exist(evt.path);
53       should.exist(evt.type);
54       evt.type.should.equal('changed');
55       evt.path.should.equal(expectedName);
56       watcher.end();
57     });
58
59     watcher.on('end', function(){
60       rimraf.sync(expectedName);
61       done();
62     });
63     setTimeout(function(){
64       fs.writeFileSync(expectedName, "test test");
65     }, 200);
66   });
67
68   it('should not return a non-matching file struct via callback', function(done) {
69     var expectedName = path.join(__dirname, "./fixtures/test123.coffee");
70     var fname = path.join(__dirname, "./fixtures/**/test.coffee");
71     mkdirp.sync(path.dirname(expectedName));
72     fs.writeFileSync(expectedName, "testing");
73
74     var watcher = watch(fname, function(evt) {
75       throw new Error("Should not have been called! "+evt.path);
76     });
77
78     setTimeout(function(){
79       fs.writeFileSync(expectedName, "test test");
80     }, 200);
81
82     setTimeout(function(){
83       rimraf.sync(expectedName);
84       done();
85     }, 1500);
86   });
87 });