-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathschemaParser.spec.ts
93 lines (84 loc) · 2.57 KB
/
schemaParser.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
import { TYPES } from '@vulcan/build/containers';
import { ISchemaParserOptions, SchemaReaderType } from '@vulcan/build/models';
import { SchemaParserOptions } from '@vulcan/build/options';
import {
SchemaFormat,
SchemaParser,
SchemaReader,
} from '@vulcan/build/schema-parser';
import { IValidatorLoader, TYPES as CORE_TYPES } from '@vulcan/core';
import { Container } from 'inversify';
import * as sinon from 'ts-sinon';
let container: Container;
let stubSchemaReader: sinon.StubbedInstance<SchemaReader>;
let stubValidatorLoader: sinon.StubbedInstance<IValidatorLoader>;
beforeEach(() => {
container = new Container();
stubSchemaReader = sinon.stubInterface<SchemaReader>();
stubValidatorLoader = sinon.stubInterface<IValidatorLoader>();
container
.bind(TYPES.Factory_SchemaReader)
.toConstantValue(() => stubSchemaReader);
container
.bind<Partial<ISchemaParserOptions>>(TYPES.SchemaParserInputOptions)
.toConstantValue({
folderPath: '',
reader: SchemaReaderType.LocalFile,
});
container
.bind(TYPES.SchemaParserOptions)
.to(SchemaParserOptions)
.inSingletonScope();
container
.bind(CORE_TYPES.ValidatorLoader)
.toConstantValue(stubValidatorLoader);
container.bind(TYPES.SchemaParser).to(SchemaParser).inSingletonScope();
});
afterEach(() => {
container.unbindAll();
});
it('Schema parser parse should return correct result', async () => {
// Assert
const generator = async function* () {
yield {
sourceName: 'detail/role',
content: `
request:
- fieldName: id
in: query
description: role id
validators:
- name: uuid
`,
type: SchemaFormat.YAML,
};
};
stubSchemaReader.readSchema.returns(generator());
stubValidatorLoader.load.resolves({
name: 'validator1',
validateSchema: () => null,
validateData: () => null,
});
const schemaParser = container.get<SchemaParser>(TYPES.SchemaParser);
// Act
const result = await schemaParser.parse();
// Assert
expect(result.schemas.length).toBe(1);
expect(result.schemas[0].request[0].description).toBe('role id');
});
it('Schema parser parse should throw with unsupported schema type', async () => {
// Assert
const generator = async function* () {
yield {
sourceName: 'detail/role',
content: ``,
type: 'unsupported' as SchemaFormat,
};
};
stubSchemaReader.readSchema.returns(generator());
const schemaParser = container.get<SchemaParser>(TYPES.SchemaParser);
// Act, Assert
await expect(schemaParser.parse()).rejects.toThrow(
`Unsupported schema type: unsupported`
);
});