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]>

remove import

Signed-off-by: Nik Nasr <[email protected]>
  • Loading branch information
nikrooz committed Jan 15, 2025
1 parent 3e326e7 commit b4b1615
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/elements-core/src/components/TryIt/TryIt.spec.tsx
Original file line number Diff line number Diff line change
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 b4b1615

Please sign in to comment.