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

Commit

Permalink
test: added testcases for stream_types object
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarsha-deriv committed Dec 7, 2023
1 parent ade327e commit 252d03f
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,35 @@ const fakeItem = {
},
},
properties: {
recursive_item: {
recursive_item_1: {
description: 'This is a recursive item',
},
recursive_item_2: {
description: 'This is recursive item 2',
oneOf: 'This is oneOf key for recursive_item_2',
},
},
definitions: {
stream_types: {
description: 'This stream_types description',
type: 'string',
enum: [
'balance',
'candles',
'cashier_payments',
'p2p_advert',
'p2p_advertiser',
'p2p_order',
'proposal',
'proposal_open_contract',
'ticks',
'transaction',
'trading_platform_asset_listing',
'website_status',
'p2p_settings',
'crypto_estimations',
],
},
},
};

Expand All @@ -26,21 +52,43 @@ describe('RecursiveProperties', () => {
is_open
properties={fakeItem.properties || fakeItem?.items?.properties}
value={fakeItem}
jsonSchema={fakeItem}
/>,
);
const recursion_1_description = await screen.findByText(/nested items/i);
expect(recursion_1_description).toBeVisible();

const recursion_2_name = await screen.findByText(/recursive_item/i);
const recursion_2_name = await screen.findByText(/recursive_item_1/i);
expect(recursion_2_name).toBeVisible();

const recursion_2_description = await screen.findByText(/This is a recursive item/i);
expect(recursion_2_description).toBeVisible();

const recursion_3_name = await screen.findByText(/recursive_item_2/i);
expect(recursion_3_name).toBeVisible();

const recursion_3_description = await screen.findByText(/This is recursive item 2/i);
expect(recursion_3_description).toBeVisible();
});

it('renders only the description (last item) if there are no nested items anymore', async () => {
render(<RecursiveProperties is_open properties={null} value={fakeItem} />);
render(
<RecursiveProperties is_open properties={null} value={fakeItem} jsonSchema={fakeItem} />,
);
const item = await screen.findByText(/This is the main item description/i);
expect(item).toBeVisible();
});

