|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -api.metatests.test('map', (test) => { |
| 3 | +api.metatests.test('for.map', test => { |
4 | 4 | const data = [1, 2, 3, 4];
|
5 | 5 | const expected = [2, 4, 6, 8];
|
6 | 6 | const fn = (item, callback) => process.nextTick(() => {
|
7 | 7 | callback(null, item * 2);
|
8 | 8 | });
|
| 9 | + api.metasync |
| 10 | + .for(data) |
| 11 | + .map(fn) |
| 12 | + .fetch((error, result) => { |
| 13 | + test.strictSame(result, expected); |
| 14 | + test.end(); |
| 15 | + }); |
| 16 | +}); |
9 | 17 |
|
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 | + }); |
15 | 109 | });
|
0 commit comments