test-callback.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* global describe, it, afterEach */
  2. var watch = require('..');
  3. var path = require('path');
  4. var fs = require('fs');
  5. var rimraf = require('rimraf');
  6. var touch = require('./util/touch');
  7. require('should');
  8. function fixtures(glob) {
  9. return path.join(__dirname, 'fixtures', glob);
  10. }
  11. describe('callback', function () {
  12. var w;
  13. afterEach(function (done) {
  14. w.on('end', function () {
  15. rimraf.sync(fixtures('newDir'));
  16. done();
  17. });
  18. w.close();
  19. });
  20. it('should be called on add event', function (done) {
  21. w = watch(fixtures('*.js'), function (file) {
  22. file.relative.should.eql('index.js');
  23. file.event.should.eql('change');
  24. done();
  25. }).on('ready', touch(fixtures('index.js')));
  26. });
  27. it('should be called on non-glob pattern', function (done) {
  28. w = watch(fixtures('index.js'), function (file) {
  29. file.relative.should.eql('index.js');
  30. file.event.should.eql('change');
  31. done();
  32. }).on('ready', touch(fixtures('index.js')));
  33. });
  34. it('should be called on add event in new directory', function (done) {
  35. w = watch(fixtures('**/*.ts'), function (file) {
  36. file.relative.should.eql(path.normalize('newDir/index.ts'));
  37. done();
  38. }).on('ready', function () {
  39. fs.mkdirSync(fixtures('newDir'));
  40. touch(fixtures('newDir/index.ts'))();
  41. });
  42. });
  43. it('unlinked `file.path` should be absolute (absolute glob)', function (done) {
  44. fs.mkdirSync(fixtures('newDir'));
  45. touch(fixtures('newDir/index.ts'), function () {
  46. w = watch(fixtures('**/*.ts'), {base: 'newDir/'}, function (file) {
  47. file.path.should.eql(path.normalize(fixtures('newDir/index.ts')));
  48. done();
  49. }).on('ready', function () {
  50. fs.unlinkSync(fixtures('newDir/index.ts'));
  51. });
  52. })();
  53. });
  54. it('unlinked `file.path` should be absolute (relative glob)', function (done) {
  55. fs.mkdirSync(fixtures('newDir'));
  56. touch(fixtures('newDir/index.ts'), function () {
  57. w = watch('test/fixtures/**/*.ts', {base: 'newDir/'}, function (file) {
  58. file.path.should.eql(path.normalize(fixtures('newDir/index.ts')));
  59. done();
  60. }).on('ready', function () {
  61. fs.unlinkSync(fixtures('newDir/index.ts'));
  62. });
  63. })();
  64. });
  65. });