test_utils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. module('utils');
  2. // for the tests, all hammer properties and methods of Hammer are exposed to window.$H
  3. test('get/set prefixed util', function() {
  4. ok(_.isUndefined($H.prefixed(window, 'FakeProperty')), 'non existent property returns undefined');
  5. window.webkitFakeProperty = 1337;
  6. ok($H.prefixed(window, 'FakeProperty') == 'webkitFakeProperty', 'existent prefixed property returns the prefixed name');
  7. delete window.webkitFakeProperty;
  8. });
  9. test('fnBind', function() {
  10. var context = { a: true };
  11. $H.bindFn(function(b) {
  12. ok(this.a === true, 'bindFn scope');
  13. ok(b === 123, 'bindFn argument');
  14. }, context)(123);
  15. });
  16. test('Inherit objects', function() {
  17. function Base() {
  18. this.name = true;
  19. }
  20. function Child() {
  21. Base.call(this);
  22. }
  23. $H.inherit(Child, Base, {
  24. newMethod: function() {
  25. }
  26. });
  27. var inst = new Child();
  28. ok(inst.name == true, 'child has extended from base');
  29. ok(inst.newMethod, 'child has a new method');
  30. ok(Child.prototype.newMethod, 'child has a new prototype method');
  31. ok(inst instanceof Child, 'is instanceof Child');
  32. ok(inst instanceof Base, 'is instanceof Base');
  33. ok(inst._super === Base.prototype, '_super is ref to prototype of Base');
  34. });
  35. test('toArray', function() {
  36. ok(_.isArray($H.toArray({ 0: true, 1: 'second', length: 2 })), 'converted an array-like object to an array');
  37. ok(_.isArray($H.toArray([true, true])), 'array stays an array');
  38. });
  39. test('inArray', function() {
  40. ok($H.inArray([1, 2, 3, 4, 'hammer'], 'hammer') === 4, 'found item and returned the index');
  41. ok($H.inArray([1, 2, 3, 4, 'hammer'], 'notfound') === -1, 'not found an item and returned -1');
  42. ok($H.inArray([
  43. {id: 2},
  44. {id: 24}
  45. ], '24', 'id') === 1, 'find by key and return the index');
  46. ok($H.inArray([
  47. {id: 2},
  48. {id: 24}
  49. ], '22', 'id') === -1, 'not found by key and return -1');
  50. });
  51. test('splitStr', function() {
  52. deepEqual($H.splitStr(' a b c d '), ['a', 'b', 'c', 'd'], 'str split valid');
  53. });
  54. test('uniqueArray', function() {
  55. deepEqual($H.uniqueArray([
  56. {id: 1},
  57. {id: 2},
  58. {id: 2}
  59. ], 'id'), [
  60. {id: 1},
  61. {id: 2}
  62. ], 'remove duplicate ids')
  63. });
  64. test('boolOrFn', function() {
  65. equal($H.boolOrFn(true), true, 'Passing an boolean');
  66. equal($H.boolOrFn(false), false, 'Passing an boolean');
  67. equal($H.boolOrFn(function() {
  68. return true;
  69. }), true, 'Passing an boolean');
  70. equal($H.boolOrFn(1), true, 'Passing an integer');
  71. });
  72. test('hasParent', function() {
  73. var parent = document.createElement('div'),
  74. child = document.createElement('div');
  75. document.body.appendChild(parent);
  76. parent.appendChild(child);
  77. equal($H.hasParent(child, parent), true, 'Found parent');
  78. equal($H.hasParent(parent, child), false, 'Not in parent');
  79. document.body.removeChild(parent);
  80. });
  81. test('each', function() {
  82. var object = { hi: true };
  83. var array = ['a', 'b', 'c'];
  84. var loop;
  85. loop = false;
  86. $H.each(object, function(value, key) {
  87. if (key == 'hi' && value === true) {
  88. loop = true;
  89. }
  90. });
  91. ok(loop, 'object loop');
  92. loop = 0;
  93. $H.each(array, function(value, key) {
  94. if (value) {
  95. loop++;
  96. }
  97. });
  98. ok(loop == 3, 'array loop');
  99. loop = 0;
  100. array.forEach = null;
  101. $H.each(array, function(value, key) {
  102. if (value) {
  103. loop++;
  104. }
  105. });
  106. ok(loop == 3, 'array loop without Array.forEach');
  107. });
  108. test('assign', function() {
  109. expect(2);
  110. deepEqual(
  111. $H.assign(
  112. {a: 1, b: 3},
  113. {b: 2, c: 3}
  114. ),
  115. {a: 1, b: 2, c: 3},
  116. 'Simple extend'
  117. );
  118. var src = { foo: true };
  119. var dest = $H.assign({}, src);
  120. src.foo = false;
  121. deepEqual(dest, {foo: true}, 'Clone reference');
  122. });
  123. test('test add/removeEventListener', function() {
  124. function handleEvent() {
  125. ok(true, 'triggered event');
  126. }
  127. expect(2);
  128. $H.addEventListeners(window, 'testEvent1 testEvent2 ', handleEvent);
  129. utils.triggerDomEvent(window, 'testEvent1');
  130. utils.triggerDomEvent(window, 'testEvent2');
  131. $H.removeEventListeners(window, ' testEvent1 testEvent2 ', handleEvent);
  132. utils.triggerDomEvent(window, 'testEvent1');
  133. utils.triggerDomEvent(window, 'testEvent2');
  134. });