test-api.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* global describe, it, afterEach */
  2. /* eslint max-nested-callbacks: [1, 5] */
  3. var fs = require('fs');
  4. var watch = require('..');
  5. var join = require('path').join;
  6. var touch = require('./util/touch');
  7. var rimraf = require('rimraf');
  8. require('should');
  9. function fixtures(glob) {
  10. return join(__dirname, 'fixtures', glob);
  11. }
  12. describe('api', function () {
  13. var w;
  14. afterEach(function (done) {
  15. w.on('end', function () {
  16. rimraf.sync(fixtures('new.js'));
  17. done();
  18. });
  19. w.close();
  20. });
  21. describe('Basic functionality', function () {
  22. it('should normalize reported paths for modified files with non-normalized absolute glob', function (done) {
  23. w = watch(fixtures('../fixtures/*.js'), function (file) {
  24. file.path.should.eql(fixtures('index.js'));
  25. done();
  26. }).on('ready', touch(fixtures('index.js'), 'fixtures index'));
  27. });
  28. it('should normalize reported paths for modified files with non-normalized relative glob', function (done) {
  29. w = watch('test/fixtures/../fixtures/*.js', function (file) {
  30. file.path.should.eql(fixtures('index.js'));
  31. done();
  32. }).on('ready', touch(fixtures('index.js'), 'fixtures index'));
  33. });
  34. it('should normalize reported paths for removed files with non-normalized absolute glob', function (done) {
  35. touch(fixtures('index.js'), 'fixtures index', function () {
  36. w = watch(fixtures('../fixtures/*.js'), function (file) {
  37. file.path.should.eql(fixtures('index.js'));
  38. done();
  39. }).on('ready', function () {
  40. fs.unlinkSync(fixtures('index.js'));
  41. });
  42. })();
  43. });
  44. it('should normalize reported paths for removed files with non-normalized relative glob', function (done) {
  45. touch(fixtures('index.js'), 'fixtures index', function () {
  46. w = watch('test/fixtures/../fixtures/*.js', function (file) {
  47. file.path.should.eql(fixtures('index.js'));
  48. done();
  49. }).on('ready', function () {
  50. fs.unlinkSync(fixtures('index.js'));
  51. });
  52. })();
  53. });
  54. });
  55. describe('add', function () {
  56. it('should emit added file', function (done) {
  57. w = watch(fixtures('folder'));
  58. w.add(fixtures('*.js'));
  59. w.on('data', function (file) {
  60. file.relative.should.eql('new.js');
  61. file.event.should.eql('add');
  62. done();
  63. }).on('ready', touch(fixtures('new.js')));
  64. });
  65. it('should emit change event on file change', function (done) {
  66. w = watch(fixtures('*/*.js'));
  67. w.add(fixtures('*.js'));
  68. w.on('ready', touch(fixtures('index.js')));
  69. w.on('data', function (file) {
  70. file.relative.should.eql('index.js');
  71. done();
  72. });
  73. });
  74. });
  75. });