From 9e1ea703e56a71567b13d0d22e2d69945a22de4d Mon Sep 17 00:00:00 2001 From: Valdir Mendes Date: Fri, 30 Jul 2021 05:18:13 -0300 Subject: [PATCH] fix: handle empty object in security array (#1678) --- .../SecurityRequirement.tsx | 34 +++++++++++-------- .../__tests__/SecurityRequirement.test.tsx | 27 +++++++++++++++ 2 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 src/components/__tests__/SecurityRequirement.test.tsx diff --git a/src/components/SecurityRequirement/SecurityRequirement.tsx b/src/components/SecurityRequirement/SecurityRequirement.tsx index 3a52ad810b..91bf9f434b 100644 --- a/src/components/SecurityRequirement/SecurityRequirement.tsx +++ b/src/components/SecurityRequirement/SecurityRequirement.tsx @@ -8,8 +8,8 @@ import { SecurityRequirementModel } from '../../services/models/SecurityRequirem import { linksCss } from '../Markdown/styled.elements'; const ScopeName = styled.code` - font-size: ${props => props.theme.typography.code.fontSize}; - font-family: ${props => props.theme.typography.code.fontFamily}; + font-size: ${(props) => props.theme.typography.code.fontSize}; + font-family: ${(props) => props.theme.typography.code.fontFamily}; border: 1px solid ${({ theme }) => theme.colors.border.dark}; margin: 0 3px; padding: 0.2em; @@ -67,18 +67,22 @@ export class SecurityRequirement extends React.PureComponent - {security.schemes.map(scheme => { - return ( - - {scheme.id} - {scheme.scopes.length > 0 && ' ('} - {scheme.scopes.map(scope => ( - {scope} - ))} - {scheme.scopes.length > 0 && ') '} - - ); - })} + {security.schemes.length ? ( + security.schemes.map((scheme) => { + return ( + + {scheme.id} + {scheme.scopes.length > 0 && ' ('} + {scheme.scopes.map((scope) => ( + {scope} + ))} + {scheme.scopes.length > 0 && ') '} + + ); + }) + ) : ( + None + )} ); } @@ -89,7 +93,7 @@ const AuthHeaderColumn = styled.div` `; const SecuritiesColumn = styled.div` - width: ${props => props.theme.schema.defaultDetailsWidth}; + width: ${(props) => props.theme.schema.defaultDetailsWidth}; ${media.lessThan('small')` margin-top: 10px; `} diff --git a/src/components/__tests__/SecurityRequirement.test.tsx b/src/components/__tests__/SecurityRequirement.test.tsx new file mode 100644 index 0000000000..723c050565 --- /dev/null +++ b/src/components/__tests__/SecurityRequirement.test.tsx @@ -0,0 +1,27 @@ +import * as React from 'react'; +import { shallow } from 'enzyme'; + +import { OpenAPIParser } from '../../services'; +import { SecurityRequirementModel } from '../../services/models/SecurityRequirement'; +import { SecurityRequirement } from '../SecurityRequirement/SecurityRequirement'; +import { RedocNormalizedOptions } from '../../services/RedocNormalizedOptions'; + +const options = new RedocNormalizedOptions({}); +describe('Components', () => { + describe('SecurityRequirement', () => { + describe('SecurityRequirement', () => { + it('should render \'None\' when empty object in security open api', () => { + const parser = new OpenAPIParser({ openapi: '3.0', info: { title: 'test', version: '0' }, paths: {} }, + undefined, + options, + ); + const securityRequirement = new SecurityRequirementModel({}, parser); + const securityElement = shallow( + + ).getElement(); + expect(securityElement.props.children.type.target).toEqual('span'); + expect(securityElement.props.children.props.children).toEqual('None'); + }); + }); + }); +});