/bin/node_modules/NodeInterval/tests/run-tests.js

https://github.com/fishmongr/Mandible · JavaScript · 97 lines · 77 code · 12 blank · 8 comment · 3 complexity · 84e757495edfa4d129935721d1e8ed7a MD5 · raw file

  1. // Test engine.
  2. var vows = require('vows'),
  3. assert = require('assert'),
  4. watch = require('nodewatch'),
  5. fs = require('fs');
  6. // Test package.
  7. var NodeInterval = require('nodeinterval');
  8. // NodeInterval init commands.
  9. var watchFolder = 'src/templates/',
  10. inputFile = 'src/index.html',
  11. replacementString = '@templates@',
  12. outputFile = 'assets/index.html';
  13. // Test variables:
  14. var randomString = 'Hi there id="bob" ' +(+ new Date),
  15. testFile1 = watchFolder + 'template01.tmpl',
  16. testFile2 = watchFolder + 'dir_level_2/dir_level_3/template09.tmpl';
  17. function fileExists(path){
  18. try{
  19. fs.lstatSync(outputFile);
  20. }catch(e){
  21. return false;
  22. }
  23. return true;
  24. }
  25. function init(){
  26. // Delete outputFile to start clean tests.
  27. if (fileExists(outputFile)){
  28. fs.unlinkSync(outputFile);
  29. }
  30. // Create a NI instance.
  31. ni = new NodeInterval.Watcher({
  32. watchFolder: watchFolder,
  33. inputFile: inputFile,
  34. outputFile: outputFile
  35. }).startWatch();
  36. vows.describe('All tests').addBatch({
  37. 'Verifying new ni object': {
  38. topic: function(){
  39. return ni;
  40. },
  41. 'Assert that we are listening to all files, in all sub dirs, and ignoring .dot files..': function(topic){
  42. assert.length(topic.watchFiles, 10);
  43. },
  44. 'Assert that passed options are stored in ni.options': function(topic){
  45. assert.isObject(topic.options);
  46. },
  47. 'Assert that default options extend passed in options': function(topic){
  48. assert.equal(topic.options.replacementString, '@templates@');
  49. }
  50. },
  51. 'Output file has content': {
  52. topic: function(){
  53. if (fileExists(outputFile)){
  54. return fs.readFileSync(outputFile, 'utf8');
  55. }
  56. return false;
  57. },
  58. 'Assert output file is updated on start and created if doesn\'t exist.': function(topic){
  59. assert.isTrue(topic.length > 0);
  60. }
  61. },
  62. 'Editing a file': {
  63. topic: function(){
  64. var that = this;
  65. console.log('making file edit...');
  66. fs.writeFileSync(watchFolder + 'template01.tmpl', randomString, 'utf8');
  67. // Wait a few secs and check that ni updated the file.
  68. setTimeout(function(){
  69. that.callback();
  70. } ,8000);
  71. },
  72. 'Assert concatinated output file has new changes.': function(topic){
  73. console.log('checking change...');
  74. var topic = fs.readFileSync(outputFile, 'utf8')
  75. assert.isTrue(!!topic.match(randomString));
  76. // Revert change
  77. console.log('Tests done. Reverting changes...');
  78. fs.writeFileSync(watchFolder + 'template01.tmpl', '<script type="text/template" id="template-01">\n\tThis is my template 01.\n</script>', 'utf8');
  79. ni.stopWatch();
  80. }
  81. }
  82. }).run();
  83. }
  84. console.log('Starting test...');
  85. init();