|
17 | 17 | import { ValidationError, WorkflowValidator } from '../../src'; |
18 | 18 | import { Workflow } from '../../src/lib/definitions/workflow'; |
19 | 19 |
|
20 | | -describe('workflow-validator', () => { |
| 20 | +const validWorkflow = { |
| 21 | + id: 'helloworld', |
| 22 | + version: '1.0', |
| 23 | + specVersion: '0.7', |
| 24 | + name: 'Hello World Workflow', |
| 25 | + description: 'Inject Hello World', |
| 26 | + start: 'Hello State', |
| 27 | + states: [ |
| 28 | + { |
| 29 | + name: 'Hello State', |
| 30 | + type: 'inject', |
| 31 | + data: { |
| 32 | + result: 'Hello World!', |
| 33 | + }, |
| 34 | + end: true, |
| 35 | + }, |
| 36 | + ], |
| 37 | +}; |
| 38 | + |
| 39 | +describe('workflow-validator, invalid state', () => { |
21 | 40 | it('should return errors instance of ValidationError if the workflow provided is not valid', () => { |
22 | 41 | // @ts-ignore |
23 | | - const workflow = { |
24 | | - id: 'helloworld', |
25 | | - name: 'Hello World Workflow', |
26 | | - version: '1.0', |
27 | | - specVersion: '0.7', |
28 | | - description: 'Inject Hello World', |
29 | | - start: 'Hello State', |
30 | | - states: [], |
31 | | - } as Workflow; |
| 42 | + const workflowWithEmptyStates = Object.assign({ ...validWorkflow }, { states: [] }) as Workflow; |
32 | 43 |
|
33 | | - const workflowValidator = new WorkflowValidator(workflow); |
34 | | - expect(workflowValidator.isValid).toBeFalsy('Expected isValid to be false'); |
35 | | - expect(workflowValidator.errors.length).toBe(1); |
36 | | - expect(workflowValidator.errors[0].constructor === ValidationError).toBeTruthy( |
37 | | - 'Expected errors to be instance of ValidationError' |
38 | | - ); |
| 44 | + const workflowValidator = new WorkflowValidator(workflowWithEmptyStates); |
| 45 | + |
| 46 | + const numErrors = 1; |
| 47 | + expectInvalidWorkflow(workflowValidator, numErrors); |
39 | 48 | expect(workflowValidator.errors[0].message).toMatch('states'); |
40 | 49 | }); |
41 | 50 |
|
| 51 | + it('should check if specVersion match the supported sdk version', () => { |
| 52 | + // @ts-ignore |
| 53 | + const workflowWithInvalidSpecVersion = Object.assign({ ...validWorkflow }, { specVersion: '0.1' }) as Workflow; |
| 54 | + |
| 55 | + const workflowValidator = new WorkflowValidator(workflowWithInvalidSpecVersion); |
| 56 | + |
| 57 | + const numErrors = 1; |
| 58 | + expectInvalidWorkflow(workflowValidator, numErrors); |
| 59 | + |
| 60 | + expect(workflowValidator.errors[0].message).toMatch('specVersion'); |
| 61 | + }); |
| 62 | + |
| 63 | + function expectInvalidWorkflow(workflowValidator: WorkflowValidator, numErrors: number) { |
| 64 | + expect(workflowValidator.isValid).toBeFalsy('Expected isValid to be false'); |
| 65 | + expect(workflowValidator.errors.length).toBe(numErrors); |
| 66 | + workflowValidator.errors.forEach((error) => { |
| 67 | + expect(error.constructor === ValidationError).toBeTruthy('Expected errors to be instance of ValidationError'); |
| 68 | + }); |
| 69 | + } |
| 70 | +}); |
| 71 | + |
| 72 | +describe('workflow-validator, valid state', () => { |
42 | 73 | it('should have no errors if the workflow is valid', () => { |
43 | 74 | // @ts-ignore |
44 | | - const workflow = { |
45 | | - id: 'helloworld', |
46 | | - version: '1.0', |
47 | | - specVersion: '0.7', |
48 | | - name: 'Hello World Workflow', |
49 | | - description: 'Inject Hello World', |
50 | | - start: 'Hello State', |
51 | | - states: [ |
52 | | - { |
53 | | - name: 'Hello State', |
54 | | - type: 'inject', |
55 | | - data: { |
56 | | - result: 'Hello World!', |
57 | | - }, |
58 | | - end: true, |
59 | | - }, |
60 | | - ], |
61 | | - } as Workflow; |
| 75 | + const workflow = validWorkflow as Workflow; |
62 | 76 |
|
63 | 77 | const workflowValidator = new WorkflowValidator(workflow); |
64 | 78 | expect(workflowValidator.errors.length).toBe(0); |
|
0 commit comments