Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit a420ee7

Browse files
Merge pull request #278 from utkarsha-deriv/utkarsha/API-call-schema-header-fix
2 parents 2f3de4e + 58972e5 commit a420ee7

File tree

12 files changed

+516
-11
lines changed

12 files changed

+516
-11
lines changed

src/features/Apiexplorer/Schema/RecursiveContent/RecursiveProperties/__tests__/RecursiveProperties.test.tsx

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,36 @@ const fakeItem = {
2020
description: 'This is recursive item 2',
2121
oneOf: 'This is oneOf key for recursive_item_2',
2222
},
23+
recursive_item_3: {
24+
description: 'This is recursive item 3',
25+
oneOf: [
26+
{
27+
description: 'This is object 1',
28+
type: 'string',
29+
},
30+
{
31+
description: 'This is object 2',
32+
type: 'object',
33+
properties: {
34+
property_1: {
35+
description: 'item 1',
36+
type: 'string',
37+
enum: ['deposit', 'withdraw'],
38+
},
39+
property_2: {
40+
description: 'item 2',
41+
type: 'object',
42+
properties: {
43+
property_2_1: {
44+
description: 'item 3',
45+
type: 'string',
46+
},
47+
},
48+
},
49+
},
50+
},
51+
],
52+
},
2353
},
2454
definitions: {
2555
stream_types: {
@@ -59,16 +89,19 @@ describe('RecursiveProperties', () => {
5989
expect(recursion_1_description).toBeVisible();
6090

6191
const recursion_2_name = await screen.findByText(/recursive_item_1/i);
62-
expect(recursion_2_name).toBeVisible();
63-
6492
const recursion_2_description = await screen.findByText(/This is a recursive item/i);
93+
expect(recursion_2_name).toBeVisible();
6594
expect(recursion_2_description).toBeVisible();
6695

6796
const recursion_3_name = await screen.findByText(/recursive_item_2/i);
68-
expect(recursion_3_name).toBeVisible();
69-
7097
const recursion_3_description = await screen.findByText(/This is recursive item 2/i);
98+
expect(recursion_3_name).toBeVisible();
7199
expect(recursion_3_description).toBeVisible();
100+
101+
const recursion_4_name = await screen.findByText(/recursive_item_3/i);
102+
const recursion_4_description = await screen.findByText(/This is recursive item 3/i);
103+
expect(recursion_4_name).toBeVisible();
104+
expect(recursion_4_description).toBeVisible();
72105
});
73106

74107
it('renders only the description (last item) if there are no nested items anymore', async () => {
@@ -79,7 +112,7 @@ describe('RecursiveProperties', () => {
79112
expect(item).toBeVisible();
80113
});
81114

82-
it('renders StreamTypesObject if value contains oneOf meaning its forgetAll api call', async () => {
115+
it('renders StreamTypesObject for forgetAll api call', async () => {
83116
render(
84117
<RecursiveProperties
85118
is_open
@@ -91,4 +124,19 @@ describe('RecursiveProperties', () => {
91124
const streamTypesObject = await screen.getByTestId('dt_stream_types_object');
92125
expect(streamTypesObject).toBeVisible();
93126
});
127+
128+
it('renders SchemaOneOfObjectContent if the value of key inside property schema contains oneOf', async () => {
129+
render(
130+
<RecursiveProperties
131+
is_open
132+
properties={fakeItem.properties}
133+
value={fakeItem.properties}
134+
jsonSchema={fakeItem}
135+
/>,
136+
);
137+
const schemaOneOfObjectContent = await screen.getAllByTestId(
138+
'dt_schema_oneof_object_content',
139+
)[0];
140+
expect(schemaOneOfObjectContent).toBeVisible();
141+
});
94142
});

src/features/Apiexplorer/Schema/RecursiveContent/RecursiveProperties/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import SchemaDescription from '../SchemaDescription';
33
import SchemaObjectContent from '../SchemaObjectContent';
44
import StreamTypesObject from '../StreamTypesObject';
5+
import SchemaOneOfObjectContent from '../SchemaOneOfObjectContent';
56

67
type TRecursiveProperties = {
78
is_open: boolean;
@@ -38,14 +39,20 @@ const RecursiveProperties = ({ is_open, properties, value, jsonSchema }: TRecurs
3839
{index === 0 && value?.items?.description && (
3940
<SchemaDescription description={value.items.description} />
4041
)}
41-
{key === 'forget_all' && 'oneOf' in value[key] ? (
42+
{key === 'forget_all' && 'oneOf' in properties[key] ? (
4243
<SchemaObjectContent
4344
key={key}
4445
key_value={key}
4546
properties={properties}
4647
jsonSchema={jsonSchema}
4748
is_stream_types
4849
/>
50+
) : typeof properties[key] === 'object' && 'oneOf' in properties[key] ? (
51+
<SchemaOneOfObjectContent
52+
property={properties[key]}
53+
key_title={key}
54+
jsonSchema={jsonSchema}
55+
/>
4956
) : (
5057
<SchemaObjectContent key={key} key_value={key} properties={properties} />
5158
)}

src/features/Apiexplorer/Schema/RecursiveContent/SchemaBodyHeader/__tests__/SchemaBodyHeader.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,24 @@ describe('SchemaBodyHeader', () => {
178178
expect(stream_types).toBeVisible();
179179
expect(array_type).toBeVisible();
180180
});
181+
182+
it('should render the SchemaBodyHeader for array with items type', async () => {
183+
render(
184+
<SchemaBodyHeader
185+
key_value='test_key_value'
186+
type='array'
187+
defaultValue='default_test'
188+
pattern='some_test_pattern'
189+
examples={['example1', 'example2']}
190+
enum={['test1', 'test2']}
191+
title={undefined}
192+
is_open_object
193+
setIsOpenObject={() => jest.fn()}
194+
is_stream_types={false}
195+
items_type='string'
196+
/>,
197+
);
198+
const type = await screen.findByText(/string/i);
199+
expect(type).toBeVisible();
200+
});
181201
});

src/features/Apiexplorer/Schema/RecursiveContent/SchemaBodyHeader/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type TSchemaBodyHeader = {
1313
examples: string[];
1414
enum;
1515
is_stream_types: boolean;
16+
items_type?: string;
1617
};
1718

1819
const SchemaBodyHeader = ({
@@ -26,6 +27,7 @@ const SchemaBodyHeader = ({
2627
is_open_object,
2728
setIsOpenObject,
2829
is_stream_types,
30+
items_type,
2931
}: TSchemaBodyHeader) => {
3032
let typeClassName;
3133
switch (type) {
@@ -110,7 +112,7 @@ const SchemaBodyHeader = ({
110112
<div className={styles.schemaObjectContent}>
111113
<div>
112114
<button onClick={() => setIsOpenObject(!is_open_object)}>
113-
{title ? key_value : 'object'}
115+
{title ? key_value : items_type ?? 'object'}
114116
</button>
115117
</div>
116118
</div>

src/features/Apiexplorer/Schema/RecursiveContent/SchemaObjectContent/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default function SchemaObjectContent({
3030
examples,
3131
enum: _enum,
3232
title,
33+
items,
3334
} = properties[key_value];
3435
const value = properties[key_value];
3536
let data;
@@ -60,6 +61,7 @@ export default function SchemaObjectContent({
6061
is_open_object={is_open_object}
6162
setIsOpenObject={setIsOpenObject}
6263
is_stream_types={is_stream_types}
64+
items_type={items?.type}
6365
/>
6466
{/* Description */}
6567
<SchemaDescription description={description} />
@@ -74,7 +76,7 @@ export default function SchemaObjectContent({
7476
{!is_code_open && (
7577
<RecursiveProperties
7678
is_open={is_open_object}
77-
properties={value.properties || value?.items?.properties}
79+
properties={value.properties || value?.items?.properties || value?.patternProperties}
7880
value={value}
7981
jsonSchema={jsonSchema}
8082
/>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import React from 'react';
2+
import SchemaOneOfBodyHeader from '..';
3+
import { screen, render } from '@testing-library/react';
4+
5+
describe('SchemaOneOfBodyHeader', () => {
6+
it('should render SchemaOneOfBodyHeader with button having object type as display name', () => {
7+
render(
8+
<SchemaOneOfBodyHeader
9+
key_title='cashier'
10+
oneOf={[
11+
{
12+
description: 'description 1',
13+
type: 'string',
14+
},
15+
{
16+
description: 'description 1',
17+
type: 'number',
18+
},
19+
{
20+
description: 'description 2',
21+
type: 'object',
22+
},
23+
]}
24+
updateIndexArray={() => jest.fn()}
25+
/>,
26+
);
27+
const one_of = screen.getByText(/one of/i);
28+
const button_1 = screen.getByRole('button', {
29+
name: /string/i,
30+
});
31+
const button_2 = screen.getByRole('button', {
32+
name: /object/i,
33+
});
34+
const button_3 = screen.getByRole('button', {
35+
name: /number/i,
36+
});
37+
expect(one_of).toBeInTheDocument();
38+
expect(button_1).toBeInTheDocument();
39+
expect(button_2).toBeInTheDocument();
40+
expect(button_3).toBeInTheDocument();
41+
});
42+
43+
it('should render SchemaOneOfBodyHeader with button having object type as display name', () => {
44+
render(
45+
<SchemaOneOfBodyHeader
46+
key_title='cashier'
47+
oneOf={[
48+
{
49+
description: 'description 1',
50+
type: 'string',
51+
title: 'some title',
52+
},
53+
{
54+
description: 'description 2',
55+
type: 'null',
56+
},
57+
]}
58+
updateIndexArray={() => jest.fn()}
59+
/>,
60+
);
61+
const one_of = screen.getByText(/one of/i);
62+
const button_1 = screen.getByRole('button', {
63+
name: /some title/i,
64+
});
65+
const button_2 = screen.getByRole('button', {
66+
name: /null/i,
67+
});
68+
expect(one_of).toBeInTheDocument();
69+
expect(button_1).toBeInTheDocument();
70+
expect(button_2).toBeInTheDocument();
71+
});
72+
73+
it('should render SchemaOneOfBodyHeader with pattern', () => {
74+
render(
75+
<SchemaOneOfBodyHeader
76+
key_title='cashier'
77+
oneOf={[
78+
{
79+
type: 'integer',
80+
pattern: 'some regex pattern',
81+
},
82+
{
83+
type: 'array',
84+
items: {
85+
type: 'string',
86+
pattern: 'some regex pattern',
87+
},
88+
},
89+
]}
90+
updateIndexArray={() => jest.fn()}
91+
/>,
92+
);
93+
const one_of = screen.getByText(/one of/i);
94+
const type_1 = screen.getByText(/integer/i);
95+
const pattern = screen.getByText(/some regex pattern/i);
96+
const type_2 = screen.getByText(/array/i);
97+
98+
expect(one_of).toBeInTheDocument();
99+
expect(type_1).toBeInTheDocument();
100+
expect(pattern).toBeInTheDocument();
101+
expect(type_2).toBeInTheDocument();
102+
});
103+
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React from 'react';
2+
import styles from '../../Schema.module.scss';
3+
4+
type TSchemaOneOfBodyHeader = {
5+
key_title: string;
6+
oneOf: Record<string, any>[];
7+
updateIndexArray: (index: number) => void;
8+
};
9+
10+
const SchemaOneOfBodyHeader = ({ key_title, oneOf, updateIndexArray }: TSchemaOneOfBodyHeader) => {
11+
const generateClassName = (type) => {
12+
let typeClass;
13+
switch (type) {
14+
case 'number':
15+
typeClass = styles.number;
16+
break;
17+
case 'array':
18+
typeClass = styles.array;
19+
break;
20+
case 'integer':
21+
typeClass = styles.integer;
22+
break;
23+
case 'null':
24+
typeClass = styles.null;
25+
break;
26+
default:
27+
typeClass = styles.string;
28+
break;
29+
}
30+
return typeClass;
31+
};
32+
return (
33+
<div className={styles.schemaBodyHeader}>
34+
<div className={styles.schemaBodyType}>
35+
<div className={styles.enumFlex}>
36+
<p style={{ fontSize: '2.6rem' }}>
37+
<strong>{key_title}</strong>
38+
</p>
39+
<div className={styles.enumContainer}>
40+
<span className={styles.enumLabel}>one of</span>
41+
{oneOf &&
42+
Array.isArray(oneOf) &&
43+
oneOf.map((object, index) => {
44+
const show_btn = object?.description || object?.properties;
45+
const typeClassName = generateClassName(object?.type);
46+
47+
return (
48+
<React.Fragment key={index}>
49+
<div className={styles.schemaObjectContent}>
50+
{show_btn ? (
51+
<button
52+
onClick={() => {
53+
updateIndexArray(index);
54+
}}
55+
>
56+
{object.title ? object.title : object.type}
57+
</button>
58+
) : (
59+
<span className={`${styles.enumType} ${typeClassName}`}>{object.type}</span>
60+
)}
61+
{'pattern' in object && (
62+
<div className={styles.schemaRegexContainer}>
63+
<span className={styles.schemaBodyPattern}>{object.pattern}</span>
64+
</div>
65+
)}
66+
</div>
67+
</React.Fragment>
68+
);
69+
})}
70+
</div>
71+
</div>
72+
</div>
73+
</div>
74+
);
75+
};
76+
77+
export default SchemaOneOfBodyHeader;

0 commit comments

Comments
 (0)