main.js 2.5 KB

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