test: removeSchema breakComponent

This commit is contained in:
Chareice 2022-02-10 21:06:22 +08:00 committed by chenos
parent 0667efefb4
commit f167bf90b4
2 changed files with 67 additions and 5 deletions

View File

@ -155,7 +155,6 @@ describe('server hooks', () => {
filter: { filter: {
name: 'intro', name: 'intro',
}, },
individualHooks: true,
}); });
const jsonTree = await uiSchemaRepository.getJsonSchema('grid1'); const jsonTree = await uiSchemaRepository.getJsonSchema('grid1');
@ -163,6 +162,57 @@ describe('server hooks', () => {
expect(jsonTree['properties']['row1']['properties']['col12']).not.toBeDefined(); expect(jsonTree['properties']['row1']['properties']['col12']).not.toBeDefined();
}); });
it('should works with breakComponent', async () => {
await db.getRepository('collections').create({
values: {
name: 'posts',
},
});
const schema = {
'x-uid': 'root',
name: 'root',
properties: {
grid: {
properties: {
row: {
'x-component': 'row',
properties: {
col: {
'x-component': 'col',
'x-uid': 'col',
'x-server-hooks': [
{
type: 'onCollectionDestroy',
collection: 'posts',
method: 'removeSchema',
params: {
breakComponent: 'row',
removeEmptyParents: true,
},
},
],
},
},
},
},
},
},
};
await uiSchemaRepository.insert(schema);
await db.getRepository('collections').destroy({
filter: {
name: 'posts',
},
});
const jsonTree = await uiSchemaRepository.getJsonSchema('root');
expect(jsonTree['properties']['grid']['properties']['row']).toBeDefined();
expect(jsonTree['properties']['grid']['properties']['row']['properties']).not.toBeDefined();
});
it('should remove schema when collection destroy', async () => { it('should remove schema when collection destroy', async () => {
await db.getRepository('collections').create({ await db.getRepository('collections').create({
values: { values: {

View File

@ -237,6 +237,10 @@ export default class UiSchemaRepository extends Repository {
}, },
}); });
if (!parent) {
return null;
}
const countResult = await db.sequelize.query( const countResult = await db.sequelize.query(
`SELECT COUNT(*) FROM ${ `SELECT COUNT(*) FROM ${
db.getCollection('ui_schema_tree_path').model.tableName db.getCollection('ui_schema_tree_path').model.tableName
@ -253,7 +257,13 @@ export default class UiSchemaRepository extends Repository {
const parentChildrenCount = countResult[0]['count']; const parentChildrenCount = countResult[0]['count'];
if (parentChildrenCount == 1) { if (parentChildrenCount == 1) {
return parent.get('ancestor') as string; const schema = await db.getRepository('ui_schemas').findOne({
filter: {
uid: parent.get('ancestor') as string,
},
});
return schema;
} }
return null; return null;
@ -263,10 +273,12 @@ export default class UiSchemaRepository extends Repository {
const { transaction, uid, breakComponent } = options; const { transaction, uid, breakComponent } = options;
const removeParent = async (nodeUid: string) => { const removeParent = async (nodeUid: string) => {
const parentUid = await this.isSingleChild(nodeUid, transaction); const parent = await this.isSingleChild(nodeUid, transaction);
if (parentUid) { const nodeComponentType = parent ? parent.get('x-component') : null;
await removeParent(parentUid);
if ((parent && !breakComponent) || (parent && breakComponent != nodeComponentType)) {
await removeParent(parent.get('uid') as string);
} else { } else {
await this.remove(nodeUid, { transaction }); await this.remove(nodeUid, { transaction });
} }