Skip to content

Commit

Permalink
chore: fix broken test & components refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Oprysk committed Dec 14, 2021
1 parent a42d29d commit 6c41e95
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 69 deletions.
71 changes: 39 additions & 32 deletions src/components/Responses/Response.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
import { observer } from 'mobx-react';
import * as React from 'react';
import { observer } from 'mobx-react';

import { ResponseModel } from '../../services/models';
import type { ResponseModel, MediaTypeModel } from '../../services/models';
import { ResponseDetails } from './ResponseDetails';
import { ResponseDetailsWrap, StyledResponseTitle } from './styled.elements';

@observer
export class ResponseView extends React.Component<{ response: ResponseModel }> {
toggle = () => {
this.props.response.toggle();
};
export interface ResponseViewProps {
response: ResponseModel;
}

render() {
const { extensions, headers, type, summary, description, code, expanded, content } = this.props.response;
const mimes =
content === undefined ? [] : content.mediaTypes.filter(mime => mime.schema !== undefined);
export const ResponseView = observer(({ response }: ResponseViewProps): React.ReactElement => {
const { extensions, headers, type, summary, description, code, expanded, content } = response;

const empty = (!extensions || Object.keys(extensions).length === 0) &&
headers.length === 0 && mimes.length === 0 && !description;
const mimes = React.useMemo<MediaTypeModel[]>(
() =>
content === undefined ? [] : content.mediaTypes.filter(mime => mime.schema !== undefined),
[content],
);

return (
<div>
<StyledResponseTitle
onClick={this.toggle}
type={type}
empty={empty}
title={summary || ''}
code={code}
opened={expanded}
/>
{expanded && !empty && (
<ResponseDetailsWrap>
<ResponseDetails response={this.props.response} />
</ResponseDetailsWrap>
)}
</div>
);
}
}
const empty = React.useMemo<boolean>(
() =>
(!extensions || Object.keys(extensions).length === 0) &&
headers.length === 0 &&
mimes.length === 0 &&
!description,
[extensions, headers, mimes, description],
);

return (
<div>
<StyledResponseTitle
onClick={response.toggle}
type={type}
empty={empty}
title={summary || ''}
code={code}
opened={expanded}
/>
{expanded && !empty && (
<ResponseDetailsWrap>
<ResponseDetails response={response} />
</ResponseDetailsWrap>
)}
</div>
);
});
53 changes: 30 additions & 23 deletions src/components/Responses/ResponseTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,34 @@ export interface ResponseTitleProps {
onClick?: () => void;
}

export class ResponseTitle extends React.PureComponent<ResponseTitleProps> {
render() {
const { title, type, empty, code, opened, className, onClick } = this.props;
return (
<button
className={className}
onClick={(!empty && onClick) || undefined}
aria-expanded={opened}
disabled={empty}
>
{!empty && (
<ShelfIcon
size={'1.5em'}
color={type}
direction={opened ? 'down' : 'right'}
float={'left'}
/>
)}
<Code>{code} </Code>
<Markdown compact={true} inline={true} source={title} />
</button>
);
}
function ResponseTitleComponent({
title,
type,
empty,
code,
opened,
className,
onClick,
}: ResponseTitleProps): React.ReactElement {
return (
<button
className={className}
onClick={(!empty && onClick) || undefined}
aria-expanded={opened}
disabled={empty}
>
{!empty && (
<ShelfIcon
size={'1.5em'}
color={type}
direction={opened ? 'down' : 'right'}
float={'left'}
/>
)}
<Code>{code} </Code>
<Markdown compact={true} inline={true} source={title} />
</button>
);
}

export const ResponseTitle = React.memo<ResponseTitleProps>(ResponseTitleComponent);
4 changes: 1 addition & 3 deletions src/components/Responses/styled.elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ export const StyledResponseTitle = styled(ResponseTitle)`
border-radius: 2px;
margin-bottom: 4px;
line-height: 1.5em;
background-color: #f2f2f2;
cursor: pointer;
color: ${props => props.theme.colors.responses[props.type].color};
background-color: ${props => props.theme.colors.responses[props.type].backgroundColor};
&:focus {
outline: auto;
outline-color: ${props => props.theme.colors.responses[props.type].color};
outline: auto ${props => props.theme.colors.responses[props.type].color};
}
${props =>
(props.empty &&
Expand Down
11 changes: 9 additions & 2 deletions src/services/__tests__/models/Response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ describe('Models', () => {

test('ensure extensions are shown if showExtensions is true', () => {
const options = new RedocNormalizedOptions({ showExtensions: true });
const resp = new ResponseModel(parser, 'default', true, { 'x-example': {a: 1} } as any, options);
const resp = new ResponseModel({
parser,
code: 'default',
defaultAsError: true,
infoOrRef: { 'x-example': { a: 1 } },
options,
isEvent: true,
});
expect(Object.keys(resp.extensions).length).toEqual(1);
expect(resp.extensions['x-example']).toEqual({a: 1});
expect(resp.extensions['x-example']).toEqual({ a: 1 });
});
});
});
3 changes: 1 addition & 2 deletions src/services/models/RequestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export class RequestBodyModel {
required: boolean;
content?: MediaContentModel;

constructor(props: RequestBodyProps) {
const { parser, infoOrRef, options, isEvent } = props;
constructor({ parser, infoOrRef, options, isEvent }: RequestBodyProps) {
const isRequest = !isEvent;
const info = parser.deref(infoOrRef);
this.description = info.description || '';
Expand Down
16 changes: 9 additions & 7 deletions src/services/models/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { action, observable, makeObservable } from 'mobx';

import { OpenAPIResponse, Referenced } from '../../types';

import {
getStatusCodeType,
extractExtensions
} from '../../utils';
import { getStatusCodeType, extractExtensions } from '../../utils';
import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
import { FieldModel } from './Field';
Expand All @@ -32,9 +29,14 @@ export class ResponseModel {
headers: FieldModel[] = [];
extensions: Record<string, any>;

constructor(props: ResponseProps) {
const { parser, code, defaultAsError, infoOrRef, options, isEvent } = props;
const isRequest = isEvent ? true : false;
constructor({
parser,
code,
defaultAsError,
infoOrRef,
options,
isEvent: isRequest,
}: ResponseProps) {
makeObservable(this);

this.expanded = options.expandResponses === 'all' || options.expandResponses[code];
Expand Down

0 comments on commit 6c41e95

Please sign in to comment.