-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexecute.test.ts
More file actions
148 lines (117 loc) · 2.8 KB
/
execute.test.ts
File metadata and controls
148 lines (117 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import test from 'ava';
import execute from '../src/execute';
const wait = `function wait() {
return (state) =>
new Promise((resolve) => {
setTimeout(() => resolve(state), 2);
});
};`;
test.serial('should return state', async (t) => {
const state = { data: { x: 1 } };
const job = `
fn(s => s)
`;
const result = await execute(job, state);
t.deepEqual(state, result);
});
test.serial('should use .then()', async (t) => {
const state = { data: { x: 1 } };
const job = `
fn(s => s)
.then((s) =>
({
data: { x: 33 }
})
)
`;
const result = await execute(job, state);
t.deepEqual(result, { data: { x: 33 } });
});
test.serial('should chain .then() with state', async (t) => {
const state = { data: { x: 1 } };
const job = `
fn(s => ({ x: 1 }))
.then((s) =>
({
x: s.x + 1
})
)
`;
const result = await execute(job, state);
t.deepEqual(result, { x: 2 });
});
test.serial('should use .then() as an argument', async (t) => {
const state = {};
const job = `fn(
fn(() => ({ x: 5 })).then((s) => ({ x: s.x + 1}))
)`;
const result = await execute(job, state);
t.deepEqual(result, { x: 6 });
});
test.serial('use then() with wait()', async (t) => {
const state = {
data: {
x: 22,
},
};
const job = `${wait}
wait().then(fn(s => s))`;
const result = await execute(job, state);
t.deepEqual(result.data, { x: 22 });
});
test.serial('catch an error and return it', async (t) => {
const state = {
data: {
x: 22,
},
};
const job = `fn(() => {
throw { err: true }
}).catch(e => e)`;
const result = await execute(job, state);
t.deepEqual(result, { err: true });
});
test.serial('catch an error and re-throw it', async (t) => {
const state = {
data: {
x: 22,
},
};
const job = `fn(() => {
throw new Error('err')
}).catch(e => { throw e })`;
const result = await execute(job, state);
t.is(result.errors['job-1'].name, 'JobError');
t.is(result.errors['job-1'].message, 'err');
});
test.serial('catch an error and return state', async (t) => {
const state = {
data: {
x: 22,
},
};
const job = `fn(() => {
throw { err: true }
}).catch((e, s) => s)`;
const result = await execute(job, state);
t.deepEqual(result, state);
});
test.serial('each with then ', async (t) => {
const state = {
ids: [1, 2, 3],
results: [],
};
const job = `each($.ids,
get(\`https://jsonplaceholder.typicode.com/todos/\${$.data}\`).then(
(s) => {
s.results.push(s.data);
return s;
}
)
)`;
const result = await execute(job, state, 'http');
t.is(result.results.length, 3);
t.is(result.results[0].id, 1);
t.is(result.results[1].id, 2);
t.is(result.results[2].id, 3);
});