From 7d1d9a3df3e5ee94dbb068c60cfb27c24257274c Mon Sep 17 00:00:00 2001 From: Oliver Sand Date: Mon, 30 Nov 2020 15:18:57 +0100 Subject: [PATCH] Add tests for components in the api-docs plugin --- .../ApiDefinitionCard.test.tsx | 121 ++++++++++++++++++ .../ApiDefinitionCard/ApiDefinitionCard.tsx | 41 +----- .../ApiDefinitionCard/ApiDefinitionWidget.tsx | 55 ++++++++ .../ApiDefinitionCard/ApiTypeTitle.test.tsx | 92 +++++++++++++ .../ApiDefinitionCard/ApiTypeTitle.tsx | 28 ++++ .../src/components/ApiDefinitionCard/index.ts | 9 +- .../ApiExplorerTable/ApiExplorerTable.tsx | 26 +--- .../AsyncApiDefinitionWidget.test.tsx | 59 +++++++++ .../components/EntityLink/EntityLink.test.tsx | 41 ++++++ .../src/components/EntityLink/EntityLink.tsx | 40 ++++++ .../src/components/EntityLink/index.ts | 17 +++ .../GraphQlDefinitionWidget.test.tsx | 61 +++++++++ .../OpenApiDefinitionWidget.test.tsx | 58 +++++++++ .../PlainApiDefinitionWidget.test.tsx | 28 ++++ 14 files changed, 609 insertions(+), 67 deletions(-) create mode 100644 plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionCard.test.tsx create mode 100644 plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionWidget.tsx create mode 100644 plugins/api-docs/src/components/ApiDefinitionCard/ApiTypeTitle.test.tsx create mode 100644 plugins/api-docs/src/components/ApiDefinitionCard/ApiTypeTitle.tsx create mode 100644 plugins/api-docs/src/components/AsyncApiDefinitionWidget/AsyncApiDefinitionWidget.test.tsx create mode 100644 plugins/api-docs/src/components/EntityLink/EntityLink.test.tsx create mode 100644 plugins/api-docs/src/components/EntityLink/EntityLink.tsx create mode 100644 plugins/api-docs/src/components/EntityLink/index.ts create mode 100644 plugins/api-docs/src/components/GraphQlDefinitionWidget/GraphQlDefinitionWidget.test.tsx create mode 100644 plugins/api-docs/src/components/OpenApiDefinitionWidget/OpenApiDefinitionWidget.test.tsx create mode 100644 plugins/api-docs/src/components/PlainApiDefinitionWidget/PlainApiDefinitionWidget.test.tsx diff --git a/plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionCard.test.tsx b/plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionCard.test.tsx new file mode 100644 index 0000000000..d59699ab9a --- /dev/null +++ b/plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionCard.test.tsx @@ -0,0 +1,121 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ApiEntity } from '@backstage/catalog-model'; +import { ApiProvider, ApiRegistry } from '@backstage/core'; +import { renderInTestApp } from '@backstage/test-utils'; +import { waitFor } from '@testing-library/react'; +import React from 'react'; +import { ApiDocsConfig, apiDocsConfigRef } from '../../config'; +import { OpenApiDefinitionWidget } from '../OpenApiDefinitionWidget'; +import { ApiDefinitionCard } from './ApiDefinitionCard'; + +describe('', () => { + const apiDocsConfig: jest.Mocked = { + getApiDefinitionWidget: jest.fn(), + } as any; + let Wrapper: React.ComponentType; + + beforeEach(() => { + const apis = ApiRegistry.with(apiDocsConfigRef, apiDocsConfig); + + Wrapper = ({ children }: { children?: React.ReactNode }) => ( + {children} + ); + }); + + afterEach(() => jest.resetAllMocks()); + + it('renders API', async () => { + const definition = ` +openapi: "3.0.0" +info: + version: 1.0.0 + title: Artist API + license: + name: MIT +servers: + - url: http://artist.spotify.net/v1 +paths: + /artists: + get: + summary: List all artists + responses: + "200": + description: Success + `; + const apiEntity: ApiEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'API', + metadata: { + name: 'my-name', + }, + spec: { + type: 'openapi', + lifecycle: '...', + owner: '...', + definition, + }, + }; + apiDocsConfig.getApiDefinitionWidget.mockReturnValue({ + type: 'openapi', + title: 'OpenAPI', + rawLanguage: 'yaml', + component: definition => ( + + ), + }); + + const { getByText } = await renderInTestApp( + + + , + ); + + await waitFor(() => { + expect(getByText(/my-name/i)).toBeInTheDocument(); + expect(getByText(/OpenAPI/)).toBeInTheDocument(); + expect(getByText(/Raw/i)).toBeInTheDocument(); + expect(getByText(/List all artists/i)).toBeInTheDocument(); + }); + }); + + it('fallback to plain view', async () => { + const apiEntity: ApiEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'API', + metadata: { + name: 'my-name', + }, + spec: { + type: 'custom-type', + lifecycle: '...', + owner: '...', + definition: 'Custom Definition', + }, + }; + + const { getByText } = await renderInTestApp( + + + , + ); + + expect(getByText(/my-name/i)).toBeInTheDocument(); + expect(getByText(/custom-type/i)).toBeInTheDocument(); + expect(getByText(/Custom Definition/i)).toBeInTheDocument(); + }); +}); diff --git a/plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionCard.tsx b/plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionCard.tsx index 49b8a69318..19370bfc18 100644 --- a/plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionCard.tsx +++ b/plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionCard.tsx @@ -15,50 +15,11 @@ */ import { ApiEntity } from '@backstage/catalog-model'; -import { CardTab, useApi, TabbedCard } from '@backstage/core'; +import { CardTab, TabbedCard, useApi } from '@backstage/core'; import { Alert } from '@material-ui/lab'; import React from 'react'; import { apiDocsConfigRef } from '../../config'; import { PlainApiDefinitionWidget } from '../PlainApiDefinitionWidget'; -import { OpenApiDefinitionWidget } from '../OpenApiDefinitionWidget'; -import { AsyncApiDefinitionWidget } from '../AsyncApiDefinitionWidget'; -import { GraphQlDefinitionWidget } from '../GraphQlDefinitionWidget'; - -export type ApiDefinitionWidget = { - type: string; - title: string; - component: (definition: string) => React.ReactElement; - rawLanguage?: string; -}; - -export function defaultDefinitionWidgets(): ApiDefinitionWidget[] { - return [ - { - type: 'openapi', - title: 'OpenAPI', - rawLanguage: 'yaml', - component: definition => ( - - ), - }, - { - type: 'asyncapi', - title: 'AsyncAPI', - rawLanguage: 'yaml', - component: definition => ( - - ), - }, - { - type: 'graphql', - title: 'GraphQL', - rawLanguage: 'graphql', - component: definition => ( - - ), - }, - ]; -} type Props = { apiEntity?: ApiEntity; diff --git a/plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionWidget.tsx b/plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionWidget.tsx new file mode 100644 index 0000000000..360404af40 --- /dev/null +++ b/plugins/api-docs/src/components/ApiDefinitionCard/ApiDefinitionWidget.tsx @@ -0,0 +1,55 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import React from 'react'; +import { AsyncApiDefinitionWidget } from '../AsyncApiDefinitionWidget'; +import { GraphQlDefinitionWidget } from '../GraphQlDefinitionWidget'; +import { OpenApiDefinitionWidget } from '../OpenApiDefinitionWidget'; + +export type ApiDefinitionWidget = { + type: string; + title: string; + component: (definition: string) => React.ReactElement; + rawLanguage?: string; +}; + +export function defaultDefinitionWidgets(): ApiDefinitionWidget[] { + return [ + { + type: 'openapi', + title: 'OpenAPI', + rawLanguage: 'yaml', + component: definition => ( + + ), + }, + { + type: 'asyncapi', + title: 'AsyncAPI', + rawLanguage: 'yaml', + component: definition => ( + + ), + }, + { + type: 'graphql', + title: 'GraphQL', + rawLanguage: 'graphql', + component: definition => ( + + ), + }, + ]; +} diff --git a/plugins/api-docs/src/components/ApiDefinitionCard/ApiTypeTitle.test.tsx b/plugins/api-docs/src/components/ApiDefinitionCard/ApiTypeTitle.test.tsx new file mode 100644 index 0000000000..4f1b2d2f3b --- /dev/null +++ b/plugins/api-docs/src/components/ApiDefinitionCard/ApiTypeTitle.test.tsx @@ -0,0 +1,92 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ApiEntity } from '@backstage/catalog-model'; +import { ApiProvider, ApiRegistry } from '@backstage/core'; +import { renderInTestApp } from '@backstage/test-utils'; +import React from 'react'; +import { ApiDocsConfig, apiDocsConfigRef } from '../../config'; +import { ApiTypeTitle } from './ApiTypeTitle'; + +describe('', () => { + const apiDocsConfig: jest.Mocked = { + getApiDefinitionWidget: jest.fn(), + } as any; + let Wrapper: React.ComponentType; + + beforeEach(() => { + const apis = ApiRegistry.with(apiDocsConfigRef, apiDocsConfig); + + Wrapper = ({ children }: { children?: React.ReactNode }) => ( + {children} + ); + }); + + afterEach(() => jest.resetAllMocks()); + + it('renders API type title', async () => { + const apiEntity: ApiEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'API', + metadata: { + name: 'my-name', + }, + spec: { + type: 'openapi', + lifecycle: '...', + owner: '...', + definition: '...', + }, + }; + apiDocsConfig.getApiDefinitionWidget.mockReturnValue({ + type: 'openapi', + title: 'OpenAPI', + component: () =>
, + }); + + const { getByText } = await renderInTestApp( + + + , + ); + + expect(getByText(/OpenAPI/)).toBeInTheDocument(); + }); + + it('fallback if title is unknown', async () => { + const apiEntity: ApiEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'API', + metadata: { + name: 'my-name', + }, + spec: { + type: 'custom-type', + lifecycle: '...', + owner: '...', + definition: '...', + }, + }; + + const { getByText } = await renderInTestApp( + + + , + ); + + expect(getByText(/custom-type/i)).toBeInTheDocument(); + }); +}); diff --git a/plugins/api-docs/src/components/ApiDefinitionCard/ApiTypeTitle.tsx b/plugins/api-docs/src/components/ApiDefinitionCard/ApiTypeTitle.tsx new file mode 100644 index 0000000000..be6212edf7 --- /dev/null +++ b/plugins/api-docs/src/components/ApiDefinitionCard/ApiTypeTitle.tsx @@ -0,0 +1,28 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ApiEntity } from '@backstage/catalog-model'; +import { useApi } from '@backstage/core'; +import React from 'react'; +import { apiDocsConfigRef } from '../../config'; + +export const ApiTypeTitle = ({ apiEntity }: { apiEntity: ApiEntity }) => { + const config = useApi(apiDocsConfigRef); + const definition = config.getApiDefinitionWidget(apiEntity); + const type = definition ? definition.title : apiEntity.spec.type; + + return {type}; +}; diff --git a/plugins/api-docs/src/components/ApiDefinitionCard/index.ts b/plugins/api-docs/src/components/ApiDefinitionCard/index.ts index de7856f70c..9875385402 100644 --- a/plugins/api-docs/src/components/ApiDefinitionCard/index.ts +++ b/plugins/api-docs/src/components/ApiDefinitionCard/index.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -export type { ApiDefinitionWidget } from './ApiDefinitionCard'; -export { - ApiDefinitionCard, - defaultDefinitionWidgets, -} from './ApiDefinitionCard'; +export { ApiDefinitionCard } from './ApiDefinitionCard'; +export { defaultDefinitionWidgets } from './ApiDefinitionWidget'; +export type { ApiDefinitionWidget } from './ApiDefinitionWidget'; +export { ApiTypeTitle } from './ApiTypeTitle'; diff --git a/plugins/api-docs/src/components/ApiExplorerTable/ApiExplorerTable.tsx b/plugins/api-docs/src/components/ApiExplorerTable/ApiExplorerTable.tsx index f4a66f02f2..149f7470c3 100644 --- a/plugins/api-docs/src/components/ApiExplorerTable/ApiExplorerTable.tsx +++ b/plugins/api-docs/src/components/ApiExplorerTable/ApiExplorerTable.tsx @@ -20,23 +20,13 @@ import { TableColumn, TableFilter, TableState, - useApi, useQueryParamState, } from '@backstage/core'; -import { entityRoute, entityRouteParams } from '@backstage/plugin-catalog'; -import { Chip, Link } from '@material-ui/core'; +import { Chip } from '@material-ui/core'; import { Alert } from '@material-ui/lab'; import React from 'react'; -import { generatePath, Link as RouterLink } from 'react-router-dom'; -import { apiDocsConfigRef } from '../../config'; - -const ApiTypeTitle = ({ apiEntity }: { apiEntity: ApiEntityV1alpha1 }) => { - const config = useApi(apiDocsConfigRef); - const definition = config.getApiDefinitionWidget(apiEntity); - const type = definition ? definition.title : apiEntity.spec.type; - - return {type}; -}; +import { ApiTypeTitle } from '../ApiDefinitionCard'; +import { EntityLink } from '../EntityLink'; const columns: TableColumn[] = [ { @@ -44,15 +34,7 @@ const columns: TableColumn[] = [ field: 'metadata.name', highlight: true, render: (entity: any) => ( - - {entity.metadata.name} - + {entity.metadata.name} ), }, { diff --git a/plugins/api-docs/src/components/AsyncApiDefinitionWidget/AsyncApiDefinitionWidget.test.tsx b/plugins/api-docs/src/components/AsyncApiDefinitionWidget/AsyncApiDefinitionWidget.test.tsx new file mode 100644 index 0000000000..4baa21d3cf --- /dev/null +++ b/plugins/api-docs/src/components/AsyncApiDefinitionWidget/AsyncApiDefinitionWidget.test.tsx @@ -0,0 +1,59 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { renderInTestApp } from '@backstage/test-utils'; +import React from 'react'; +import { AsyncApiDefinitionWidget } from './AsyncApiDefinitionWidget'; + +describe('', () => { + it('renders asyncapi spec', async () => { + const definition = ` +asyncapi: 2.0.0 +info: + title: Account Service + version: 1.0.0 +channels: + user/signedup: + subscribe: + message: + $ref: '#/components/messages/UserSignedUp' +components: + messages: + UserSignedUp: + payload: + type: object + properties: + displayName: + type: string + `; + const { getByText, getAllByText } = await renderInTestApp( + , + ); + + expect(getByText(/Account Service/i)).toBeInTheDocument(); + expect(getByText(/user\/signedup/i)).toBeInTheDocument(); + expect(getByText(/UserSignedUp/i)).toBeInTheDocument(); + expect(getAllByText(/displayName/i)).toHaveLength(4); + }); + + it('renders error if definition is missing', async () => { + const { getByText } = await renderInTestApp( + , + ); + expect(getByText(/Error/i)).toBeInTheDocument(); + expect(getByText(/Document can't be null or falsey/i)).toBeInTheDocument(); + }); +}); diff --git a/plugins/api-docs/src/components/EntityLink/EntityLink.test.tsx b/plugins/api-docs/src/components/EntityLink/EntityLink.test.tsx new file mode 100644 index 0000000000..9221b96604 --- /dev/null +++ b/plugins/api-docs/src/components/EntityLink/EntityLink.test.tsx @@ -0,0 +1,41 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Entity } from '@backstage/catalog-model'; +import { renderInTestApp } from '@backstage/test-utils'; +import React from 'react'; +import { EntityLink } from './EntityLink'; + +describe('', () => { + it('provides link to entity page', async () => { + const entity: Entity = { + apiVersion: 'v1', + kind: 'Component', + metadata: { + name: 'my-name', + namespace: 'my-namespace', + }, + }; + const { getByText } = await renderInTestApp( + inner, + ); + expect(getByText(/inner/i)).toBeInTheDocument(); + expect(getByText(/inner/i)).toHaveAttribute( + 'href', + '/catalog/my-namespace/component/my-name', + ); + }); +}); diff --git a/plugins/api-docs/src/components/EntityLink/EntityLink.tsx b/plugins/api-docs/src/components/EntityLink/EntityLink.tsx new file mode 100644 index 0000000000..98b2103882 --- /dev/null +++ b/plugins/api-docs/src/components/EntityLink/EntityLink.tsx @@ -0,0 +1,40 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Entity } from '@backstage/catalog-model'; +import { entityRoute, entityRouteParams } from '@backstage/plugin-catalog'; +import { Link } from '@material-ui/core'; +import React, { PropsWithChildren } from 'react'; +import { generatePath, Link as RouterLink } from 'react-router-dom'; + +type Props = { + entity: Entity; +}; + +// TODO: Could be useful for others too, as part of the catalog plugin +export const EntityLink = ({ entity, children }: PropsWithChildren) => { + return ( + + {children} + + ); +}; diff --git a/plugins/api-docs/src/components/EntityLink/index.ts b/plugins/api-docs/src/components/EntityLink/index.ts new file mode 100644 index 0000000000..44875e4634 --- /dev/null +++ b/plugins/api-docs/src/components/EntityLink/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export { EntityLink } from './EntityLink'; diff --git a/plugins/api-docs/src/components/GraphQlDefinitionWidget/GraphQlDefinitionWidget.test.tsx b/plugins/api-docs/src/components/GraphQlDefinitionWidget/GraphQlDefinitionWidget.test.tsx new file mode 100644 index 0000000000..58ac611430 --- /dev/null +++ b/plugins/api-docs/src/components/GraphQlDefinitionWidget/GraphQlDefinitionWidget.test.tsx @@ -0,0 +1,61 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { renderInTestApp } from '@backstage/test-utils'; +import React from 'react'; +import { GraphQlDefinitionWidget } from './GraphQlDefinitionWidget'; + +describe('', () => { + it('renders graphql schema', async () => { + const definition = ` +"""Hello World!""" +schema { + query: Film +} + +"""A single film.""" +type Film { + """The ID of an object""" + id: ID! + """The title of this film.""" + title: String +} + `; + // CodeMirror uses features that jsdom doesn't support so we have to patch + // it here, see: https://github.com/jsdom/jsdom/issues/3002 + document.createRange = () => { + const range = new Range(); + + range.getBoundingClientRect = jest.fn(); + + range.getClientRects = () => { + return { + item: () => null, + length: 0, + [Symbol.iterator]: jest.fn(), + }; + }; + + return range; + }; + + const { getByText } = await renderInTestApp( + , + ); + + expect(getByText(/Film/i)).toBeInTheDocument(); + }); +}); diff --git a/plugins/api-docs/src/components/OpenApiDefinitionWidget/OpenApiDefinitionWidget.test.tsx b/plugins/api-docs/src/components/OpenApiDefinitionWidget/OpenApiDefinitionWidget.test.tsx new file mode 100644 index 0000000000..518b2a593e --- /dev/null +++ b/plugins/api-docs/src/components/OpenApiDefinitionWidget/OpenApiDefinitionWidget.test.tsx @@ -0,0 +1,58 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { renderInTestApp } from '@backstage/test-utils'; +import { waitFor } from '@testing-library/react'; +import React from 'react'; +import { OpenApiDefinitionWidget } from './OpenApiDefinitionWidget'; + +describe('', () => { + it('renders openapi spec', async () => { + const definition = ` +openapi: "3.0.0" +info: + version: 1.0.0 + title: Artist API + license: + name: MIT +servers: + - url: http://artist.spotify.net/v1 +paths: + /artists: + get: + summary: List all artists + responses: + "200": + description: Success + `; + const { getByText } = await renderInTestApp( + , + ); + + // swagger-ui loads the documentation asynchronously + await waitFor(() => { + expect(getByText(/\/artists/i)).toBeInTheDocument(); + expect(getByText(/List all artists/i)).toBeInTheDocument(); + }); + }); + + it('renders error if definition is missing', async () => { + const { getByText } = await renderInTestApp( + , + ); + expect(getByText(/No API definition provided/i)).toBeInTheDocument(); + }); +}); diff --git a/plugins/api-docs/src/components/PlainApiDefinitionWidget/PlainApiDefinitionWidget.test.tsx b/plugins/api-docs/src/components/PlainApiDefinitionWidget/PlainApiDefinitionWidget.test.tsx new file mode 100644 index 0000000000..cb3aab9526 --- /dev/null +++ b/plugins/api-docs/src/components/PlainApiDefinitionWidget/PlainApiDefinitionWidget.test.tsx @@ -0,0 +1,28 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { renderInTestApp } from '@backstage/test-utils'; +import React from 'react'; +import { PlainApiDefinitionWidget } from './PlainApiDefinitionWidget'; + +describe('', () => { + it('renders plain text', async () => { + const { getByText } = await renderInTestApp( + , + ); + expect(getByText(/Hello World/i)).toBeInTheDocument(); + }); +});