test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Test.
  3. */
  4. var assert = require('assert');
  5. var before = require('../');
  6. describe("before(context)", function(){
  7. it("should return a function", function(){
  8. var speak = {
  9. greeting: 'welcome',
  10. greet: function(a, b, fn){
  11. fn(null, a + ' ' + b);
  12. }
  13. };
  14. speak.before = before(speak);
  15. speak.before.should.be.a.Function;
  16. })
  17. })
  18. describe("before(method, fn)", function(){
  19. it("should add a before hook and run it in the context", function(done){
  20. var speak = {
  21. greeting: 'welcome',
  22. greet: function(a, b, fn){
  23. fn(null, a + ' ' + b);
  24. }
  25. };
  26. speak.before = before(speak);
  27. speak.before('greet', function(args, fn){
  28. args[0] = args[0] + ', ' + this.greeting;
  29. assert(this === speak);
  30. fn();
  31. });
  32. speak.greet('Hello', 'to this world!', function(err, result){
  33. assert(null == err);
  34. result.should.equal('Hello, welcome to this world!');
  35. done();
  36. });
  37. })
  38. describe("when run multiple times", function(){
  39. it("should add multiple before hooks", function(done){
  40. var speak = {
  41. greeting: 'welcome',
  42. greet: function(a, b, fn){
  43. fn(null, a + ' ' + b);
  44. }
  45. };
  46. speak.before = before(speak);
  47. speak.before('greet', function(args, fn){
  48. args[0] = args[0] + ' to this wonderful';
  49. assert(this === speak);
  50. fn();
  51. });
  52. speak.before('greet', function(args, fn){
  53. args[0] = args[0] + ', ' + this.greeting;
  54. assert(this === speak);
  55. fn();
  56. });
  57. speak.greet('Hello', 'world!', function(err, result){
  58. assert(null == err);
  59. result.should.equal('Hello, welcome to this wonderful world!');
  60. done();
  61. });
  62. })
  63. })
  64. })