Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c60df5
commit 60dfae1
Showing
2 changed files
with
362 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const strapi = { | ||
plugins: { | ||
'users-permissions': { | ||
contentTypes: { | ||
role: { | ||
attributes: { | ||
name: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
routes: { | ||
'content-api': { | ||
routes: [], | ||
}, | ||
}, | ||
}, | ||
}, | ||
api: { | ||
restaurant: { | ||
contentTypes: { | ||
restaurant: { | ||
attributes: { | ||
name: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
routes: { | ||
restaurant: { routes: [] }, | ||
}, | ||
}, | ||
}, | ||
contentType: () => ({ info: {}, attributes: { test: { type: 'string' } } }), | ||
}; | ||
|
||
module.exports = strapi; |
321 changes: 321 additions & 0 deletions
321
packages/plugins/documentation/__tests__/build-component-schema.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,321 @@ | ||
'use strict'; | ||
|
||
const buildComponentSchema = require('../server/services/helpers/build-component-schema'); | ||
const strapi = require('../__mocks__/strapi'); | ||
|
||
describe('Build Component Schema', () => { | ||
it('builds the Response schema', () => { | ||
global.strapi = strapi; | ||
|
||
const apiMocks = [ | ||
{ | ||
name: 'users-permissions', | ||
getter: 'plugin', | ||
ctNames: ['role'], | ||
}, | ||
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] }, | ||
]; | ||
|
||
let schemas; | ||
for (const mock of apiMocks) { | ||
schemas = { | ||
...schemas, | ||
...buildComponentSchema(mock), | ||
}; | ||
} | ||
|
||
const schemaLength = Object.keys(schemas).length; | ||
const [pluginResponseName, apiResponseName] = Object.keys(schemas); | ||
const [pluginResponseValue, apiResponseValue] = Object.values(schemas); | ||
|
||
const expectedShape = { | ||
properties: { | ||
data: { | ||
type: 'object', | ||
properties: { | ||
id: { type: 'string' }, | ||
attributes: { type: 'object', properties: { test: { type: 'string' } } }, | ||
}, | ||
}, | ||
meta: { type: 'object' }, | ||
}, | ||
}; | ||
|
||
expect(schemaLength).toBe(2); | ||
expect(pluginResponseName).toBe('UsersPermissionsRoleResponse'); | ||
expect(apiResponseName).toBe('RestaurantResponse'); | ||
expect(pluginResponseValue).toStrictEqual(expectedShape); | ||
expect(apiResponseValue).toStrictEqual(expectedShape); | ||
}); | ||
|
||
it('builds the ResponseList schema', () => { | ||
global.strapi = { | ||
...strapi, | ||
plugins: { | ||
'users-permissions': { | ||
...strapi.plugins['users-permissions'], | ||
routes: { | ||
'content-api': { routes: [{ method: 'GET', path: '/test', handler: 'test.find' }] }, | ||
}, | ||
}, | ||
}, | ||
api: { | ||
restaurant: { | ||
...strapi.api.restaurant, | ||
routes: { | ||
restaurant: { routes: [{ method: 'GET', path: '/test', handler: 'test.find' }] }, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const apiMocks = [ | ||
{ | ||
name: 'users-permissions', | ||
getter: 'plugin', | ||
ctNames: ['role'], | ||
}, | ||
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] }, | ||
]; | ||
|
||
let schemas; | ||
for (const mock of apiMocks) { | ||
schemas = { | ||
...schemas, | ||
...buildComponentSchema(mock), | ||
}; | ||
} | ||
|
||
const schemaLength = Object.keys(schemas).length; | ||
const schemaNames = Object.keys(schemas); | ||
const pluginListResponseValue = schemas['UsersPermissionsRoleListResponse']; | ||
const apiListResponseValue = schemas['RestaurantListResponse']; | ||
|
||
const expectedShape = { | ||
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' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
expect(schemaLength).toBe(4); | ||
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', () => { | ||
global.strapi = { | ||
...strapi, | ||
plugins: { | ||
'users-permissions': { | ||
...strapi.plugins['users-permissions'], | ||
routes: { | ||
'content-api': { routes: [{ method: 'POST', path: '/test', handler: 'test.create' }] }, | ||
}, | ||
}, | ||
}, | ||
api: { | ||
restaurant: { | ||
...strapi.api.restaurant, | ||
routes: { | ||
restaurant: { routes: [{ method: 'POST', path: '/test', handler: 'test.create' }] }, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const apiMocks = [ | ||
{ | ||
name: 'users-permissions', | ||
getter: 'plugin', | ||
ctNames: ['role'], | ||
}, | ||
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] }, | ||
]; | ||
|
||
let schemas; | ||
for (const mock of apiMocks) { | ||
schemas = { | ||
...schemas, | ||
...buildComponentSchema(mock), | ||
}; | ||
} | ||
|
||
const schemaLength = Object.keys(schemas).length; | ||
const schemaNames = Object.keys(schemas); | ||
const pluginListResponseValue = schemas['UsersPermissionsRoleRequest']; | ||
const apiListResponseValue = schemas['RestaurantRequest']; | ||
|
||
const expectedShape = { | ||
type: 'object', | ||
properties: { | ||
data: { | ||
type: 'object', | ||
properties: { test: { type: 'string' } }, | ||
}, | ||
}, | ||
}; | ||
|
||
expect(schemaLength).toBe(4); | ||
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', () => { | ||
global.strapi = { | ||
...strapi, | ||
plugins: { | ||
'users-permissions': { | ||
...strapi.plugins['users-permissions'], | ||
routes: { | ||
'content-api': { routes: [{ method: 'GET', path: '/localizations', handler: 'test' }] }, | ||
}, | ||
}, | ||
}, | ||
api: { | ||
restaurant: { | ||
...strapi.api.restaurant, | ||
routes: { | ||
restaurant: { routes: [{ method: 'GET', path: '/localizations', handler: 'test' }] }, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const apiMocks = [ | ||
{ | ||
name: 'users-permissions', | ||
getter: 'plugin', | ||
ctNames: ['role'], | ||
}, | ||
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] }, | ||
]; | ||
|
||
let schemas; | ||
for (const mock of apiMocks) { | ||
schemas = { | ||
...schemas, | ||
...buildComponentSchema(mock), | ||
}; | ||
} | ||
|
||
const schemaLength = Object.keys(schemas).length; | ||
const schemaNames = Object.keys(schemas); | ||
const pluginListResponseValue = schemas['UsersPermissionsRoleLocalizationResponse']; | ||
const apiListResponseValue = schemas['RestaurantLocalizationResponse']; | ||
|
||
const expectedShape = { | ||
type: 'object', | ||
properties: { | ||
id: { type: 'string' }, | ||
test: { type: 'string' }, | ||
}, | ||
}; | ||
|
||
expect(schemaLength).toBe(4); | ||
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', () => { | ||
global.strapi = { | ||
...strapi, | ||
plugins: { | ||
'users-permissions': { | ||
...strapi.plugins['users-permissions'], | ||
routes: { | ||
'content-api': { | ||
routes: [{ method: 'POST', path: '/localizations', handler: 'test' }], | ||
}, | ||
}, | ||
}, | ||
}, | ||
api: { | ||
restaurant: { | ||
...strapi.api.restaurant, | ||
routes: { | ||
restaurant: { routes: [{ method: 'POST', path: '/localizations', handler: 'test' }] }, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const apiMocks = [ | ||
{ | ||
name: 'users-permissions', | ||
getter: 'plugin', | ||
ctNames: ['role'], | ||
}, | ||
{ name: 'restaurant', getter: 'api', ctNames: ['restaurant'] }, | ||
]; | ||
|
||
let schemas; | ||
for (const mock of apiMocks) { | ||
schemas = { | ||
...schemas, | ||
...buildComponentSchema(mock), | ||
}; | ||
} | ||
|
||
const schemaLength = Object.keys(schemas).length; | ||
const schemaNames = Object.keys(schemas); | ||
const pluginListResponseValue = schemas['UsersPermissionsRoleLocalizationRequest']; | ||
const apiListResponseValue = schemas['RestaurantLocalizationRequest']; | ||
|
||
const expectedShape = { | ||
type: 'object', | ||
properties: { test: { type: 'string' } }, | ||
}; | ||
|
||
expect(schemaLength).toBe(8); | ||
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', () => { | ||
global.strapi = strapi; | ||
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'); | ||
}); | ||
}); |