strapi/packages/plugins/documentation/__tests__/build-component-schema.test.js

272 lines
8.1 KiB
JavaScript
Raw Normal View History

2022-05-10 10:05:28 +02:00
'use strict';
2022-05-10 15:04:59 +02:00
const _ = require('lodash');
2022-05-10 10:05:28 +02:00
const buildComponentSchema = require('../server/services/helpers/build-component-schema');
const strapi = require('../__mocks__/strapi');
describe('Build Component Schema', () => {
2022-05-10 15:04:59 +02:00
beforeEach(() => {
// Reset the mocked strapi instance
global.strapi = _.cloneDeep(strapi);
});
2022-05-10 10:05:28 +02:00
2022-05-10 15:04:59 +02:00
it('builds the Response schema', () => {
2022-05-10 10:05:28 +02:00
const apiMocks = [
{
name: 'users-permissions',
getter: 'plugin',
ctNames: ['role'],
},
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
];
2022-05-10 15:04:59 +02:00
let schemas = {};
2022-05-10 10:05:28 +02:00
for (const mock of apiMocks) {
schemas = {
...schemas,
...buildComponentSchema(mock),
};
}
2022-05-10 10:15:38 +02:00
const schemaNames = Object.keys(schemas);
2022-05-10 10:05:28 +02:00
const [pluginResponseName, apiResponseName] = Object.keys(schemas);
const [pluginResponseValue, apiResponseValue] = Object.values(schemas);
const expectedShape = {
type: 'object',
2022-05-10 10:05:28 +02:00
properties: {
data: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: { type: 'object', properties: { test: { type: 'string' } } },
},
},
meta: { type: 'object' },
},
};
2022-05-10 10:15:38 +02:00
expect(schemaNames.length).toBe(2);
2022-05-10 10:05:28 +02:00
expect(pluginResponseName).toBe('UsersPermissionsRoleResponse');
expect(apiResponseName).toBe('RestaurantResponse');
expect(pluginResponseValue).toStrictEqual(expectedShape);
expect(apiResponseValue).toStrictEqual(expectedShape);
});
it('builds the ResponseList schema', () => {
2022-05-10 15:04:59 +02:00
global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
{ method: 'GET', path: '/test', handler: 'test.find' },
];
global.strapi.api.restaurant.routes.restaurant.routes = [
{ method: 'GET', path: '/test', handler: 'test.find' },
];
2022-05-10 10:05:28 +02:00
const apiMocks = [
{
name: 'users-permissions',
getter: 'plugin',
ctNames: ['role'],
},
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
];
2022-05-10 15:04:59 +02:00
let schemas = {};
2022-05-10 10:05:28 +02:00
for (const mock of apiMocks) {
schemas = {
...schemas,
...buildComponentSchema(mock),
};
}
const schemaNames = Object.keys(schemas);
2022-08-08 15:50:34 +02:00
const pluginListResponseValue = schemas.UsersPermissionsRoleListResponse;
const apiListResponseValue = schemas.RestaurantListResponse;
2022-05-10 10:05:28 +02:00
const expectedShape = {
type: 'object',
2022-05-10 10:05:28 +02:00
properties: {
data: {
type: 'array',
items: {
type: 'object',
properties: {
id: { type: 'string' },
attributes: { type: 'object', properties: { test: { type: 'string' } } },
},
},
},
meta: {
type: 'object',
properties: {
pagination: {
properties: {
page: { type: 'integer' },
pageSize: { type: 'integer', minimum: 25 },
pageCount: { type: 'integer', maximum: 1 },
total: { type: 'integer' },
},
},
},
},
},
};
2022-05-10 10:15:38 +02:00
expect(schemaNames.length).toBe(4);
2022-05-10 10:05:28 +02:00
expect(schemaNames.includes('UsersPermissionsRoleListResponse')).toBe(true);
expect(schemaNames.includes('RestaurantListResponse')).toBe(true);
expect(pluginListResponseValue).toStrictEqual(expectedShape);
expect(apiListResponseValue).toStrictEqual(expectedShape);
});
it('builds the Request schema', () => {
2022-05-10 15:04:59 +02:00
global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
{ method: 'POST', path: '/test', handler: 'test.create' },
];
global.strapi.api.restaurant.routes.restaurant.routes = [
{ method: 'POST', path: '/test', handler: 'test.create' },
];
2022-05-10 10:05:28 +02:00
const apiMocks = [
{
name: 'users-permissions',
getter: 'plugin',
ctNames: ['role'],
},
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
];
2022-05-10 15:04:59 +02:00
let schemas = {};
2022-05-10 10:05:28 +02:00
for (const mock of apiMocks) {
schemas = {
...schemas,
...buildComponentSchema(mock),
};
}
const schemaNames = Object.keys(schemas);
2022-08-08 15:50:34 +02:00
const pluginListResponseValue = schemas.UsersPermissionsRoleRequest;
const apiListResponseValue = schemas.RestaurantRequest;
2022-05-10 10:05:28 +02:00
const expectedShape = {
type: 'object',
2022-05-16 14:26:54 +02:00
required: ['data'],
2022-05-10 10:05:28 +02:00
properties: {
data: {
2022-05-16 14:26:54 +02:00
required: [],
2022-05-10 10:05:28 +02:00
type: 'object',
properties: { test: { type: 'string' } },
},
},
};
2022-05-10 10:15:38 +02:00
expect(schemaNames.length).toBe(4);
2022-05-10 10:05:28 +02:00
expect(schemaNames.includes('UsersPermissionsRoleRequest')).toBe(true);
expect(schemaNames.includes('RestaurantRequest')).toBe(true);
expect(pluginListResponseValue).toStrictEqual(expectedShape);
expect(apiListResponseValue).toStrictEqual(expectedShape);
});
it('builds the LocalizationResponse schema', () => {
2022-05-10 15:04:59 +02:00
global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
{ method: 'GET', path: '/localizations', handler: 'test' },
];
global.strapi.api.restaurant.routes.restaurant.routes = [
{ method: 'GET', path: '/localizations', handler: 'test' },
];
2022-05-10 10:05:28 +02:00
const apiMocks = [
{
name: 'users-permissions',
getter: 'plugin',
ctNames: ['role'],
},
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
];
2022-05-10 15:04:59 +02:00
let schemas = {};
2022-05-10 10:05:28 +02:00
for (const mock of apiMocks) {
schemas = {
...schemas,
...buildComponentSchema(mock),
};
}
const schemaNames = Object.keys(schemas);
2022-08-08 15:50:34 +02:00
const pluginListResponseValue = schemas.UsersPermissionsRoleLocalizationResponse;
const apiListResponseValue = schemas.RestaurantLocalizationResponse;
2022-05-10 10:05:28 +02:00
const expectedShape = {
type: 'object',
properties: {
id: { type: 'string' },
test: { type: 'string' },
},
};
2022-05-10 10:15:38 +02:00
expect(schemaNames.length).toBe(4);
2022-05-10 10:05:28 +02:00
expect(schemaNames.includes('UsersPermissionsRoleLocalizationResponse')).toBe(true);
expect(schemaNames.includes('RestaurantLocalizationResponse')).toBe(true);
expect(pluginListResponseValue).toStrictEqual(expectedShape);
expect(apiListResponseValue).toStrictEqual(expectedShape);
});
it('builds the LocalizationRequest schema', () => {
2022-05-10 15:04:59 +02:00
global.strapi.plugins['users-permissions'].routes['content-api'].routes = [
{ method: 'POST', path: '/localizations', handler: 'test' },
];
global.strapi.api.restaurant.routes.restaurant.routes = [
{ method: 'POST', path: '/localizations', handler: 'test' },
];
2022-05-10 10:05:28 +02:00
const apiMocks = [
{
name: 'users-permissions',
getter: 'plugin',
ctNames: ['role'],
},
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] },
];
2022-05-10 15:04:59 +02:00
let schemas = {};
2022-05-10 10:05:28 +02:00
for (const mock of apiMocks) {
schemas = {
...schemas,
...buildComponentSchema(mock),
};
}
const schemaNames = Object.keys(schemas);
2022-08-08 15:50:34 +02:00
const pluginListResponseValue = schemas.UsersPermissionsRoleLocalizationRequest;
const apiListResponseValue = schemas.RestaurantLocalizationRequest;
2022-05-10 10:05:28 +02:00
const expectedShape = {
type: 'object',
2022-05-16 14:26:54 +02:00
required: ['locale'],
2022-05-10 10:05:28 +02:00
properties: { test: { type: 'string' } },
};
2022-05-10 10:15:38 +02:00
expect(schemaNames.length).toBe(8);
2022-05-10 10:05:28 +02:00
expect(schemaNames.includes('UsersPermissionsRoleLocalizationRequest')).toBe(true);
expect(schemaNames.includes('RestaurantLocalizationRequest')).toBe(true);
expect(pluginListResponseValue).toStrictEqual(expectedShape);
expect(apiListResponseValue).toStrictEqual(expectedShape);
});
it('creates the correct name given multiple content types', () => {
const apiMock = {
name: 'users-permissions',
getter: 'plugin',
ctNames: ['permission', 'role', 'user'],
};
const schemas = buildComponentSchema(apiMock);
const schemaNames = Object.keys(schemas);
const [permission, role, user] = schemaNames;
expect(schemaNames.length).toBe(3);
expect(permission).toBe('UsersPermissionsPermissionResponse');
expect(role).toBe('UsersPermissionsRoleResponse');
expect(user).toBe('UsersPermissionsUserResponse');
});
});