Skip to content

Commit

Permalink
handle no schema request body
Browse files Browse the repository at this point in the history
Signed-off-by: Nik Nasr <[email protected]>

remove unused import

Signed-off-by: Nik Nasr <[email protected]>

fix

Signed-off-by: Nik Nasr <[email protected]>

trigger CI

update tests

Signed-off-by: Nik Nasr <[email protected]>
  • Loading branch information
nikrooz committed Jan 15, 2025
1 parent 3e326e7 commit 9ca3182
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
56 changes: 55 additions & 1 deletion packages/elements-core/src/components/TryIt/TryIt.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@testing-library/jest-dom';

import { Provider as MosaicProvider } from '@stoplight/mosaic';
import { HttpParamStyles, IHttpOperation } from '@stoplight/types';
import { HttpParamStyles, IHttpOperation, IMediaTypeContent } from '@stoplight/types';
import { cleanup, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import fetchMock from 'jest-fetch-mock';
Expand Down Expand Up @@ -609,6 +609,60 @@ describe('TryIt', () => {
expect(bodyHeader).not.toBeInTheDocument();
});

it('does not hide panel when request body has no schema but has content', async () => {
render(
<TryItWithPersistence
httpOperation={{
...requestBodyEmptySchema,
request: {
body: {
id: '?http-request-body?',
contents: [
{
id: '?http-media-0?',
mediaType: 'application/json',
},
],
},
},
method: 'POST',
}}
/>,
);

let bodyHeader = screen.queryByText('Body');
expect(bodyHeader).toBeInTheDocument();
});

it('send content-type header when request body has no schema but has content', async () => {
render(
<TryItWithPersistence
httpOperation={{
...requestBodyEmptySchema,
request: {
body: {
id: '?http-request-body?',
contents: [
{
id: '?http-media-0?',
mediaType: 'application/json',
},
],
},
},
method: 'POST',
}}
/>,
);

clickSend();
await waitFor(() => expect(fetchMock).toHaveBeenCalled());
const requestInit = fetchMock.mock.calls[0][1]!;
expect(requestInit.method).toMatch(/^post$/i);
const headers = new Headers(requestInit.headers);
expect(headers.get('Content-Type')).toBe('application/json');
});

it('statically generates request body basing on request body schema', () => {
render(<TryItWithPersistence httpOperation={requestBody} />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const generateExampleFromMediaTypeContent = (
) => {
const textRequestBodySchema = mediaTypeContent?.schema;
const textRequestBodyExamples = mediaTypeContent?.examples;
const textRequestBodyMediaType = mediaTypeContent?.mediaType;

try {
if (textRequestBodyExamples?.length) {
Expand All @@ -57,6 +58,9 @@ export const generateExampleFromMediaTypeContent = (
console.warn(e);
return `Example cannot be created for this schema\n${e}`;
}
if (textRequestBodyMediaType) {
return '';
}
return undefined;
};

Expand Down

0 comments on commit 9ca3182

Please sign in to comment.