Skip to content

Commit 15bb5f1

Browse files
committed
Refactor for/chain tests and move to /test folder
PR-URL: #371
1 parent c0188e5 commit 15bb5f1

File tree

3 files changed

+100
-114
lines changed

3 files changed

+100
-114
lines changed

test/chain.js

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,109 @@
11
'use strict';
22

3-
api.metatests.test('map', (test) => {
3+
api.metatests.test('for.map', test => {
44
const data = [1, 2, 3, 4];
55
const expected = [2, 4, 6, 8];
66
const fn = (item, callback) => process.nextTick(() => {
77
callback(null, item * 2);
88
});
9+
api.metasync
10+
.for(data)
11+
.map(fn)
12+
.fetch((error, result) => {
13+
test.strictSame(result, expected);
14+
test.end();
15+
});
16+
});
917

10-
api.metasync.for(data).map(fn).fetch((err, result) => {
11-
test.error(err, 'must not return an error');
12-
test.strictSame(result, expected, `result should be: ${expected}`);
13-
test.end();
14-
});
18+
api.metatests.test('for chain sync', test => {
19+
api.metasync
20+
.for([1, 2, 3, 4])
21+
.filter((item, cb) => cb(null, item % 2 === 0))
22+
.map((item, cb) => cb(null, item * 2))
23+
.reduce((a, b, cb) => cb(null, a + b))
24+
.fetch((error, result) => {
25+
test.error(error);
26+
test.strictSame(result, 12); // 2 * 2 + 4 * 2
27+
test.end();
28+
});
29+
});
30+
31+
api.metatests.test('for chain async', test => {
32+
api.metasync
33+
.for([1, 2, 3, 4])
34+
.filter((item, cb) => process.nextTick(cb, null, item % 2 === 0))
35+
.map((item, cb) => process.nextTick(cb, null, item * 2))
36+
.reduce((a, b, cb) => process.nextTick(cb, null, a + b))
37+
.fetch((error, result) => {
38+
test.error(error);
39+
test.strictSame(result, 12); // 2 * 2 + 4 * 2
40+
test.end();
41+
});
42+
});
43+
44+
api.metatests.test('for chain error', test => {
45+
api.metasync
46+
.for([1, 2, 3, 4])
47+
.filter((item, cb) => cb(null, item % 2 === 0))
48+
.map((item, cb) => cb(new Error('Something happens')))
49+
.reduce((a, b, cb) => cb(null, a + b))
50+
.fetch((error, result) => {
51+
test.strictSame(error instanceof Error, true);
52+
test.strictSame(result, undefined);
53+
test.end();
54+
});
55+
});
56+
57+
api.metatests.test('for chain after fetch', test => {
58+
api.metasync
59+
.for([1, 2, 3, 4])
60+
.map((item, cb) => cb(null, item * item))
61+
.filter((item, cb) => cb(null, item > 5))
62+
.fetch((error, result, resume) => {
63+
test.error(error);
64+
test.strictSame(result, [9, 16]);
65+
resume(null, result);
66+
})
67+
.filter((item, cb) => {
68+
cb(null, item > 10);
69+
})
70+
.map((item, cb) => {
71+
cb(null, --item);
72+
})
73+
.fetch((error, result) => {
74+
test.error(error);
75+
test.strictSame(result, [15]);
76+
test.end();
77+
});
78+
});
79+
80+
api.metatests.test('for chain all methods', test => {
81+
api.metasync
82+
.for([1, 2, 3, 4])
83+
.concat([8, 6, 7])
84+
.slice(1)
85+
.sort()
86+
.reverse()
87+
.shift()
88+
.unshift(10)
89+
.pop()
90+
.push(11)
91+
.fetch((error, result, resume) => {
92+
test.error(error);
93+
const expected = [10, 7, 6, 4, 3, 11];
94+
test.strictSame(result, expected);
95+
resume(null, result);
96+
})
97+
.includes(6)
98+
.fetch((error, result, resume) => {
99+
test.error(error);
100+
test.strictSame(result, true);
101+
resume(null, [6, 8]);
102+
})
103+
.includes(7)
104+
.fetch((error, result) => {
105+
test.error(error);
106+
test.strictSame(result, false);
107+
test.end();
108+
});
15109
});

tests/all.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const tests = [
4-
'chain',
54
'composition',
65
'composition.pause',
76
'composition.parallel',

tests/chain.js

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)