UNPKG

2.15 kBJavaScriptView Raw
1const assert = require('assert');
2const co = require('../cockrel');
3
4
5
6describe("Plugin: do", function() {
7
8
9 it("can execute a synchronous function", function(done) {
10
11 co.do((data) => { data.test = 2; return data; }, '@')
12 .do(check)
13 .begin({test: 1});
14
15 function check(data) {
16 assert.equal(data.test, 2);
17 done();
18 }
19
20 });
21
22 it("can execute a synchronous function with picked args", function(done) {
23
24 co
25 .do(check, '@arg1', '@arg2', '@', 'cnst', 'hi {arg1}', {foo : '@arg1'})
26 .begin({arg1: 1, arg2 : 2});
27
28 function check(arg1, arg2, all, cnst, interp, some) {
29 assert.equal(arg1, 1);
30 assert.equal(arg2, 2);
31 assert.equal(all.arg2, 2);
32 assert.equal(cnst, 'cnst');
33 assert.equal(interp, 'hi 1');
34 assert.equal(some.foo, 1);
35 done();
36 }
37
38 });
39
40 it("can handle a synchronous function that throws", function(done) {
41
42 co.do((data) => { throw new Error('uh oh'); })
43 .begin({test:1})
44 .catch(check);
45
46 function check(err) {
47 assert.equal(err, 'uh oh');
48 done();
49 }
50
51 });
52
53 it("can execute a promise function", function(done) {
54
55 co.do(testPromise)
56 .do(check)
57 .begin({test: 1});
58
59 function testPromise(data) {
60 return new Promise((res, rej) => {
61 data.foo = 'bar';
62 return res(data);
63 });
64 }
65
66 function check(data) {
67 assert.equal(data.foo, 'bar');
68 done();
69 }
70
71 });
72
73 it("can handle a promise function that rejects", function(done) {
74
75 co.do(testPromise).begin({test:1}).catch(check);
76
77 function testPromise(data) {
78 return new Promise((res, rej) => {
79 return rej('uh oh');
80 });
81 }
82
83 function check(err) {
84 assert.equal(err, 'uh oh');
85 done();
86 }
87
88 });
89
90 it("can execute a sub-chain", function(done) {
91
92 co.do(co.do(testPromise))
93 .do(check)
94 .begin({test: 1});
95
96 function testPromise(data) {
97 return new Promise((res, rej) => {
98 data.foo = 'bar';
99 return res(data);
100 });
101 }
102
103 function check(data) {
104 assert.equal(data.foo, 'bar');
105 done();
106 }
107
108 });
109
110
111});
112
113