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

Commit 6e2523e

Browse files
authored
Merge pull request #269 from utkarsha-deriv/utkarsha/DERA-386/Fix-request-schema-forget-api-call
2 parents 2393dd0 + 4659ad9 commit 6e2523e

File tree

13 files changed

+424
-7
lines changed

13 files changed

+424
-7
lines changed

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,35 @@ const fakeItem = {
1313
},
1414
},
1515
properties: {
16-
recursive_item: {
16+
recursive_item_1: {
1717
description: 'This is a recursive item',
1818
},
19+
recursive_item_2: {
20+
description: 'This is recursive item 2',
21+
oneOf: 'This is oneOf key for recursive_item_2',
22+
},
23+
},
24+
definitions: {
25+
stream_types: {
26+
description: 'This stream_types description',
27+
type: 'string',
28+
enum: [
29+
'balance',
30+
'candles',
31+
'cashier_payments',
32+
'p2p_advert',
33+
'p2p_advertiser',
34+
'p2p_order',
35+
'proposal',
36+
'proposal_open_contract',
37+
'ticks',
38+
'transaction',
39+
'trading_platform_asset_listing',
40+
'website_status',
41+
'p2p_settings',
42+
'crypto_estimations',
43+
],
44+
},
1945
},
2046
};
2147

@@ -26,21 +52,43 @@ describe('RecursiveProperties', () => {
2652
is_open
2753
properties={fakeItem.properties || fakeItem?.items?.properties}
2854
value={fakeItem}
55+
jsonSchema={fakeItem}
2956
/>,
3057
);
3158
const recursion_1_description = await screen.findByText(/nested items/i);
3259
expect(recursion_1_description).toBeVisible();
3360

34-
const recursion_2_name = await screen.findByText(/recursive_item/i);
61+
const recursion_2_name = await screen.findByText(/recursive_item_1/i);
3562
expect(recursion_2_name).toBeVisible();
3663

3764
const recursion_2_description = await screen.findByText(/This is a recursive item/i);
3865
expect(recursion_2_description).toBeVisible();
66+
67+
const recursion_3_name = await screen.findByText(/recursive_item_2/i);
68+
expect(recursion_3_name).toBeVisible();
69+
70+
const recursion_3_description = await screen.findByText(/This is recursive item 2/i);
71+
expect(recursion_3_description).toBeVisible();
3972
});
4073

4174
it('renders only the description (last item) if there are no nested items anymore', async () => {
42-
render(<RecursiveProperties is_open properties={null} value={fakeItem} />);
75+
render(
76+
<RecursiveProperties is_open properties={null} value={fakeItem} jsonSchema={fakeItem} />,
77+
);
4378
const item = await screen.findByText(/This is the main item description/i);
4479
expect(item).toBeVisible();
4580
});
81+
82+
it('renders StreamTypesObject if value contains oneOf meaning its forgetAll api call', async () => {
83+
render(
84+
<RecursiveProperties
85+
is_open
86+
properties={null}
87+
value={fakeItem.properties.recursive_item_2}
88+
jsonSchema={fakeItem}
89+
/>,
90+
);
91+
const streamTypesObject = await screen.getByTestId('dt_stream_types_object');
92+
expect(streamTypesObject).toBeVisible();
93+
});
4694
});

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import React from 'react';
22
import SchemaDescription from '../SchemaDescription';
33
import SchemaObjectContent from '../SchemaObjectContent';
4+
import StreamTypesObject from '../StreamTypesObject';
45

56
type TRecursiveProperties = {
67
is_open: boolean;
78
properties: any;
89
value: any;
10+
jsonSchema: any;
911
};
1012

