diff --git a/packages/core/test/src/client/__tests__/__snapshots__/omitSomeFields.test.ts.snap b/packages/core/test/src/client/__tests__/__snapshots__/omitSomeFields.test.ts.snap new file mode 100644 index 000000000..8da003a72 --- /dev/null +++ b/packages/core/test/src/client/__tests__/__snapshots__/omitSomeFields.test.ts.snap @@ -0,0 +1,32 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`omitSomeFields > should omit key & collectionName 1`] = ` +[ + { + "description": null, + "fields": [ + { + "allowNull": false, + "autoIncrement": true, + "description": null, + "interface": "id", + "name": "id", + "parentKey": null, + "primaryKey": true, + "reverseKey": null, + "type": "bigInt", + "uiSchema": { + "title": "{{t(\\"ID\\")}}", + "type": "number", + "x-component": "InputNumber", + "x-read-pretty": true, + }, + }, + ], + "hidden": false, + "inherit": false, + "name": "t_0a1w7khj0y7", + "title": "a", + }, +] +`; diff --git a/packages/core/test/src/client/__tests__/omitSomeFields.test.ts b/packages/core/test/src/client/__tests__/omitSomeFields.test.ts new file mode 100644 index 000000000..12fa22ae9 --- /dev/null +++ b/packages/core/test/src/client/__tests__/omitSomeFields.test.ts @@ -0,0 +1,39 @@ +import { omitSomeFields } from '../e2eUtils'; + +describe('omitSomeFields', () => { + test('should omit key & collectionName', () => { + const collections = [ + { + key: 'y7yd3v18l4t', + name: 't_0a1w7khj0y7', + title: 'a', + inherit: false, + hidden: false, + description: null, + fields: [ + { + key: '1anm4syjvjt', + name: 'id', + type: 'bigInt', + interface: 'id', + description: null, + collectionName: 't_0a1w7khj0y7', + parentKey: null, + reverseKey: null, + autoIncrement: true, + primaryKey: true, + allowNull: false, + uiSchema: { + type: 'number', + title: '{{t("ID")}}', + 'x-component': 'InputNumber', + 'x-read-pretty': true, + }, + }, + ], + }, + ]; + + expect(omitSomeFields(collections)).toMatchSnapshot(); + }); +}); diff --git a/packages/core/test/src/client/e2eUtils.ts b/packages/core/test/src/client/e2eUtils.ts index ac69b7c85..d362040a0 100644 --- a/packages/core/test/src/client/e2eUtils.ts +++ b/packages/core/test/src/client/e2eUtils.ts @@ -129,7 +129,7 @@ class NocoPage { async init() { if (this.options?.collections?.length) { - const collections: any = deleteKeyOfCollection(this.options.collections); + const collections: any = omitSomeFields(this.options.collections); this.collectionsName = collections.map((item) => item.name); await createCollections(collections); @@ -191,7 +191,9 @@ export const test = base.extend<{ let collectionsName = []; const _createCollections = async (collectionSettings: CollectionSetting | CollectionSetting[]) => { - collectionSettings = Array.isArray(collectionSettings) ? collectionSettings : [collectionSettings]; + collectionSettings = omitSomeFields( + Array.isArray(collectionSettings) ? collectionSettings : [collectionSettings], + ); collectionsName = collectionSettings.map((item) => item.name); await createCollections(collectionSettings); }; @@ -356,15 +358,15 @@ const deleteCollections = async (collectionNames: string[]) => { }; /** - * 如果不删除 key 会报错 + * 删除一些不需要的字段,如 key * @param collectionSettings * @returns */ -const deleteKeyOfCollection = (collectionSettings: CollectionSetting[]) => { +export const omitSomeFields = (collectionSettings: CollectionSetting[]): any[] => { return collectionSettings.map((collection) => { return { ..._.omit(collection, ['key']), - fields: collection.fields.map((field) => _.omit(field, ['key'])), + fields: collection.fields.map((field) => _.omit(field, ['key', 'collectionName'])), }; }); };