tools2.test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. var tools = require("../index");
  2. var expect = require('expect.js');
  3. describe('Tools API', function() {
  4. // before(setup);
  5. // after(cleanup);
  6. describe('add_action do_action', function() {
  7. it('Can add an `action` function to be called with do_action', function(done) {
  8. var tool = tools();
  9. console.log(tool);
  10. tool.add_action("test", function() {
  11. expect(true)
  12. done();
  13. }, 10);
  14. tool.do_action("test");
  15. });
  16. it('Can add an `action` function to be called with do_action with extra arguments', function(
  17. done) {
  18. var tool = tools();
  19. tool.add_action("test", function(myarg) {
  20. expect(myarg).to.equal("hello");
  21. done();
  22. }, 10);
  23. tool.do_action("test", "hello");
  24. });
  25. it('Can add an `action` function to be called with do_action sorted', function(done) {
  26. var tool = tools();
  27. var iii = 0;
  28. tool.add_action("test", function() {
  29. expect(iii).to.equal(2)
  30. iii++
  31. }, 30);
  32. tool.add_action("test", function() {
  33. expect(iii).to.equal(1)
  34. iii++
  35. done();
  36. }, 20);
  37. tool.add_action("test", function() {
  38. expect(iii).to.equal(3)
  39. iii++
  40. }, 50);
  41. tool.add_action("test", function() {
  42. expect(iii).to.equal(0)
  43. iii++
  44. }, 10);
  45. tool.do_action("test");
  46. });
  47. it('Can add an `action` function to be called with do_action with extra arguments sorted',
  48. function(done) {
  49. var tool = tools();
  50. var iii = 0;
  51. tool.add_action("test", function(myarg) {
  52. expect(iii).to.equal(2)
  53. expect(myarg).to.equal("hello");
  54. iii++
  55. }, 30);
  56. tool.add_action("test", function(myarg) {
  57. expect(iii).to.equal(1)
  58. expect(myarg).to.equal("hello");
  59. iii++
  60. done();
  61. }, 20);
  62. tool.add_action("test", function(myarg) {
  63. expect(iii).to.equal(3)
  64. expect(myarg).to.equal("hello");
  65. iii++
  66. }, 50);
  67. tool.add_action("test", function(myarg) {
  68. expect(iii).to.equal(0)
  69. expect(myarg).to.equal("hello");
  70. iii++
  71. }, 10);
  72. tool.do_action("test", "hello");
  73. });
  74. })
  75. describe('add_filters apply_filters', function() {
  76. it('Can add an `filter` function to be called with apply_filters ARRAY', function(done) {
  77. var tool = tools();
  78. tool.add_filters("test", function(a) {
  79. a.push("test");
  80. return a;
  81. }, 10, "test");
  82. var t = tool.apply_filters("test", []);
  83. expect(t).to.be.an('array');
  84. expect(t.length).to.equal(1);
  85. expect(t[0]).to.equal("test");
  86. done();
  87. });
  88. it('Can add an `filter` function to be called with apply_filters ARRAY sorted', function(done) {
  89. var tool = tools();
  90. tool.add_filters("test", function(a) {
  91. a.push("test");
  92. return a;
  93. }, 10);
  94. tool.add_filters("test", function(a) {
  95. a.push("test2");
  96. return a;
  97. }, 5);
  98. var t = tool.apply_filters("test", []);
  99. expect(t).to.be.an('array');
  100. expect(t.length).to.equal(2);
  101. expect(t[0]).to.equal("test2");
  102. done();
  103. });
  104. it('Can add an `filter` function to be called with apply_filters ARRAY sorted 2', function(done) {
  105. var tool = tools();
  106. tool.add_filters("test", function(a) {
  107. if(a.length>0){
  108. a[0]=a[0]+":1";
  109. }
  110. return a;
  111. }, 10);
  112. var t = tool.apply_filters("test", ["1","2","3"]);
  113. expect(t).to.be.an('array');
  114. expect(t.length).to.equal(3);
  115. expect(t[0]).to.equal("1:1");
  116. done();
  117. });
  118. it('Can add an `filter` function to be called with apply_filters ARRAY sorted', function(done) {
  119. var tool = tools();
  120. tool.add_filters("test", function(a) {
  121. a.push(function(b, cb) {
  122. console.log("LLL", arguments);
  123. cb(null, b())
  124. })
  125. return a;
  126. }, 10);
  127. tool.add_filters("test", function(a) {
  128. a.push(function(b, cb) {
  129. console.log("oooo", arguments);
  130. cb(null, b())
  131. })
  132. return a;
  133. }, 5);
  134. tool.add_filters("testaa", function(a) {
  135. a.push(function(b, cb) {
  136. console.log("aaa", arguments);
  137. cb(null, b())
  138. })
  139. return a;
  140. }, 5);
  141. var t = tool.apply_filters_map(["test","testaa"],function(ao){
  142. console.log("OOOOOAAA",ao)
  143. }, function(err, result) {
  144. console.log("JJJJJJJJJJJJ", arguments);
  145. });
  146. console.log(t);
  147. expect(t).to.be.an('array');
  148. expect(t.length).to.equal(2);
  149. expect(t[0]).to.equal("test2");
  150. done();
  151. });
  152. })
  153. })