|
1 | 1 | import {
|
2 | 2 | InterfaceDeclaration,
|
| 3 | + TypeAliasDeclaration, |
3 | 4 | TypescriptParser
|
4 | 5 | } from 'typescript-parser';
|
5 | 6 | import { NgOpenApiGen } from '../lib/ng-openapi-gen';
|
6 | 7 | import options from './self-ref.config.json';
|
7 | 8 | import selfRef from './self-ref.json';
|
8 | 9 |
|
9 |
| -const gen = new NgOpenApiGen(selfRef, options); |
10 |
| -gen.generate(); |
11 |
| - |
12 |
| -describe('Generation tests using self-ref.json', () => { |
13 |
| - it('Baz model', done => { |
14 |
| - const baz = gen.models.get('Foo.Bar.Baz'); |
15 |
| - const ts = gen.templates.apply('model', baz); |
16 |
| - |
17 |
| - const parser = new TypescriptParser(); |
18 |
| - parser.parseSource(ts).then(ast => { |
19 |
| - expect(ast.declarations.length).toBe(1); |
20 |
| - expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration)); |
21 |
| - const decl = ast.declarations[0] as InterfaceDeclaration; |
22 |
| - expect(decl.name).toBe('Baz'); |
23 |
| - expect(decl.properties.length).toBe(3); |
24 |
| - |
25 |
| - const ref = decl.properties.find(p => p.name === 'refProperty'); |
26 |
| - expect(ref).withContext('refProperty property').toBeDefined(); |
27 |
| - if (ref) { |
28 |
| - expect(ref.type).toBe('Baz'); |
29 |
| - } |
30 |
| - |
31 |
| - const array = decl.properties.find(p => p.name === 'arrayProperty'); |
32 |
| - expect(array).withContext('arrayProperty property').toBeDefined(); |
33 |
| - if (array) { |
34 |
| - expect(array.type).toBe('Array<Baz>'); |
35 |
| - } |
36 |
| - |
37 |
| - const object = decl.properties.find(p => p.name === 'objectProperty'); |
38 |
| - expect(object).withContext('objectProperty property').toBeDefined(); |
39 |
| - if (object) { |
40 |
| - expect(object.type).toBe('{\n\'nestedArray\': Array<Baz>;\n\'nestedRef\': Baz;\n}'); |
41 |
| - } |
42 |
| - |
43 |
| - done(); |
| 10 | +import optionsAllof from './self-ref-allof.config.json'; |
| 11 | +import selfRefAllof from './self-ref-allof.json'; |
| 12 | + |
| 13 | +describe('Test self referencing', () => { |
| 14 | + describe('Generation tests using self-ref.json', () => { |
| 15 | + const gen = new NgOpenApiGen(selfRef, options); |
| 16 | + gen.generate(); |
| 17 | + it('Baz model', done => { |
| 18 | + const baz = gen.models.get('Foo.Bar.Baz'); |
| 19 | + const ts = gen.templates.apply('model', baz); |
| 20 | + |
| 21 | + const parser = new TypescriptParser(); |
| 22 | + parser.parseSource(ts).then(ast => { |
| 23 | + expect(ast.declarations.length).toBe(1); |
| 24 | + expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration)); |
| 25 | + const decl = ast.declarations[0] as InterfaceDeclaration; |
| 26 | + expect(decl.name).toBe('Baz'); |
| 27 | + expect(decl.properties.length).toBe(3); |
| 28 | + |
| 29 | + const ref = decl.properties.find(p => p.name === 'refProperty'); |
| 30 | + expect(ref).withContext('refProperty property').toBeDefined(); |
| 31 | + if (ref) { |
| 32 | + expect(ref.type).toBe('Baz'); |
| 33 | + } |
| 34 | + |
| 35 | + const array = decl.properties.find(p => p.name === 'arrayProperty'); |
| 36 | + expect(array).withContext('arrayProperty property').toBeDefined(); |
| 37 | + if (array) { |
| 38 | + expect(array.type).toBe('Array<Baz>'); |
| 39 | + } |
| 40 | + |
| 41 | + const object = decl.properties.find(p => p.name === 'objectProperty'); |
| 42 | + expect(object).withContext('objectProperty property').toBeDefined(); |
| 43 | + if (object) { |
| 44 | + expect(object.type).toBe('{\n\'nestedArray\': Array<Baz>;\n\'nestedRef\': Baz;\n}'); |
| 45 | + } |
| 46 | + |
| 47 | + done(); |
| 48 | + }); |
44 | 49 | });
|
| 50 | + |
45 | 51 | });
|
46 | 52 |
|
| 53 | + describe('Generation tests using self-ref-allof.json', () => { |
| 54 | + const gen = new NgOpenApiGen(selfRefAllof, optionsAllof); |
| 55 | + gen.generate(); |
| 56 | + it('Baz model', done => { |
| 57 | + const baz = gen.models.get('Foo.Bar.Baz'); |
| 58 | + const ts = gen.templates.apply('model', baz); |
| 59 | + |
| 60 | + const parser = new TypescriptParser(); |
| 61 | + parser.parseSource(ts).then(ast => { |
| 62 | + expect(ast.declarations.length).toBe(1); |
| 63 | + expect(ast.declarations[0]).toEqual(jasmine.any(TypeAliasDeclaration)); |
| 64 | + const decl = ast.declarations[0] as TypeAliasDeclaration; |
| 65 | + expect(decl.name).toBe('Baz'); |
| 66 | + |
| 67 | + const text = ts.substring(decl.start || 0, decl.end || ts.length); |
| 68 | + |
| 69 | + expect(text).toContain('\'refProperty\'?: Baz;'); |
| 70 | + expect(text).toContain('\'arrayProperty\': Array<Baz>;'); |
| 71 | + expect(text).toContain('\'nestedArray\': Array<Baz>;'); |
| 72 | + expect(text).toContain('\'nestedRef\': Baz;'); |
| 73 | + |
| 74 | + done(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
47 | 78 | });
|
0 commit comments