Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

test.js 5.4KB

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