/
Action.spec.ts
344 lines (285 loc) · 7.97 KB
/
Action.spec.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { Action, isAction } from './Action';
import { Permission, isPermission } from '../permission/Permission';
import { DataTypeString } from '../datatype/dataTypes';
import { buildObjectDataType } from '../datatype/ObjectDataType';
import { passOrThrow } from '../util';
describe('Action', () => {
it('should have a name', () => {
function fn() {
// eslint-disable-next-line no-new
new Action();
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should have a description', () => {
function fn() {
// eslint-disable-next-line no-new
new Action({
name: 'example',
});
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should allow definitions without input', () => {
function fn() {
// eslint-disable-next-line no-new
new Action({
name: 'example',
description: 'do something',
resolve() {},
});
}
expect(fn).not.toThrow();
});
it('should have a valid input definition', () => {
function fn() {
// eslint-disable-next-line no-new
new Action({
name: 'example',
description: 'do something',
input: 123,
output: 456,
});
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should allow definitions without output', () => {
function fn() {
// eslint-disable-next-line no-new
new Action({
name: 'example',
description: 'do something',
input() {},
resolve() {},
});
}
expect(fn).not.toThrow();
});
it('should have a valid output definition', () => {
function fn() {
// eslint-disable-next-line no-new
new Action({
name: 'example',
description: 'do something',
input() {},
output: 'wrong',
});
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should have valid input and output definitions when generated via a function', () => {
// eslint-disable-next-line no-new
const action = new Action({
name: 'example',
description: 'do something',
input() {
return 123;
},
output() {},
resolve() {},
});
function fn1() {
action.getInput();
}
function fn2() {
action.getOutput();
}
expect(fn1).toThrowErrorMatchingSnapshot();
expect(fn2).toThrowErrorMatchingSnapshot();
});
it('should have valid input data types', () => {
const action = new Action({
name: 'example',
description: 'do something',
input: {
someAttr: {},
},
output: {},
resolve() {},
});
function fn() {
action.getInput();
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should have valid output data types', () => {
const action = new Action({
name: 'example',
description: 'do something',
input: {},
output: {
someAttr: {},
},
resolve() {},
});
function fn() {
action.getOutput();
}
expect(fn).toThrowErrorMatchingSnapshot();
});
// it('should have valid input attributes if valid defaultValue functions', () => {
// const action = new Action({
// name: 'example',
// description: 'do something',
// input: {
// type: DataTypeString,
// defaultValue: 'not-a-function'
// },
// output: {},
// resolve() {},
// })
// function fn() {
// action.getInput()
// }
// expect(fn).toThrowErrorMatchingSnapshot();
// })
it('should have a valid resolve function', () => {
function fn() {
// eslint-disable-next-line no-new
new Action({
name: 'example',
description: 'do something',
});
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it("should return it's name", () => {
const action = new Action({
name: 'example',
description: 'do something',
input: {},
output: {},
resolve() {},
});
expect(action.name).toBe('example');
expect(String(action)).toBe('example');
});
it('should return input and output params', () => {
const action1 = new Action({
name: 'example',
description: 'do something',
resolve() {},
});
const input1 = action1.getInput();
const output1 = action1.getOutput();
expect(input1).toEqual(null);
expect(output1).toEqual(null);
const hasInput1 = action1.hasInput();
const hasOutput1 = action1.hasOutput();
expect(hasInput1).toBe(false);
expect(hasOutput1).toBe(false);
const action2 = new Action({
name: 'example',
description: 'do something',
input: {
type: buildObjectDataType({
attributes: {
firstName: {
description: 'First name',
type: DataTypeString,
defaultValue() {
return 'Waldo';
},
},
lastName: {
description: 'Last name',
type: DataTypeString,
required: true,
},
about: {
description: 'Just some description',
type: buildObjectDataType({
attributes: {
favouriteActor: {
type: DataTypeString,
description: 'One more description',
},
},
}),
},
},
}),
},
output: {
type: buildObjectDataType({
attributes: {
luckyNumber: {
description: 'Lucky number',
type: DataTypeString,
},
},
}),
},
resolve() {},
});
const input2 = action2.getInput();
const output2 = action2.getOutput();
expect(input2).toMatchSnapshot();
expect(output2).toMatchSnapshot();
const attributes2 = input2.type.getAttributes();
expect(attributes2).toMatchSnapshot();
expect(typeof attributes2.firstName.defaultValue).toBe('function');
const nestedAttributes2 = attributes2.about.type.getAttributes();
expect(nestedAttributes2).toMatchSnapshot();
const hasInput2 = action2.hasInput();
const hasOutput2 = action2.hasOutput();
expect(hasInput2).toBe(true);
expect(hasOutput2).toBe(true);
});
describe('isAction', () => {
const action = new Action({
name: 'example',
description: 'do something',
input: {},
output: {},
resolve() {},
});
it('should recognize objects of type Action', () => {
function fn() {
passOrThrow(isAction(action), () => 'This error will never happen');
}
expect(fn).not.toThrow();
});
it('should recognize non-Action objects', () => {
function fn() {
passOrThrow(
isAction({}) || isAction(function test() {}) || isAction(Error),
() => 'Not a Action object',
);
}
expect(fn).toThrowErrorMatchingSnapshot();
});
});
describe('permissions', () => {
it('should return permissions', () => {
const action = new Action({
name: 'example',
description: 'do something',
input: {},
output: {},
resolve() {},
permissions: new Permission().authenticated(),
});
const permissions = action.getPermissions();
expect(isPermission(permissions)).toBe(true);
});
it('should throw if empty permissions are provided', () => {
const action = new Action({
name: 'example',
description: 'do something',
input: {},
output: {},
resolve() {},
permissions: [new Permission().authenticated(), new Permission()],
});
function fn() {
action.getPermissions();
}
expect(fn).toThrowErrorMatchingSnapshot();
});
});
});
/* eslint-enable @typescript-eslint/no-empty-function */
/* eslint-enable @typescript-eslint/explicit-function-return-type */