Skip to content

Commit

Permalink
fix pre-rendering spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Sep 13, 2023
1 parent 80713c8 commit 3e61b15
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
13 changes: 10 additions & 3 deletions helpers/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
import ReactDOMServer from 'react-dom/server';
import fetch from 'sync-fetch';
import AsyncApiComponent, { hljs } from '@asyncapi/react-component';
import {stringify} from '@asyncapi/parser';
import {stringify, AsyncAPIDocumentInterface} from '@asyncapi/parser';

function isJsonObject(o) {
return o && typeof o === 'object' && !Array.isArray(o);
Expand Down Expand Up @@ -33,7 +33,7 @@ function mergeInto(from, to) {
* Prepares configuration for component.
*/
export function prepareConfiguration(params = {}) {
const config = { show: { sidebar: true }, sidebar: { showOperations: 'byDefault' } };
const config = { show: { sidebar: false }, sidebar: { showOperations: 'byDefault' } };
// Apply config override
if (params.config) {
let configOverride;
Expand Down Expand Up @@ -146,10 +146,17 @@ export function stringifyConfiguration(params) {
/**
* Renders AsyncApi component by given AsyncAPI spec and with corresponding template configuration.
*/


/**
* @param {AsyncAPIDocumentInterface} asyncapi
* @param {*} params
*/
export function renderSpec(asyncapi, params) {
loadLanguagesConfig();
const config = prepareConfiguration(params);
const component = <AsyncApiComponent schema={asyncapi} config={config}/>;
const stringified = stringify(asyncapi);
const component = <AsyncApiComponent schema={stringified} config={config}/>;
//const component = <></>
return ReactDOMServer.renderToString(component);
}
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"@asyncapi/generator-react-sdk": "^1.0.0",
"@asyncapi/react-component": "^1.0.0-next.53",
"@asyncapi/parser": "^3.0.0-next-major-spec.2",
"highlight.js": "10.7.3",
"puppeteer": "^14.1.0",
"react-dom": "^17.0.2",
Expand All @@ -43,7 +44,6 @@
},
"devDependencies": {
"@asyncapi/generator": "asyncapi/generator#add-v3-support",
"@asyncapi/parser": "^3.0.0-next-major-spec.2",
"cross-env": "^7.0.3",
"jest": "^26.6.3",
"jest-esm-transformer": "^1.0.0",
Expand Down
6 changes: 3 additions & 3 deletions template/js/asyncapi-ui.min.js

Large diffs are not rendered by default.

24 changes: 10 additions & 14 deletions test/helpers/all.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hljs } from '@asyncapi/react-component';
import { AsyncAPIDocumentV2, createAsyncAPIDocument } from "@asyncapi/parser";
import Parser, { AsyncAPIDocumentV2, createAsyncAPIDocument } from "@asyncapi/parser";

import {
includeFile,
Expand Down Expand Up @@ -55,14 +55,8 @@ describe('Helpers', () => {
'x-parser-circular-props': ['bar'],
}
schema.properties.bar = schema;
const doc = createAsyncAPIDocument({
semver: {
major: 2,
minor: 0,
patch: 0,
},
parsed: { asyncapi: '2.0.0', components: { schemas: { dummySchema: schema } }},
});

const doc = new AsyncAPIDocumentV2({ asyncapi: '2.0.0', components: { schemas: { dummySchema: schema } }});

const expected = `{\n \"asyncapi\": \"2.0.0\",\n \"components\": {\n \"schemas\": {\n \"dummySchema\": {\n \"type\": \"object\",\n \"properties\": {\n \"foo\": {\n \"type\": \"string\"\n },\n \"bar\": \"$ref:$.components.schemas.dummySchema\"\n },\n \"x-parser-schema-id\": \"parsedDoc\",\n \"x-parser-circular-props\": [\n \"bar\"\n ]\n }\n }\n },\n \"x-parser-spec-stringified\": true\n}`;
const expectedParsed = {
Expand Down Expand Up @@ -137,13 +131,15 @@ describe('Helpers', () => {
});

describe('.renderSpec', () => {
it.skip('should work', async () => {
const schema = new AsyncAPIDocumentV2({
it('should work', async () => {
const parser = new Parser();
const {document} = await parser.parse({
asyncapi: '2.0.0',
info: { title: 'test', version: '0.0.0' },
info: { title: 'dummy spec', version: '1.5.34' },
channels: {},
});
const result = renderSpec(schema);
})

const result = renderSpec(document);
// check if '1.5.34' version is rendered
expect(result.includes('1.5.34')).toEqual(true);
// check if 'dummy spec for testing' title is rendered
Expand Down

0 comments on commit 3e61b15

Please sign in to comment.