forked from horiuchi/dtsgenerator
/
intersection_test.ts
89 lines (78 loc) · 2.25 KB
/
intersection_test.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
import * as assert from 'power-assert';
import { clear } from '../src/commandOptions';
import opts from '../src/commandOptions';
import dtsgenerator from '../src/core';
describe('intersection test', () => {
beforeEach(() => {
opts.intersection = true;
opts.allDefs = true;
});
afterEach(() => {
clear();
});
it('should combine allOf and anyOf using intersection', async () => {
const schema: JsonSchemaOrg.Draft04.Schema = {
id: 'http://test/intersection',
allOf: [
{ anyOf: [
{ type: 'object', required: ['a'], properties: { a: { type: 'string' } } },
{ type: 'object', required: ['b'], properties: { b: { enum: ['one', 'two'] } } },
]},
{
required: ['c'],
type: 'object',
properties: {
c: { type: 'number' },
},
},
],
};
const result = await dtsgenerator({ contents: [schema] });
const expected = `declare namespace Test {
export type Intersection = ({
a: string;
} | {
b: "one" | "two";
}) & {
c: number;
};
}
`;
assert.equal(result, expected, result);
});
it('two namespace test', async () => {
const schema1: JsonSchemaOrg.Draft04.Schema = {
id: `my_ns`,
definitions: {
A: {
type: 'array',
items: {
$ref: `my_other_ns#/definitions/B`,
},
},
},
};
const schema2: JsonSchemaOrg.Draft04.Schema = {
id: `my_other_ns`,
definitions: {
B: { type: 'string' },
},
};
const result = await dtsgenerator({ contents: [schema1, schema2] });
const x =
`declare type MyNs = any;
declare namespace MyNs {
namespace Definitions {
export type A = MyOtherNs.Definitions.B[];
}
}
declare type MyOtherNs = any;
declare namespace MyOtherNs {
namespace Definitions {
export type B = string;
}
}
`;
assert.equal(result, x, result);
});
});