tests.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. function equals(a, b) {
  3. return !!(JSON.stringify(a) === JSON.stringify(b));
  4. }
  5. Tinytest.add('ReactiveList - definition', function(test) {
  6. test.isTrue(typeof ReactiveList !== 'undefined', 'ReactiveList is undefined, make sure to add the reactive-list package');
  7. });
  8. // We do a small test of insert and remove
  9. Tinytest.addAsync('ReactiveList - basic insert and remove - test 1', function test1 (test, onComplete) {
  10. Meteor.setTimeout(function() {
  11. var list = new ReactiveList({
  12. reactive: true
  13. });
  14. var testLength = 500;
  15. for (var i = 0; i < testLength; i++)
  16. list.insert(i, 'value' + i);
  17. test.equal(list._length, testLength, 'List length is not as expected');
  18. var order = 0;
  19. list.forEach(function(value, key) {
  20. test.equal(key, order, 'order is not as expected');
  21. test.equal(value, 'value'+order, 'order is not as expected');
  22. order++;
  23. });
  24. test.equal(order, testLength, 'forEach length is not as expected');
  25. list.forEachReverse(function(value, key) {
  26. order--;
  27. test.equal(key, order, 'order is not as expected');
  28. test.equal(value, 'value'+order, 'order is not as expected');
  29. });
  30. test.equal(order, 0, 'forEachReverse length is not as expected');
  31. // Remove all items
  32. for (var i = 0; i < testLength; i++)
  33. list.remove(i);
  34. test.equal(list._length, 0, 'List length is not as expected');
  35. test.isUndefined(list.first, 'First should now be undefined');
  36. test.isUndefined(list.last, 'Last should now be undefined');
  37. onComplete();
  38. }, 1000);
  39. });
  40. // We test insert and remove on larger scale
  41. Tinytest.addAsync('ReactiveList - basic insert and reset - test 2', function test2 (test, onComplete) {
  42. Meteor.setTimeout(function() {
  43. var list = new ReactiveList({
  44. reactive: true
  45. });
  46. var testLength = 500;
  47. for (var i = 0; i < testLength; i++)
  48. list.insert(i, 'value' + i);
  49. test.equal(list._length, testLength, 'List length is not as expected');
  50. // Remove all items
  51. list.reset();
  52. test.equal(list._length, 0, 'List length is not as expected');
  53. test.isUndefined(list.first, 'First should now be undefined');
  54. test.isUndefined(list.last, 'Last should now be undefined');
  55. onComplete();
  56. }, 5000);
  57. });
  58. //Test API:
  59. //test.isFalse(v, msg)
  60. //test.isTrue(v, msg)
  61. //test.equalactual, expected, message, not
  62. //test.length(obj, len)
  63. //test.include(s, v)
  64. //test.isNaN(v, msg)
  65. //test.isUndefined(v, msg)
  66. //test.isNotNull
  67. //test.isNull
  68. //test.throws(func)
  69. //test.instanceOf(obj, klass)
  70. //test.notEqual(actual, expected, message)
  71. //test.runId()
  72. //test.exception(exception)
  73. //test.expect_fail()
  74. //test.ok(doc)
  75. //test.fail(doc)
  76. //test.equal(a, b, msg)