mirror of
https://github.com/strapi/strapi.git
synced 2025-08-10 17:58:07 +00:00
Merge pull request #11749 from strapi/fix/cli-ct-attribute-generator
Fix the attribute generation for the CT generator
This commit is contained in:
commit
2c49c531ba
3
packages/generators/generators/.gitignore
vendored
3
packages/generators/generators/.gitignore
vendored
@ -8,3 +8,6 @@ package-lock.json
|
||||
.DS_Store
|
||||
npm-debug.log
|
||||
.idea
|
||||
|
||||
# Tests related folders
|
||||
**/__tests__/output
|
||||
|
@ -0,0 +1,179 @@
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const { readFile, rm, stat } = require('fs-extra');
|
||||
const strapiGenerators = require('../../index');
|
||||
|
||||
describe('Content Type Generator', () => {
|
||||
const outputDirectory = path.join(__dirname, 'output');
|
||||
|
||||
afterEach(async () => {
|
||||
await rm(outputDirectory, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test('it generates the schema', async () => {
|
||||
await strapiGenerators.generate(
|
||||
'content-type',
|
||||
{
|
||||
displayName: 'testContentType',
|
||||
singularName: 'testContentType',
|
||||
pluralName: 'testContentTypes',
|
||||
kind: 'singleType',
|
||||
id: 'testContentType',
|
||||
useDraftAndPublish: false,
|
||||
destination: 'new',
|
||||
bootstrapApi: false,
|
||||
attributes: [],
|
||||
},
|
||||
{ dir: outputDirectory }
|
||||
);
|
||||
|
||||
const generatedSchemaPath = path.join(
|
||||
outputDirectory,
|
||||
'src/api/testContentType',
|
||||
'content-types/testContentType/schema.json'
|
||||
);
|
||||
|
||||
expect((await stat(generatedSchemaPath)).isFile()).toBeTruthy();
|
||||
|
||||
const fileContent = await readFile(generatedSchemaPath, 'utf-8');
|
||||
|
||||
expect(fileContent).not.toBeNull();
|
||||
|
||||
const schema = JSON.parse(fileContent.toString());
|
||||
|
||||
expect(schema).toStrictEqual({
|
||||
kind: 'singleType',
|
||||
collectionName: 'test_content_types',
|
||||
info: {
|
||||
singularName: 'testContentType',
|
||||
pluralName: 'testContentTypes',
|
||||
displayName: 'testContentType',
|
||||
},
|
||||
options: {
|
||||
draftAndPublish: false,
|
||||
comment: '',
|
||||
},
|
||||
attributes: {},
|
||||
});
|
||||
});
|
||||
|
||||
test('it scaffolds a new API', async () => {
|
||||
await strapiGenerators.generate(
|
||||
'content-type',
|
||||
{
|
||||
displayName: 'testContentType',
|
||||
singularName: 'testContentType',
|
||||
pluralName: 'testContentTypes',
|
||||
kind: 'singleType',
|
||||
id: 'testContentType',
|
||||
useDraftAndPublish: false,
|
||||
destination: 'new',
|
||||
bootstrapApi: true,
|
||||
attributes: [],
|
||||
},
|
||||
{ dir: outputDirectory }
|
||||
);
|
||||
const generatedApiPath = path.join(outputDirectory, 'src/api/testContentType');
|
||||
|
||||
expect((await stat(generatedApiPath)).isDirectory()).toBeTruthy();
|
||||
expect(
|
||||
(await stat(path.join(generatedApiPath, 'controllers/testContentType.js'))).isFile()
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
(await stat(path.join(generatedApiPath, 'services/testContentType.js'))).isFile()
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
(await stat(path.join(generatedApiPath, 'routes/testContentType.js'))).isFile()
|
||||
).toBeTruthy();
|
||||
|
||||
const controller = await readFile(
|
||||
path.join(generatedApiPath, 'controllers/testContentType.js')
|
||||
);
|
||||
const router = await readFile(path.join(generatedApiPath, 'routes/testContentType.js'));
|
||||
const service = await readFile(path.join(generatedApiPath, 'services/testContentType.js'));
|
||||
|
||||
expect(controller.toString()).toMatchInlineSnapshot(`
|
||||
"'use strict';
|
||||
|
||||
/**
|
||||
* testContentType controller
|
||||
*/
|
||||
|
||||
const { createCoreController } = require('@strapi/strapi').factories;
|
||||
|
||||
module.exports = createCoreController('api::testContentType.testContentType');
|
||||
"
|
||||
`);
|
||||
expect(router.toString()).toMatchInlineSnapshot(`
|
||||
"'use strict';
|
||||
|
||||
/**
|
||||
* testContentType router.
|
||||
*/
|
||||
|
||||
const { createCoreRouter } = require('@strapi/strapi').factories;
|
||||
|
||||
module.exports = createCoreRouter('api::testContentType.testContentType');
|
||||
"
|
||||
`);
|
||||
expect(service.toString()).toMatchInlineSnapshot(`
|
||||
"'use strict';
|
||||
|
||||
/**
|
||||
* testContentType service.
|
||||
*/
|
||||
|
||||
const { createCoreService } = require('@strapi/strapi').factories;
|
||||
|
||||
module.exports = createCoreService('api::testContentType.testContentType');
|
||||
"
|
||||
`);
|
||||
});
|
||||
|
||||
test('it generates the schema, then adds the attributes', async () => {
|
||||
await strapiGenerators.generate(
|
||||
'content-type',
|
||||
{
|
||||
displayName: 'testContentType',
|
||||
singularName: 'testContentType',
|
||||
pluralName: 'testContentTypes',
|
||||
kind: 'singleType',
|
||||
id: 'testContentType',
|
||||
useDraftAndPublish: false,
|
||||
destination: 'new',
|
||||
bootstrapApi: false,
|
||||
attributes: [
|
||||
{
|
||||
attributeName: 'name',
|
||||
attributeType: 'string',
|
||||
},
|
||||
{
|
||||
attributeName: 'email',
|
||||
attributeType: 'string',
|
||||
},
|
||||
],
|
||||
},
|
||||
{ dir: outputDirectory }
|
||||
);
|
||||
|
||||
const generatedSchemaPath = path.join(
|
||||
outputDirectory,
|
||||
'src/api/testContentType',
|
||||
'content-types/testContentType/schema.json'
|
||||
);
|
||||
|
||||
expect((await stat(generatedSchemaPath)).isFile()).toBeTruthy();
|
||||
|
||||
const fileContent = await readFile(generatedSchemaPath, 'utf-8');
|
||||
|
||||
expect(fileContent).not.toBeNull();
|
||||
|
||||
const schema = JSON.parse(fileContent.toString());
|
||||
|
||||
expect(schema.attributes).toStrictEqual({
|
||||
email: { type: 'string' },
|
||||
name: { type: 'string' },
|
||||
});
|
||||
});
|
||||
});
|
@ -93,7 +93,7 @@ module.exports = plop => {
|
||||
},
|
||||
];
|
||||
|
||||
if (attributes.lenght > 0) {
|
||||
if (Object.entries(attributes).length > 0) {
|
||||
baseActions.push({
|
||||
type: 'modify',
|
||||
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
||||
|
Loading…
x
Reference in New Issue
Block a user