it('renders StreamTypesObject if value contains oneOf meaning its forgetAll api call', async () => {
render(
<RecursiveProperties
is_open
properties={null}
value={fakeItem.properties.recursive_item_2}
jsonSchema={fakeItem}
/>,
);
const streamTypesObject = await screen.getByTestId('dt_stream_types_object');
expect(streamTypesObject).toBeVisible();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ type TRecursiveProperties = {
is_open: boolean;
properties: any;
value: any;
jsonSchema?: any;
jsonSchema: any;
};

const RecursiveProperties = ({ is_open, properties, value, jsonSchema }: TRecursiveProperties) => {
const keys = properties && Object.keys(properties);

if (!is_open) {
//if object is not open then ret null
return null;
}

if ('oneOf' in value) {
return <StreamTypesObject definitions={jsonSchema.definitions} />;
if (value && 'oneOf' in value) {
return (
<React.Fragment>
<StreamTypesObject definitions={jsonSchema.definitions} />
</React.Fragment>
);
}
// this will be true when we are not inside properties obj? !!!!!!
if (!keys) {
return (
<React.Fragment>
Expand All @@ -36,7 +38,6 @@ const RecursiveProperties = ({ is_open, properties, value, jsonSchema }: TRecurs
{index === 0 && value?.items?.description && (
<SchemaDescription description={value.items.description} />
)}
{/* check if its forgetAll Request not response */}
{key === 'forget_all' && 'oneOf' in value[key] ? (
<SchemaObjectContent
key={key}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('SchemaBodyHeader', () => {
title='test title'
is_open_object
setIsOpenObject={() => jest.fn()}
is_stream_types={false}
/>,
);
const type = await screen.findByText('number');
Expand All @@ -34,6 +35,7 @@ describe('SchemaBodyHeader', () => {
title='test title'
is_open_object
setIsOpenObject={() => jest.fn()}
is_stream_types={false}
/>,
);
const type = await screen.findByText('array');
Expand All @@ -52,6 +54,7 @@ describe('SchemaBodyHeader', () => {
title='test title'
is_open_object
setIsOpenObject={() => jest.fn()}
is_stream_types={false}
/>,
);
const type = await screen.findByText('integer');
Expand All @@ -70,6 +73,7 @@ describe('SchemaBodyHeader', () => {
title='test title'
is_open_object
setIsOpenObject={() => jest.fn()}
is_stream_types={false}
/>,
);
const type = await screen.findByText('string');
Expand All @@ -88,6 +92,7 @@ describe('SchemaBodyHeader', () => {
title='test title'
is_open_object
setIsOpenObject={() => jest.fn()}
is_stream_types={false}
/>,
);
const type = await screen.findByText(/number/i);
Expand All @@ -106,6 +111,7 @@ describe('SchemaBodyHeader', () => {
title='test title'
is_open_object
setIsOpenObject={() => jest.fn()}
is_stream_types={false}
/>,
);
const type = await screen.findByText(/string/i);
Expand All @@ -124,6 +130,7 @@ describe('SchemaBodyHeader', () => {
title='test title'
is_open_object
setIsOpenObject={() => jest.fn()}
is_stream_types={false}
/>,
);
const type = await screen.findByText(/array/i);
Expand All @@ -142,9 +149,33 @@ describe('SchemaBodyHeader', () => {
title='test title'
is_open_object
setIsOpenObject={() => jest.fn()}
is_stream_types={false}
/>,
);
const type = await screen.findByText(/integer/i);
expect(type).toBeVisible();
});

it('should render the SchemaBodyHeader with oneOf stream_types array if is_stream_types is true', async () => {
render(
<SchemaBodyHeader
key_value={null}
type={null}
defaultValue='default_test'
pattern=''
examples={null}
enum={null}
title=''
is_open_object
setIsOpenObject={() => jest.fn()}
is_stream_types={true}
/>,
);
const oneOfType = await screen.findByText(/one of/i);
const stream_types = await screen.findByText(/stream_types/i);
const array_type = await screen.findByText(/array/i);
expect(oneOfType).toBeVisible();
expect(stream_types).toBeVisible();
expect(array_type).toBeVisible();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type TSchemaBodyHeader = {
setIsOpenObject: (boolean) => void;
examples: string[];
enum;
is_stream_types?: boolean;
is_stream_types: boolean;
};

const SchemaBodyHeader = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,37 @@ const fake_properties = {
},
};

const stream_types_schema = {
properties: {
test_property: {
description: 'property description',
oneOf: 'this is oneOf key',
},
},
definitions: {
stream_types: {
description: 'This stream_types description',
type: 'string',
enum: [
'balance',
'candles',
'cashier_payments',
'p2p_advert',
'p2p_advertiser',
'p2p_order',
'proposal',
'proposal_open_contract',
'ticks',
'transaction',
'trading_platform_asset_listing',
'website_status',
'p2p_settings',
'crypto_estimations',
],
},
},
};

describe('SchemaObjectContent', () => {
it('should be able to open a nested object item', async () => {
render(<SchemaObjectContent key_value='test_item' properties={fake_properties} />);
Expand Down Expand Up @@ -96,4 +127,24 @@ describe('SchemaObjectContent', () => {
const schema = await screen.findByTitle('JSON');
expect(schema).toBeVisible();
});

it('should open StreamTypesObject upon clicking stream_types button', async () => {
render(
<SchemaObjectContent
key='test_property'
key_value='test_property'
properties={stream_types_schema.properties}
jsonSchema={stream_types_schema}
is_stream_types={true}
/>,
);

const button = await screen.findByRole('button', { name: /stream_types/i });
expect(button).toBeVisible();

await userEvent.click(button);

const streamTypesObject = await screen.getByTestId('dt_stream_types_object');
expect(streamTypesObject).toBeVisible();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type TSchemaObjectContent = {
is_stream_types?: boolean;
};

//json schema also here full obj
export default function SchemaObjectContent({
key_value,
properties,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react';
import userEvent from '@testing-library/user-event';
import { screen, render } from '@testing-library/react';
import StreamTypesObject from '..';

describe('StreamTypesObject', () => {
const json_schema = {
stream_types: {
description: 'This stream_types description',
type: 'string',
enum: [
'balance',
'candles',
'cashier_payments',
'p2p_advert',
'p2p_advertiser',
'p2p_order',
'proposal',
'proposal_open_contract',
'ticks',
'transaction',
'trading_platform_asset_listing',
'website_status',
'p2p_settings',
'crypto_estimations',
],
},
};

it('should render button that opens jsonschema', async () => {
render(<StreamTypesObject definitions={json_schema} />);

const schema_button = await screen.findByText('{}');

expect(schema_button).toBeVisible();

await userEvent.click(schema_button);

const schema = await screen.findByTitle('JSON');
expect(schema).toBeVisible();
});

it('should render the header of the object', () => {
render(<StreamTypesObject definitions={json_schema} />);

const header_title = screen.getAllByText(/stream_types/)[0];
expect(header_title).toBeInTheDocument();

const header_description = screen.getByText(/This stream_types description/i);
expect(header_description).toBeInTheDocument();
});

it('should render the body of StreamTypesObject', () => {
render(<StreamTypesObject definitions={json_schema} />);

const type = screen.getByText(/enum/i);
expect(type).toBeInTheDocument();

const enum_type = screen.getByText(/string/i);
expect(enum_type).toBeInTheDocument();

json_schema.stream_types.enum.map((item) => {
const enum_name = screen.getByText(item);
expect(enum_name).toBeInTheDocument();
});
});
});
Loading

0 comments on commit 252d03f

Please sign in to comment.