tools2.test.js 7.5 KB

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