tachybase_todo/packages/plugin-collection-manager/src/__tests__/fields/reverseField.test.ts
chenos 380b5e8c7a
Feat/plugin collection manager (#147)
* refactor: collection manager plugin

* feat(database): magic attribute model

* MagicAttributeModel

* load collections & fields options

* collections filterTargetKey
2022-01-19 10:02:52 +08:00

60 lines
1.4 KiB
TypeScript

import Database, { Collection as DBCollection, StringFieldOptions } from '@nocobase/database';
import Application from '@nocobase/server';
import { createApp } from '..';
describe('reverseField options', () => {
let db: Database;
let app: Application;
let Collection: DBCollection;
let Field: DBCollection;
beforeEach(async () => {
app = await createApp();
await app.db.sync();
db = app.db;
Collection = db.getCollection('collections');
Field = db.getCollection('fields');
await Collection.repository.create({
values: {
name: 'tests',
},
});
await Collection.repository.create({
values: {
name: 'targets',
},
});
});
afterEach(async () => {
await app.destroy();
});
it('reverseField', async () => {
const field = await Field.repository.create({
values: {
type: 'hasMany',
collectionName: 'tests',
target: 'targets',
reverseField: {},
},
});
const json = JSON.parse(JSON.stringify(field.toJSON()));
expect(json).toMatchObject({
type: 'hasMany',
collectionName: 'tests',
target: 'targets',
targetKey: 'id',
sourceKey: 'id',
reverseField: {
type: 'belongsTo',
collectionName: 'targets',
target: 'tests',
targetKey: 'id',
sourceKey: 'id',
},
});
expect(json.foreignKey).toBe(json.reverseField.foreignKey);
});
});