UNPKG

3.62 kBJavaScriptView Raw
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12/* eslint-env mocha */
13
14const path = require('path');
15const fs = require('fs-extra');
16const assert = require('assert');
17const { loader } = require('../lib/schemaProxy');
18const { assertMarkdown } = require('./testUtils');
19const readme = require('../lib/readmeBuilder');
20const markdown = require('../lib/markdownBuilder');
21const traverse = require('../lib/traverseSchema');
22const { writeSchema } = require('../lib/writeSchema');
23
24describe('Integration Test: Cyclic References', () => {
25 let one;
26 let two;
27 let three;
28
29 let proxiedone;
30 let proxiedtwo;
31 let proxiedthree;
32
33 let allschemas;
34
35 beforeEach('Read Schemas from disk', async () => {
36 console.log('Reading Schemas');
37 one = await fs.readJson(path.resolve(__dirname, 'fixtures', 'cyclic', 'one.schema.json'));
38 two = await fs.readJson(path.resolve(__dirname, 'fixtures', 'cyclic', 'two.schema.json'));
39 three = await fs.readJson(path.resolve(__dirname, 'fixtures', 'cyclic', 'three.schema.json'));
40
41 const myloader = loader();
42
43 console.log('Loading Schemas');
44 proxiedone = myloader(one, path.resolve(__dirname, 'fixtures', 'cyclic', 'one.schema.json'));
45 proxiedtwo = myloader(two, path.resolve(__dirname, 'fixtures', 'cyclic', 'two.schema.json'));
46 proxiedthree = myloader(three, path.resolve(__dirname, 'fixtures', 'cyclic', 'three.schema.json'));
47
48 console.log('Traversing Schemas');
49
50 allschemas = traverse([proxiedone, proxiedtwo, proxiedthree]);
51 });
52
53 it('Schemas with cyclic references generate README', () => {
54 const builder = readme({ readme: true });
55 const result = builder([proxiedone, proxiedtwo, proxiedthree]);
56
57 assertMarkdown(result)
58 .contains('http://example.com/schemas/one')
59 .contains('http://example.com/schemas/two')
60 .contains('http://example.com/schemas/three')
61 .contains('http://json-schema.org/draft-04/schema#');
62 });
63
64 it('Schemas with cyclic references generate Markdown', () => {
65 const builder = markdown();
66 const documents = builder(allschemas);
67 assertMarkdown(documents['one-properties-children-items'])
68 .contains('any of');
69
70 assertMarkdown(documents.one)
71 .contains('one-properties-children-items');
72 });
73
74 it('Schemas with cyclic references get written to disk', () => {
75 const writer = writeSchema({ origindir: path.resolve(__dirname, 'fixtures', 'cyclic'), schemadir: path.resolve(__dirname, 'fixtures', 'cyclic-out') });
76 writer(allschemas);
77 const schemaone = fs.readJsonSync(path.resolve(__dirname, 'fixtures', 'cyclic-out', 'one.schema.json'));
78 assert.deepStrictEqual(schemaone, {
79 $schema: 'http://json-schema.org/draft-04/schema#',
80 $id: 'http://example.com/schemas/one',
81 type: 'object',
82 description: 'The first schema in the cycle (or is it the last?)',
83 examples: [{}],
84 properties: { children: { type: 'array', items: { anyOf: [{ $ref: 'http://example.com/schemas/three' }, { $ref: 'http://example.com/schemas/two' }] } } },
85 });
86 });
87});