Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
add tests, response type title
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolodya committed Oct 28, 2018
1 parent fb17e61 commit a861e1e
Show file tree
Hide file tree
Showing 4 changed files with 388 additions and 13 deletions.
193 changes: 191 additions & 2 deletions packages/api-explorer/__tests__/ResponseSchema.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ test('selectedStatus should change state of selectedStatus', () => {
test('should display response schema description', () => {
const responseSchema = shallow(<ResponseSchema {...props} />);

expect(responseSchema.find('p.desc').text()).toBe(props.operation.responses['200'].description);
expect(
responseSchema
.find('p.desc')
.first()
.text(),
).toBe(props.operation.responses['200'].description);
});

test('should work if there are no responses', () => {
Expand All @@ -47,6 +52,7 @@ test('should work if there are no responses', () => {
Object.assign({}, oas.operation('/pet/{petId}', 'get'), { responses: undefined }),
)
}
oas={oas}
/>,
);

Expand All @@ -64,13 +70,14 @@ test('should work if responses is an empty object', () => {
Object.assign({}, oas.operation('/pet/{petId}', 'get'), { responses: {} }),
)
}
oas={oas}
/>,
);

expect(responseSchema.html()).toBe(null);
});

test('should contain ResponseSchemaBody element if $ref exist', () => {
test('should contain ResponseSchemaBody element if $ref exist for "application/json"', () => {
const responseSchema = shallow(<ResponseSchema {...props} />);
expect(responseSchema.text()).toContain('ResponseSchemaBody');
});
Expand All @@ -88,3 +95,185 @@ test('should not contain ResponseSchemaBody element if $ref not exist', () => {
const responseSchema = shallow(<ResponseSchema {...testProps} />);
expect(responseSchema.find('table').length).toBe(0);
});

test('should show "string" response type', () => {
const testProps = {
operation: oas.operation('/user/login', 'get'),
oas,
};

const responseSchema = shallow(<ResponseSchema {...testProps} />);
expect(
responseSchema
.find('p span')
.last()
.text(),
).toBe('string');
});

test('should show "object" response schema type', () => {
const testProps = {
operation: oas.operation('/store/inventory', 'get'),
oas,
};

const responseSchema = shallow(<ResponseSchema {...testProps} />);
expect(
responseSchema
.find('p span')
.last()
.text(),
).toBe('object');
});

test('should show "array" response schema type', () => {
const testProps = {
operation: oas.operation('/pet/findByTags', 'get'),
oas,
};

const responseSchema = shallow(<ResponseSchema {...testProps} />);
expect(
responseSchema
.find('p span')
.last()
.text(),
).toBe('array');
});

test('should render schema type from "application/json"', () => {
const testProps = {
operation: new Operation(
{},
'/',
'get',
Object.assign({}, oas.operation('/pet/findByTags', 'get'), {
responses: {
'200': {
content: {
'application/json': {
description: 'successful operation',
schema: {
type: 'string',
},
},
},
},
},
}),
),
oas,
};

const responseSchema = shallow(<ResponseSchema {...testProps} />);
expect(
responseSchema
.find('p span')
.last()
.text(),
).toBe('string');
});

test('should contain ResponseSchemaBody element if $ref exist for "application/xml"', () => {
const testProps = {
operation: new Operation(
oas,
'/',
'get',
Object.assign({}, oas.operation('/pet/{petId}', 'get'), {
responses: {
'200': {
content: {
'application/xml': {
description: 'successful operation',
schema: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
}),
),
oas,
};

const responseSchema = shallow(<ResponseSchema {...testProps} />);
expect(responseSchema.text()).toContain('ResponseSchemaBody');
});

test('should change selectedStatus in component', () => {
const responseSchema = shallow(<ResponseSchema {...props} />);

const selectedStatus = responseSchema.state().selectedStatus;
// const selectedStatus = responseSchema.instance().selectedStatus

responseSchema.instance().changeHandler({ target: { value: '404' } });
const newSelectedStatus = responseSchema.state().selectedStatus;
expect(selectedStatus).toEqual('200');
expect(newSelectedStatus).toEqual('404');
});

test('should show "object" response schema type for "application/xml" content', () => {
const testProps = {
operation: new Operation(
{},
'/',
'get',
Object.assign({}, oas.operation('/pet/findByTags', 'get'), {
responses: {
'200': {
content: {
'application/xml': {
description: 'successful operation',
schema: {
type: 'object',
properties: {
code: {
type: 'integer',
format: 'int32',
},
},
},
},
},
},
},
}),
),
oas,
};

const responseSchema = shallow(<ResponseSchema {...testProps} />);
expect(
responseSchema
.find('p span')
.last()
.text(),
).toBe('object');
});

test('should not break if schema property missing', () => {
const testProps = {
operation: new Operation(
{},
'/',
'get',
Object.assign({}, oas.operation('/pet/findByTags', 'get'), {
responses: {
'200': {
content: {
'application/xml': {
description: 'successful operation',
},
},
},
},
}),
),
oas,
};

const responseSchema = shallow(<ResponseSchema {...testProps} />);
expect(responseSchema.find('table').length).toBe(0);
});
160 changes: 160 additions & 0 deletions packages/api-explorer/__tests__/ResponseSchemaBody.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,163 @@ test('should flatten array ', () => {
},
]);
});

test('display object properties inside another object in the table', () => {
const schema = {
type: 'object',
properties: {
a: {
type: 'object',
properties: {
a: {
type: 'string',
},
},
},
},
};
const responseSchemaBody = shallow(<ResponseSchemaBody oas={oas} schema={schema} />);
expect(
responseSchemaBody
.find('th')
.map(a => a.text())
.filter(a => a === 'a.a').length,
).toBe(1);
});

test('display $ref items inside object', () => {
const schema = {
type: 'object',
properties: {
a: {
type: 'object',
properties: {
pets: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
};
const testOas = {
components: {
schemas: {
Pet: {
type: 'object',
properties: {
index: {
type: 'integer',
},
},
},
},
},
};
const responseSchemaBody = shallow(<ResponseSchemaBody oas={testOas} schema={schema} />);
expect(
responseSchemaBody
.find('td')
.map(a => a.text())
.filter(a => a === 'array of objects').length,
).toBe(1);
expect(
responseSchemaBody
.find('th')
.map(a => a.text())
.filter(a => a === '| | index').length,
).toBe(1);
});

test('not fail when object property missing', () => {
const schema = {
type: 'object',
additionalProperties: {
type: 'integer',
format: 'int32',
},
};

const responseSchemaBody = shallow(<ResponseSchemaBody oas={oas} schema={schema} />);
expect(responseSchemaBody.find('th').length).toBe(0);
});

test('render top level array of object', () => {
const schema = {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
};

const testOas = {
components: {
schemas: {
Pet: {
type: 'object',
properties: {
name: {
type: 'string',
example: 'doggie',
},
},
},
},
},
};
const responseSchemaBody = shallow(<ResponseSchemaBody oas={testOas} schema={schema} />);
expect(
responseSchemaBody
.find('th')
.map(a => a.text())
.filter(a => a === 'name').length,
).toBe(1);
expect(
responseSchemaBody
.find('td')
.map(a => a.text())
.filter(a => a === 'string').length,
).toBe(1);
});

test('not render more than 3 level deep object', () => {
const schema = {
type: 'object',
properties: {
a: {
type: 'object',
properties: {
a: {
type: 'object',
properties: {
a: {
type: 'object',
properties: {
a: {
type: 'string',
},
},
},
},
},
},
},
},
};

const responseSchemaBody = shallow(<ResponseSchemaBody oas={oas} schema={schema} />);
expect(
responseSchemaBody
.find('th')
.map(a => a.text())
.filter(a => a === 'a.a.a').length,
).toBe(1);
expect(
responseSchemaBody
.find('th')
.map(a => a.text())
.filter(a => a === 'a.a.a.a').length,
).toBe(0);
});
Loading

0 comments on commit a861e1e

Please sign in to comment.