test: update reverseField (#231)

* test: update reverseField

* fix: reverseField test

* feat: throw error when reverseField exists
This commit is contained in:
ChengLei Shao 2022-03-11 10:08:58 +08:00 committed by GitHub
parent a28b69326b
commit accb2a59b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 2 deletions

View File

@ -9,7 +9,7 @@ import {
ModelCtor,
Op,
Transaction,
UpdateOptions as SequelizeUpdateOptions
UpdateOptions as SequelizeUpdateOptions,
} from 'sequelize';
import { Collection } from './collection';
import { Database } from './database';

View File

@ -56,4 +56,78 @@ describe('reverseField options', () => {
});
expect(json.foreignKey).toBe(json.reverseField.foreignKey);
});
it('should update reverseField', async () => {
const field = await Field.repository.create({
values: {
type: 'hasMany',
collectionName: 'tests',
target: 'targets',
reverseField: {},
},
});
expect(
await Field.repository.count({
filter: {
collectionName: 'targets',
},
}),
).toEqual(1);
let reverseField = await Field.repository.findOne({
filter: {
collectionName: 'targets',
},
});
let err;
try {
await Field.repository.update({
filterByTk: field.get('key') as string,
values: {
reverseField: {
uiSchema: {
title: '123',
},
},
},
});
} catch (e) {
err = e;
}
expect(err).toBeDefined();
await Field.repository.update({
filterByTk: field.get('key') as string,
values: {
reverseField: {
key: reverseField.get('key'),
uiSchema: {
title: '123',
},
},
},
});
expect(
await Field.repository.count({
filter: {
collectionName: 'targets',
},
}),
).toEqual(1);
reverseField = await db.getRepository('fields').findOne({
filter: {
key: reverseField.get('key'),
},
appends: ['uiSchema'],
});
const uiSchema = reverseField.get('uiSchema');
expect(uiSchema['schema']).toEqual({ title: '123' });
});
});

View File

@ -1,10 +1,13 @@
import { mockServer } from '@nocobase/test';
import PluginUiSchema from '@nocobase/plugin-ui-schema-storage';
import CollectionManagerPlugin from '..';
export async function createApp() {
const app = mockServer();
await app.cleanDb();
app.plugin(CollectionManagerPlugin);
app.plugin(PluginUiSchema);
await app.load();
return app;
}

View File

@ -5,9 +5,10 @@ import {
afterCreateForReverseField,
beforeCreateForChildrenCollection,
beforeCreateForReverseField,
beforeInitOptions
beforeInitOptions,
} from './hooks';
import { CollectionModel, FieldModel } from './models';
import lodash from 'lodash';
export class CollectionManagerPlugin extends Plugin {
async beforeLoad() {
@ -20,6 +21,17 @@ export class CollectionManagerPlugin extends Plugin {
CollectionRepository,
});
this.app.db.on('fields.beforeUpdate', async (model, options) => {
const newValue = options.values;
if (
model.get('reverseKey') &&
lodash.get(newValue, 'reverseField') &&
!lodash.get(newValue, 'reverseField.key')
) {
throw new Error('cant update field without a reverseField key');
}
});
// 要在 beforeInitOptions 之前处理
this.app.db.on('fields.beforeCreate', beforeCreateForReverseField(this.app.db));
this.app.db.on('fields.beforeCreate', beforeCreateForChildrenCollection(this.app.db));