vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
2.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. var assert = require('assert');
  2. var Queue = require('../lib/Queue');
  3. describe('Queue', function () {
  4. it('queue actions', function () {
  5. var queue = new Queue();
  6. var count = 0;
  7. function inc() {
  8. count++;
  9. }
  10. queue.queue(inc);
  11. assert.equal(count, 0);
  12. queue.flush();
  13. assert.equal(count, 1);
  14. });
  15. it('queue actions with a delay', function (done) {
  16. var queue = new Queue({delay: 25});
  17. var count = 0;
  18. function inc() {
  19. count++;
  20. }
  21. queue.queue(inc);
  22. assert.equal(count, 0);
  23. setTimeout(function () {
  24. assert.equal(count, 1);
  25. done();
  26. }, 50);
  27. });
  28. it('queue multiple actions with a delay', function (done) {
  29. var queue = new Queue({delay: 100});
  30. var count = 0;
  31. function inc() {
  32. count++;
  33. }
  34. queue.queue(inc);
  35. assert.equal(count, 0);
  36. setTimeout(function () {
  37. queue.queue(inc);
  38. assert.equal(count, 0);
  39. // flush should now occur after 100 ms from now, lets test after 75 and 125 ms
  40. setTimeout(function () {
  41. assert.equal(count, 0);
  42. setTimeout(function () {
  43. assert.equal(count, 2);
  44. done();
  45. }, 50);
  46. }, 75);
  47. }, 50);
  48. });
  49. it('flush when the configured maximum is exceeded', function () {
  50. var queue = new Queue({max: 4});
  51. var count = 0;
  52. function inc() {
  53. count++;
  54. }
  55. queue.queue(inc);
  56. queue.queue(inc);
  57. assert.equal(count, 0);
  58. queue.queue(inc);
  59. queue.queue(inc);
  60. queue.queue(inc);
  61. assert.equal(count, 5);
  62. });
  63. it('queue actions with args', function () {
  64. var queue = new Queue();
  65. var count = 0;
  66. function add(value) {
  67. count += value;
  68. }
  69. queue.queue({fn: add, args: [2]});
  70. assert.equal(count, 0);
  71. queue.flush();
  72. assert.equal(count, 2);
  73. });
  74. it('queue actions with args and context', function () {
  75. var queue = new Queue();
  76. var obj = {
  77. count: 0,
  78. add: function (value) {
  79. this.count += value;
  80. }
  81. };
  82. queue.queue({context: obj, fn: obj.add, args: [2]});
  83. assert.equal(obj.count, 0);
  84. queue.flush();
  85. assert.equal(obj.count, 2);
  86. });
  87. it('replace functions on an object', function () {
  88. var queue = new Queue();
  89. var obj = {
  90. count: 0,
  91. add: function (value) {
  92. this.count += value;
  93. }
  94. };
  95. queue.replace(obj, 'add');
  96. obj.add(3);
  97. assert.equal(obj.count, 0);
  98. queue.flush();
  99. assert.equal(obj.count, 3);
  100. });
  101. it('extend an object', function () {
  102. var obj = {
  103. count: 0,
  104. add: function (value) {
  105. this.count += value;
  106. },
  107. subtract: function (value) {
  108. this.count -= value;
  109. }
  110. };
  111. Queue.extend(obj, {replace: ['add', 'subtract']});
  112. obj.add(3);
  113. obj.subtract(1);
  114. assert.equal(obj.count, 0);
  115. obj.flush();
  116. assert.equal(obj.count, 2);
  117. });
  118. });