test: removeSchema breakComponent
This commit is contained in:
		
							parent
							
								
									0667efefb4
								
							
						
					
					
						commit
						f167bf90b4
					
				@ -155,7 +155,6 @@ describe('server hooks', () => {
 | 
			
		||||
      filter: {
 | 
			
		||||
        name: 'intro',
 | 
			
		||||
      },
 | 
			
		||||
      individualHooks: true,
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    const jsonTree = await uiSchemaRepository.getJsonSchema('grid1');
 | 
			
		||||
@ -163,6 +162,57 @@ describe('server hooks', () => {
 | 
			
		||||
    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 () => {
 | 
			
		||||
    await db.getRepository('collections').create({
 | 
			
		||||
      values: {
 | 
			
		||||
 | 
			
		||||
@ -237,6 +237,10 @@ export default class UiSchemaRepository extends Repository {
 | 
			
		||||
      },
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if (!parent) {
 | 
			
		||||
      return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const countResult = await db.sequelize.query(
 | 
			
		||||
      `SELECT COUNT(*) FROM ${
 | 
			
		||||
        db.getCollection('ui_schema_tree_path').model.tableName
 | 
			
		||||
@ -253,7 +257,13 @@ export default class UiSchemaRepository extends Repository {
 | 
			
		||||
    const parentChildrenCount = countResult[0]['count'];
 | 
			
		||||
 | 
			
		||||
    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;
 | 
			
		||||
@ -263,10 +273,12 @@ export default class UiSchemaRepository extends Repository {
 | 
			
		||||
    const { transaction, uid, breakComponent } = options;
 | 
			
		||||
 | 
			
		||||
    const removeParent = async (nodeUid: string) => {
 | 
			
		||||
      const parentUid = await this.isSingleChild(nodeUid, transaction);
 | 
			
		||||
      const parent = await this.isSingleChild(nodeUid, transaction);
 | 
			
		||||
 | 
			
		||||
      if (parentUid) {
 | 
			
		||||
        await removeParent(parentUid);
 | 
			
		||||
      const nodeComponentType = parent ? parent.get('x-component') : null;
 | 
			
		||||
 | 
			
		||||
      if ((parent && !breakComponent) || (parent && breakComponent != nodeComponentType)) {
 | 
			
		||||
        await removeParent(parent.get('uid') as string);
 | 
			
		||||
      } else {
 | 
			
		||||
        await this.remove(nodeUid, { transaction });
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user