source: frontend/node_modules/for-each/test/test.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[9af201e]1'use strict';
2
3var test = require('tape');
4var forEach = require('../');
5
6test('forEach calls each iterator', function (t) {
7 var count = 0;
8 t.plan(4);
9
10 forEach({ a: 1, b: 2 }, function (value, key) {
11 if (count === 0) {
12 t.equal(value, 1);
13 t.equal(key, 'a');
14 } else {
15 t.equal(value, 2);
16 t.equal(key, 'b');
17 }
18 count += 1;
19 });
20});
21
22test('forEach calls iterator with correct this value', function (t) {
23 var thisValue = {};
24
25 t.plan(1);
26
27 forEach([0], function () {
28 t.equal(this, thisValue);
29 }, thisValue);
30});
31
32test('second argument: iterator', function (t) {
33 /** @type {unknown[]} */
34 var arr = [];
35
36 // @ts-expect-error
37 t['throws'](function () { forEach(arr); }, TypeError, 'undefined is not a function');
38 // @ts-expect-error
39 t['throws'](function () { forEach(arr, null); }, TypeError, 'null is not a function');
40 // @ts-expect-error
41 t['throws'](function () { forEach(arr, ''); }, TypeError, 'string is not a function');
42 // @ts-expect-error
43 t['throws'](function () { forEach(arr, /a/); }, TypeError, 'regex is not a function');
44 // @ts-expect-error
45 t['throws'](function () { forEach(arr, true); }, TypeError, 'true is not a function');
46 // @ts-expect-error
47 t['throws'](function () { forEach(arr, false); }, TypeError, 'false is not a function');
48 // @ts-expect-error
49 t['throws'](function () { forEach(arr, NaN); }, TypeError, 'NaN is not a function');
50 // @ts-expect-error
51 t['throws'](function () { forEach(arr, 42); }, TypeError, '42 is not a function');
52
53 t.doesNotThrow(function () { forEach(arr, function () {}); }, 'function is a function');
54 // @ts-expect-error TODO fixme
55 t.doesNotThrow(function () { forEach(arr, setTimeout); }, 'setTimeout is a function');
56
57 /* eslint-env browser */
58 if (typeof window !== 'undefined') {
59 t.doesNotThrow(function () { forEach(arr, window.alert); }, 'alert is a function');
60 }
61
62 t.end();
63});
64
65test('array', function (t) {
66 var arr = /** @type {const} */ ([1, 2, 3]);
67
68 t.test('iterates over every item', function (st) {
69 var index = 0;
70 forEach(arr, function () { index += 1; });
71 st.equal(index, arr.length, 'iterates ' + arr.length + ' times');
72 st.end();
73 });
74
75 t.test('first iterator argument', function (st) {
76 var index = 0;
77 st.plan(arr.length);
78
79 forEach(arr, function (item) {
80 st.equal(arr[index], item, 'item ' + index + ' is passed as first argument');
81 index += 1;
82 });
83
84 st.end();
85 });
86
87 t.test('second iterator argument', function (st) {
88 var counter = 0;
89 st.plan(arr.length);
90
91 forEach(arr, function (_item, index) {
92 st.equal(counter, index, 'index ' + index + ' is passed as second argument');
93 counter += 1;
94 });
95
96 st.end();
97 });
98
99 t.test('third iterator argument', function (st) {
100 st.plan(arr.length);
101
102 forEach(arr, function (_item, _index, array) {
103 st.deepEqual(arr, array, 'array is passed as third argument');
104 });
105
106 st.end();
107 });
108
109 t.test('context argument', function (st) {
110 var context = {};
111
112 forEach([], function () {
113 st.equal(this, context, '"this" is the passed context');
114 }, context);
115
116 st.end();
117 });
118
119 t.end();
120});
121
122test('object', function (t) {
123 var obj = {
124 a: 1,
125 b: 2,
126 c: 3
127 };
128 var keys = /** @type {const} */ (['a', 'b', 'c']);
129
130 /** @constructor */
131 function F() {
132 this.a = 1;
133 this.b = 2;
134 }
135 F.prototype.c = 3;
136 var fKeys = /** @type {const} */ (['a', 'b']);
137
138 t.test('iterates over every object literal key', function (st) {
139 var counter = 0;
140
141 forEach(obj, function () { counter += 1; });
142
143 st.equal(counter, keys.length, 'iterated ' + counter + ' times');
144
145 st.end();
146 });
147
148 t.test('iterates only over own keys', function (st) {
149 var counter = 0;
150
151 forEach(new F(), function () { counter += 1; });
152
153 st.equal(counter, fKeys.length, 'iterated ' + fKeys.length + ' times');
154
155 st.end();
156 });
157
158 t.test('first iterator argument', function (st) {
159 var index = 0;
160 st.plan(keys.length);
161
162 forEach(obj, function (item) {
163 st.equal(obj[keys[index]], item, 'item at key ' + keys[index] + ' is passed as first argument');
164 index += 1;
165 });
166
167 st.end();
168 });
169
170 t.test('second iterator argument', function (st) {
171 var counter = 0;
172 st.plan(keys.length);
173
174 forEach(obj, function (_item, key) {
175 st.equal(keys[counter], key, 'key ' + key + ' is passed as second argument');
176 counter += 1;
177 });
178
179 st.end();
180 });
181
182 t.test('third iterator argument', function (st) {
183 st.plan(keys.length);
184
185 forEach(obj, function (_item, _key, object) {
186 st.deepEqual(obj, object, 'object is passed as third argument');
187 });
188
189 st.end();
190 });
191
192 t.test('context argument', function (st) {
193 var context = {};
194
195 forEach({}, function () {
196 st.equal(this, context, '"this" is the passed context');
197 }, context);
198
199 st.end();
200 });
201
202 t.end();
203});
204
205test('string', function (t) {
206 var str = /** @type {const} */ ('str');
207
208 t.test('second iterator argument', function (st) {
209 var counter = 0;
210 st.plan((str.length * 2) + 1);
211
212 forEach(str, function (item, index) {
213 st.equal(counter, index, 'index ' + index + ' is passed as second argument');
214 st.equal(str.charAt(index), item);
215 counter += 1;
216 });
217
218 st.equal(counter, str.length, 'iterates ' + str.length + ' times');
219
220 st.end();
221 });
222
223 t.end();
224});
Note: See TracBrowser for help on using the repository browser.