11-
const RecursiveProperties = ({ is_open, properties, value }: TRecursiveProperties) => {
13+
const RecursiveProperties = ({ is_open, properties, value, jsonSchema }: TRecursiveProperties) => {
1214
const keys = properties && Object.keys(properties);
15+
1316
if (!is_open) {
1417
return null;
1518
}
19+
if (value && 'oneOf' in value) {
20+
return (
21+
<React.Fragment>
22+
<StreamTypesObject definitions={jsonSchema.definitions} />
23+
</React.Fragment>
24+
);
25+
}
1626
if (!keys) {
1727
return (
1828
<React.Fragment>
@@ -28,7 +38,17 @@ const RecursiveProperties = ({ is_open, properties, value }: TRecursivePropertie
2838
{index === 0 && value?.items?.description && (
2939
<SchemaDescription description={value.items.description} />
3040
)}
31-
<SchemaObjectContent key={key} key_value={key} properties={properties} />
41+
{key === 'forget_all' && 'oneOf' in value[key] ? (
42+
<SchemaObjectContent
43+
key={key}
44+
key_value={key}
45+
properties={properties}
46+
jsonSchema={jsonSchema}
47+
is_stream_types
48+
/>
49+
) : (
50+
<SchemaObjectContent key={key} key_value={key} properties={properties} />
51+
)}
3252
</React.Fragment>
3353
);
3454
})}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('SchemaBodyHeader', () => {
1616
title='test title'
1717
is_open_object
1818
setIsOpenObject={() => jest.fn()}
19+
is_stream_types={false}
1920
/>,
2021
);
2122
const type = await screen.findByText('number');
@@ -34,6 +35,7 @@ describe('SchemaBodyHeader', () => {
3435
title='test title'
3536
is_open_object
3637
setIsOpenObject={() => jest.fn()}
38+
is_stream_types={false}
3739
/>,
3840
);
3941
const type = await screen.findByText('array');
@@ -52,6 +54,7 @@ describe('SchemaBodyHeader', () => {
5254
title='test title'
5355
is_open_object
5456
setIsOpenObject={() => jest.fn()}
57+
is_stream_types={false}
5558
/>,
5659
);
5760
const type = await screen.findByText('integer');
@@ -70,6 +73,7 @@ describe('SchemaBodyHeader', () => {
7073
title='test title'
7174
is_open_object
7275
setIsOpenObject={() => jest.fn()}
76+
is_stream_types={false}
7377
/>,
7478
);
7579
const type = await screen.findByText('string');
@@ -88,6 +92,7 @@ describe('SchemaBodyHeader', () => {
8892
title='test title'
8993
is_open_object
9094
setIsOpenObject={() => jest.fn()}
95+
is_stream_types={false}
9196
/>,
9297
);
9398
const type = await screen.findByText(/number/i);
@@ -106,6 +111,7 @@ describe('SchemaBodyHeader', () => {
106111
title='test title'
107112
is_open_object
108113
setIsOpenObject={() => jest.fn()}
114+
is_stream_types={false}
109115
/>,
110116
);
111117
const type = await screen.findByText(/string/i);
@@ -124,6 +130,7 @@ describe('SchemaBodyHeader', () => {
124130
title='test title'
125131
is_open_object
126132
setIsOpenObject={() => jest.fn()}
133+
is_stream_types={false}
127134
/>,
128135
);
129136
const type = await screen.findByText(/array/i);
@@ -142,9 +149,33 @@ describe('SchemaBodyHeader', () => {
142149
title='test title'
143150
is_open_object
144151
setIsOpenObject={() => jest.fn()}
152+
is_stream_types={false}
145153
/>,
146154
);
147155
const type = await screen.findByText(/integer/i);
148156
expect(type).toBeVisible();
149157
});
158+
159+
it('should render the SchemaBodyHeader with oneOf stream_types array if is_stream_types is true', async () => {
160+
render(
161+
<SchemaBodyHeader
162+
key_value={null}
163+
type={null}
164+
defaultValue='default_test'
165+
pattern=''
166+
examples={null}
167+
enum={null}
168+
title=''
169+
is_open_object
170+
setIsOpenObject={() => jest.fn()}
171+
is_stream_types={true}
172+
/>,
173+
);
174+
const oneOfType = await screen.findByText(/one of/i);
175+
const stream_types = await screen.findByText(/stream_types/i);
176+
const array_type = await screen.findByText(/array/i);
177+
expect(oneOfType).toBeVisible();
178+
expect(stream_types).toBeVisible();
179+
expect(array_type).toBeVisible();
180+
});
150181
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type TSchemaBodyHeader = {
1212
setIsOpenObject: (boolean) => void;
1313
examples: string[];
1414
enum;
15+
is_stream_types: boolean;
1516
};
1617

1718
const SchemaBodyHeader = ({
@@ -24,6 +25,7 @@ const SchemaBodyHeader = ({
2425
title,
2526
is_open_object,
2627
setIsOpenObject,
28+
is_stream_types,
2729
}: TSchemaBodyHeader) => {
2830
let typeClassName;
2931
switch (type) {
@@ -116,6 +118,15 @@ const SchemaBodyHeader = ({
116118
) : (
117119
<></>
118120
)}
121+
122+
{is_stream_types && (
123+
<div className={styles.schemaObjectContent}>
124+
<span className={styles.enumLabel}>one of</span>
125+
<button onClick={() => setIsOpenObject(!is_open_object)}>stream_types</button>
126+
<span className={`${styles.enumType} ${styles.array}`}>array</span>
127+
</div>
128+
)}
129+
119130
{pattern && (
120131
<div className={styles.schemaRegexContainer}>
121132
<div className={styles.schemaBodyPattern}>{pattern}</div>

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,37 @@ const fake_properties = {
2121
},
2222
};
2323

24+
const stream_types_schema = {
25+
properties: {
26+
test_property: {
27+
description: 'property description',
28+
oneOf: 'this is oneOf key',
29+
},
30+
},
31+
definitions: {
32+
stream_types: {
33+
description: 'This stream_types description',
34+
type: 'string',
35+
enum: [
36+
'balance',
37+
'candles',
38+
'cashier_payments',
39+
'p2p_advert',
40+
'p2p_advertiser',
41+
'p2p_order',
42+
'proposal',
43+
'proposal_open_contract',
44+
'ticks',
45+
'transaction',
46+
'trading_platform_asset_listing',
47+
'website_status',
48+
'p2p_settings',
49+
'crypto_estimations',
50+
],
51+
},
52+
},
53+
};
54+
2455
describe('SchemaObjectContent', () => {
2556
it('should be able to open a nested object item', async () => {
2657
render(<SchemaObjectContent key_value='test_item' properties={fake_properties} />);
@@ -96,4 +127,24 @@ describe('SchemaObjectContent', () => {
96127
const schema = await screen.findByTitle('JSON');
97128
expect(schema).toBeVisible();
98129
});
130+
131+
it('should open StreamTypesObject upon clicking stream_types button', async () => {
132+
render(
133+
<SchemaObjectContent
134+
key='test_property'
135+
key_value='test_property'
136+
properties={stream_types_schema.properties}
137+
jsonSchema={stream_types_schema}
138+
is_stream_types={true}
139+
/>,
140+
);
141+
142+
const button = await screen.findByRole('button', { name: /stream_types/i });
143+
expect(button).toBeVisible();
144+
145+
await userEvent.click(button);
146+
147+
const streamTypesObject = await screen.getByTestId('dt_stream_types_object');
148+
expect(streamTypesObject).toBeVisible();
149+
});
99150
});

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ const ReactJson = React.lazy(() => import('react-json-view'));
1010
type TSchemaObjectContent = {
1111
key_value: string;
1212
properties: any;
13+
jsonSchema?: any;
14+
is_stream_types?: boolean;
1315
};
1416

15-
export default function SchemaObjectContent({ key_value, properties }: TSchemaObjectContent) {
17+
export default function SchemaObjectContent({
18+
key_value,
19+
properties,
20+
jsonSchema,
21+
is_stream_types = false,
22+
}: TSchemaObjectContent) {
1623
const [is_open_object, setIsOpenObject] = useState<boolean>(false);
1724
const [is_code_open, setIsCodeOpen] = useState<boolean>(false);
1825
const {
@@ -52,6 +59,7 @@ export default function SchemaObjectContent({ key_value, properties }: TSchemaOb
5259
title={title}
5360
is_open_object={is_open_object}
5461
setIsOpenObject={setIsOpenObject}
62+
is_stream_types={is_stream_types}
5563
/>
5664
{/* Description */}
5765
<SchemaDescription description={description} />
@@ -68,6 +76,7 @@ export default function SchemaObjectContent({ key_value, properties }: TSchemaOb
6876
is_open={is_open_object}
6977
properties={value.properties || value?.items?.properties}
7078
value={value}
79+
jsonSchema={jsonSchema}
7180
/>
7281
)}
7382
</div>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import { TEnumStreamType } from '@site/src/types';
3+
import styles from '../../Schema.module.scss';
4+
5+
type TStreamTypesBody = {
6+
type: string;
7+
_enum: TEnumStreamType;
8+
};
9+
10+
const StreamTypesBody = ({ type, _enum }: TStreamTypesBody) => {
11+
return (
12+
<div className={styles.streamTypesBody}>
13+
<div className={styles.streamTypesObject}>
14+
<span className={styles.enumLabel}>enum</span>
15+
<span className={`${styles.enumType} ${styles.string}`}>{type}</span>
16+
{_enum.map((enum_name: string, i: number) => (
17+
<div className={`${styles.schemaCode} ${styles.schemaEnums}`} key={i}>
18+
{enum_name}
19+
</div>
20+
))}
21+
</div>
22+
</div>
23+
);
24+
};
25+
26+
export default StreamTypesBody;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import SchemaTitle from '../../SchemaTitle';
3+
import styles from '../../Schema.module.scss';
4+
5+
type TStreamTypesHeader = {
6+
description: string;
7+
};
8+
9+
const StreamTypesHeader = ({ description }: TStreamTypesHeader) => {
10+
return (
11+
<div className={styles.streamTypesHeader}>
12+
<SchemaTitle className={styles.streamTypesTitle}>stream_types</SchemaTitle>
13+
<div className={styles.streamTypesDescription}>
14+
<div>{description}</div>
15+
</div>
16+
</div>
17+
);
18+
};
19+
20+
export default StreamTypesHeader;

0 commit comments

Comments
 (0)