index.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. var assert = require('assert');
  2. var delegate = require('..');
  3. describe('.method(name)', function(){
  4. it('should delegate methods', function(){
  5. var obj = {};
  6. obj.request = {
  7. foo: function(bar){
  8. assert(this == obj.request);
  9. return bar;
  10. }
  11. };
  12. delegate(obj, 'request').method('foo');
  13. obj.foo('something').should.equal('something');
  14. })
  15. })
  16. describe('.getter(name)', function(){
  17. it('should delegate getters', function(){
  18. var obj = {};
  19. obj.request = {
  20. get type() {
  21. return 'text/html';
  22. }
  23. }
  24. delegate(obj, 'request').getter('type');
  25. obj.type.should.equal('text/html');
  26. })
  27. })
  28. describe('.setter(name)', function(){
  29. it('should delegate setters', function(){
  30. var obj = {};
  31. obj.request = {
  32. get type() {
  33. return this._type.toUpperCase();
  34. },
  35. set type(val) {
  36. this._type = val;
  37. }
  38. }
  39. delegate(obj, 'request').setter('type');
  40. obj.type = 'hey';
  41. obj.request.type.should.equal('HEY');
  42. })
  43. })
  44. describe('.access(name)', function(){
  45. it('should delegate getters and setters', function(){
  46. var obj = {};
  47. obj.request = {
  48. get type() {
  49. return this._type.toUpperCase();
  50. },
  51. set type(val) {
  52. this._type = val;
  53. }
  54. }
  55. delegate(obj, 'request').access('type');
  56. obj.type = 'hey';
  57. obj.type.should.equal('HEY');
  58. })
  59. })
  60. describe('.fluent(name)', function () {
  61. it('should delegate in a fluent fashion', function () {
  62. var obj = {
  63. settings: {
  64. env: 'development'
  65. }
  66. };
  67. delegate(obj, 'settings').fluent('env');
  68. obj.env().should.equal('development');
  69. obj.env('production').should.equal(obj);
  70. obj.settings.env.should.equal('production');
  71. })
  72. })