test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use strict';
  2. var test = require('tape');
  3. var forEach = require('../');
  4. test('forEach calls each iterator', function (t) {
  5. var count = 0;
  6. t.plan(4);
  7. forEach({ a: 1, b: 2 }, function (value, key) {
  8. if (count === 0) {
  9. t.equal(value, 1);
  10. t.equal(key, 'a');
  11. } else {
  12. t.equal(value, 2);
  13. t.equal(key, 'b');
  14. }
  15. count += 1;
  16. });
  17. });
  18. test('forEach calls iterator with correct this value', function (t) {
  19. var thisValue = {};
  20. t.plan(1);
  21. forEach([0], function () {
  22. t.equal(this, thisValue);
  23. }, thisValue);
  24. });
  25. test('second argument: iterator', function (t) {
  26. /** @type {unknown[]} */
  27. var arr = [];
  28. // @ts-expect-error
  29. t['throws'](function () { forEach(arr); }, TypeError, 'undefined is not a function');
  30. // @ts-expect-error
  31. t['throws'](function () { forEach(arr, null); }, TypeError, 'null is not a function');
  32. // @ts-expect-error
  33. t['throws'](function () { forEach(arr, ''); }, TypeError, 'string is not a function');
  34. // @ts-expect-error
  35. t['throws'](function () { forEach(arr, /a/); }, TypeError, 'regex is not a function');
  36. // @ts-expect-error
  37. t['throws'](function () { forEach(arr, true); }, TypeError, 'true is not a function');
  38. // @ts-expect-error
  39. t['throws'](function () { forEach(arr, false); }, TypeError, 'false is not a function');
  40. // @ts-expect-error
  41. t['throws'](function () { forEach(arr, NaN); }, TypeError, 'NaN is not a function');
  42. // @ts-expect-error
  43. t['throws'](function () { forEach(arr, 42); }, TypeError, '42 is not a function');
  44. t.doesNotThrow(function () { forEach(arr, function () {}); }, 'function is a function');
  45. // @ts-expect-error TODO fixme
  46. t.doesNotThrow(function () { forEach(arr, setTimeout); }, 'setTimeout is a function');
  47. /* eslint-env browser */
  48. if (typeof window !== 'undefined') {
  49. t.doesNotThrow(function () { forEach(arr, window.alert); }, 'alert is a function');
  50. }
  51. t.end();
  52. });
  53. test('array', function (t) {
  54. var arr = /** @type {const} */ ([1, 2, 3]);
  55. t.test('iterates over every item', function (st) {
  56. var index = 0;
  57. forEach(arr, function () { index += 1; });
  58. st.equal(index, arr.length, 'iterates ' + arr.length + ' times');
  59. st.end();
  60. });
  61. t.test('first iterator argument', function (st) {
  62. var index = 0;
  63. st.plan(arr.length);
  64. forEach(arr, function (item) {
  65. st.equal(arr[index], item, 'item ' + index + ' is passed as first argument');
  66. index += 1;
  67. });
  68. st.end();
  69. });
  70. t.test('second iterator argument', function (st) {
  71. var counter = 0;
  72. st.plan(arr.length);
  73. forEach(arr, function (_item, index) {
  74. st.equal(counter, index, 'index ' + index + ' is passed as second argument');
  75. counter += 1;
  76. });
  77. st.end();
  78. });
  79. t.test('third iterator argument', function (st) {
  80. st.plan(arr.length);
  81. forEach(arr, function (_item, _index, array) {
  82. st.deepEqual(arr, array, 'array is passed as third argument');
  83. });
  84. st.end();
  85. });
  86. t.test('context argument', function (st) {
  87. var context = {};
  88. forEach([], function () {
  89. st.equal(this, context, '"this" is the passed context');
  90. }, context);
  91. st.end();
  92. });
  93. t.end();
  94. });
  95. test('object', function (t) {
  96. var obj = {
  97. a: 1,
  98. b: 2,
  99. c: 3
  100. };
  101. var keys = /** @type {const} */ (['a', 'b', 'c']);
  102. /** @constructor */
  103. function F() {
  104. this.a = 1;
  105. this.b = 2;
  106. }
  107. F.prototype.c = 3;
  108. var fKeys = /** @type {const} */ (['a', 'b']);
  109. t.test('iterates over every object literal key', function (st) {
  110. var counter = 0;
  111. forEach(obj, function () { counter += 1; });
  112. st.equal(counter, keys.length, 'iterated ' + counter + ' times');
  113. st.end();
  114. });
  115. t.test('iterates only over own keys', function (st) {
  116. var counter = 0;
  117. forEach(new F(), function () { counter += 1; });
  118. st.equal(counter, fKeys.length, 'iterated ' + fKeys.length + ' times');
  119. st.end();
  120. });
  121. t.test('first iterator argument', function (st) {
  122. var index = 0;
  123. st.plan(keys.length);
  124. forEach(obj, function (item) {
  125. st.equal(obj[keys[index]], item, 'item at key ' + keys[index] + ' is passed as first argument');
  126. index += 1;
  127. });
  128. st.end();
  129. });
  130. t.test('second iterator argument', function (st) {
  131. var counter = 0;
  132. st.plan(keys.length);
  133. forEach(obj, function (_item, key) {
  134. st.equal(keys[counter], key, 'key ' + key + ' is passed as second argument');
  135. counter += 1;
  136. });
  137. st.end();
  138. });
  139. t.test('third iterator argument', function (st) {
  140. st.plan(keys.length);
  141. forEach(obj, function (_item, _key, object) {
  142. st.deepEqual(obj, object, 'object is passed as third argument');
  143. });
  144. st.end();
  145. });
  146. t.test('context argument', function (st) {
  147. var context = {};
  148. forEach({}, function () {
  149. st.equal(this, context, '"this" is the passed context');
  150. }, context);
  151. st.end();
  152. });
  153. t.end();
  154. });
  155. test('string', function (t) {
  156. var str = /** @type {const} */ ('str');
  157. t.test('second iterator argument', function (st) {
  158. var counter = 0;
  159. st.plan((str.length * 2) + 1);
  160. forEach(str, function (item, index) {
  161. st.equal(counter, index, 'index ' + index + ' is passed as second argument');
  162. st.equal(str.charAt(index), item);
  163. counter += 1;
  164. });
  165. st.equal(counter, str.length, 'iterates ' + str.length + ' times');
  166. st.end();
  167. });
  168. t.end();
  169. });