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

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 5.4 KB
Line 
1'use strict';
2
3/* globals window */
4
5var test = require('tape');
6var forEach = require('../');
7
8test('forEach calls each iterator', function (t) {
9 var count = 0;
10 t.plan(4);
11 forEach({ a: 1, b: 2 }, function (value, key) {
12 if (count === 0) {
13 t.equal(value, 1);
14 t.equal(key, 'a');
15 } else {
16 t.equal(value, 2);
17 t.equal(key, 'b');
18 }
19 count += 1;
20 });
21});
22
23test('forEach calls iterator with correct this value', function (t) {
24 var thisValue = {};
25
26 t.plan(1);
27
28 forEach([0], function () {
29 t.equal(this, thisValue);
30 }, thisValue);
31});
32
33test('second argument: iterator', function (t) {
34 var arr = [];
35 t['throws'](function () { forEach(arr); }, TypeError, 'undefined is not a function');
36 t['throws'](function () { forEach(arr, null); }, TypeError, 'null is not a function');
37 t['throws'](function () { forEach(arr, ''); }, TypeError, 'string is not a function');
38 t['throws'](function () { forEach(arr, /a/); }, TypeError, 'regex is not a function');
39 t['throws'](function () { forEach(arr, true); }, TypeError, 'true is not a function');
40 t['throws'](function () { forEach(arr, false); }, TypeError, 'false is not a function');
41 t['throws'](function () { forEach(arr, NaN); }, TypeError, 'NaN is not a function');
42 t['throws'](function () { forEach(arr, 42); }, TypeError, '42 is not a function');
43 t.doesNotThrow(function () { forEach(arr, function () {}); }, 'function is a function');
44 t.doesNotThrow(function () { forEach(arr, setTimeout); }, 'setTimeout is a function');
45 if (typeof window !== 'undefined') {
46 t.doesNotThrow(function () { forEach(arr, window.alert); }, 'alert is a function');
47 }
48 t.end();
49});
50
51test('array', function (t) {
52 var arr = [1, 2, 3];
53
54 t.test('iterates over every item', function (st) {
55 var index = 0;
56 forEach(arr, function () { index += 1; });
57 st.equal(index, arr.length, 'iterates ' + arr.length + ' times');
58 st.end();
59 });
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
71 t.test('second iterator argument', function (st) {
72 var counter = 0;
73 st.plan(arr.length);
74 forEach(arr, function (item, index) {
75 st.equal(counter, index, 'index ' + index + ' is passed as second argument');
76 counter += 1;
77 });
78 st.end();
79 });
80
81 t.test('third iterator argument', function (st) {
82 st.plan(arr.length);
83 forEach(arr, function (item, index, array) {
84 st.deepEqual(arr, array, 'array is passed as third argument');
85 });
86 st.end();
87 });
88
89 t.test('context argument', function (st) {
90 var context = {};
91 forEach([], function () {
92 st.equal(this, context, '"this" is the passed context');
93 }, context);
94 st.end();
95 });
96
97 t.end();
98});
99
100test('object', function (t) {
101 var obj = {
102 a: 1,
103 b: 2,
104 c: 3
105 };
106 var keys = ['a', 'b', 'c'];
107
108 var F = function F() {
109 this.a = 1;
110 this.b = 2;
111 };
112 F.prototype.c = 3;
113 var fKeys = ['a', 'b'];
114
115 t.test('iterates over every object literal key', function (st) {
116 var counter = 0;
117 forEach(obj, function () { counter += 1; });
118 st.equal(counter, keys.length, 'iterated ' + counter + ' times');
119 st.end();
120 });
121
122 t.test('iterates only over own keys', function (st) {
123 var counter = 0;
124 forEach(new F(), function () { counter += 1; });
125 st.equal(counter, fKeys.length, 'iterated ' + fKeys.length + ' times');
126 st.end();
127 });
128
129 t.test('first iterator argument', function (st) {
130 var index = 0;
131 st.plan(keys.length);
132 forEach(obj, function (item) {
133 st.equal(obj[keys[index]], item, 'item at key ' + keys[index] + ' is passed as first argument');
134 index += 1;
135 });
136 st.end();
137 });
138
139 t.test('second iterator argument', function (st) {
140 var counter = 0;
141 st.plan(keys.length);
142 forEach(obj, function (item, key) {
143 st.equal(keys[counter], key, 'key ' + key + ' is passed as second argument');
144 counter += 1;
145 });
146 st.end();
147 });
148
149 t.test('third iterator argument', function (st) {
150 st.plan(keys.length);
151 forEach(obj, function (item, key, object) {
152 st.deepEqual(obj, object, 'object is passed as third argument');
153 });
154 st.end();
155 });
156
157 t.test('context argument', function (st) {
158 var context = {};
159 forEach({}, function () {
160 st.equal(this, context, '"this" is the passed context');
161 }, context);
162 st.end();
163 });
164
165 t.end();
166});
167
168test('string', function (t) {
169 var str = 'str';
170 t.test('second iterator argument', function (st) {
171 var counter = 0;
172 st.plan((str.length * 2) + 1);
173 forEach(str, function (item, index) {
174 st.equal(counter, index, 'index ' + index + ' is passed as second argument');
175 st.equal(str.charAt(index), item);
176 counter += 1;
177 });
178 st.equal(counter, str.length, 'iterates ' + str.length + ' times');
179 st.end();
180 });
181 t.end();
182});
Note: See TracBrowser for help on using the repository browser.