diff --git a/packages/plugin-collections/src/__tests__/actions/collections.test.ts b/packages/plugin-collections/src/__tests__/actions/collections.test.ts new file mode 100644 index 000000000..8fad54c30 --- /dev/null +++ b/packages/plugin-collections/src/__tests__/actions/collections.test.ts @@ -0,0 +1,44 @@ +import { Agent, getAgent, getApp } from '..'; +import { Application } from '@nocobase/server'; +import { types } from '../../interfaces'; +jest.setTimeout(30000); +describe('collection hooks', () => { + let app: Application; + let agent: Agent; + + beforeEach(async () => { + app = await getApp(); + agent = getAgent(app); + }); + + afterEach(() => app.database.close()); + + it('create table', async () => { + const response = await agent.resource('collections').create({ + values: { + name: 'tests', + title: 'tests', + }, + }); + const table = app.database.getTable('tests'); + expect(table).toBeDefined(); + }); + + it('create table without name', async () => { + const response = await agent.resource('collections').create({ + values: { + title: 'tests', + }, + }); + + const { name } = response.body; + const table = app.database.getTable(name); + expect(table).toBeDefined(); + expect(table.getOptions().title).toBe('tests'); + + const list = await agent.resource('collections').list(); + expect(list.body.rows.length).toBe(1); + + await table.getModel().drop(); + }); +}); diff --git a/packages/plugin-collections/src/__tests__/collections.test.ts b/packages/plugin-collections/src/__tests__/collections.test.ts deleted file mode 100644 index 443436265..000000000 --- a/packages/plugin-collections/src/__tests__/collections.test.ts +++ /dev/null @@ -1,166 +0,0 @@ -import { Agent, getAgent, getApp } from '.'; -import { Application } from '@nocobase/server'; -import * as types from '../interfaces/types'; -jest.setTimeout(30000); -describe('collection hooks', () => { - let app: Application; - let agent: Agent; - - beforeEach(async () => { - app = await getApp(); - agent = getAgent(app); - }); - - afterEach(() => app.database.close()); - - it('import', async () => { - const tables = app.database.getTables([]); - for (const table of tables) { - const Collection = app.database.getModel('collections'); - await Collection.import(table.getOptions(), { migrate: false }); - } - }); - - it('create table', async () => { - const response = await agent.resource('collections').create({ - values: { - name: 'tests', - title: 'tests', - }, - }); - const table = app.database.getTable('tests'); - expect(table).toBeDefined(); - }); - - it('create table without name', async () => { - const created = await agent.resource('collections').create({ - values: { - title: 'tests', - }, - }); - - const { name } = created.body; - const table = app.database.getTable(name); - expect(table).toBeDefined(); - expect(table.getOptions().title).toBe('tests'); - - const list = await agent.resource('collections').list(); - expect(list.body.rows.length).toBe(1); - - await table.getModel().drop(); - }); - - it('list fields', async () => { - const response = await agent.resource('collections.fields').list({ - associatedKey: 'tests', - // values: { - // type: 'string', - // name: 'title', - // title: '标题', - // }, - }); - // console.log(response.body); - }); - - it('create field', async () => { - await agent.resource('collections').create({ - values: { - name: 'tests', - title: 'tests', - }, - }); - - await agent.resource('collections.fields').create({ - associatedKey: 'tests', - values: { - type: 'string', - name: 'name', - options: { - type: 'string', - name: 'name', - }, - }, - }); - - const table = app.database.getTable('tests'); - expect(table.getField('name')).toBeDefined(); - - const { body } = await agent.resource('tests').create({ - values: { name: 'a' } - }); - - expect(body.name).toBe('a'); - }); - - it('create field without name', async () => { - await agent.resource('collections').create({ - values: { - name: 'tests', - title: 'tests', - }, - }); - - const createdField = await agent.resource('collections.fields').create({ - associatedKey: 'tests', - values: { - type: 'string', - }, - }); - const { name: createdFieldName } = createdField.body; - - const table = app.database.getTable('tests'); - expect(table.getField(createdFieldName)).toBeDefined(); - - const createdRow = await agent.resource('tests').create({ - values: { [createdFieldName]: 'a' } - }); - - expect(createdRow.body[createdFieldName]).toBe('a'); - }); - - it('create string field by interface', async () => { - await agent.resource('collections').create({ - values: { - name: 'tests', - title: 'tests', - }, - }); - - const values = { - interface: 'string', - title: '名称', - name: 'name', - required: true, - viewable: true, - sortable: true, - filterable: true, - 'component.tooltip': 'test' - } - - const createdField = await agent.resource('collections.fields').create({ - associatedKey: 'tests', - values, - }); - - expect(createdField.body).toMatchObject({ - ...types['string'].options, - ...{ - interface: 'string', - title: '名称', - name: 'name', - required: true, - viewable: true, - sortable: true, - filterable: true, - }, - sort: 1, - collection_name: 'tests', - }); - - const gotField = await agent.resource('fields').get({ - resourceKey: createdField.body.id - }); - - expect(gotField.body).toEqual(createdField.body); - }); -}); diff --git a/packages/plugin-collections/src/__tests__/index.ts b/packages/plugin-collections/src/__tests__/index.ts index 7e2a4ea69..854ec6117 100644 --- a/packages/plugin-collections/src/__tests__/index.ts +++ b/packages/plugin-collections/src/__tests__/index.ts @@ -11,6 +11,7 @@ function getTestKey() { const { id } = require.main; const key = id .replace(`${process.env.PWD}/packages`, '') + .replace(/src\/__tests__/g, '') .replace('.test.ts', '') .replace(/[^\w]/g, '_') .replace(/_+/g, '_'); diff --git a/packages/plugin-collections/src/__tests__/models/collection.test.ts b/packages/plugin-collections/src/__tests__/models/collection.test.ts index 1887e4196..92296a18b 100644 --- a/packages/plugin-collections/src/__tests__/models/collection.test.ts +++ b/packages/plugin-collections/src/__tests__/models/collection.test.ts @@ -13,7 +13,15 @@ describe('models.collection', () => { afterEach(() => app.database.close()); - it('import', async () => { + it('import all tables', async () => { + const tables = app.database.getTables([]); + for (const table of tables) { + const Collection = app.database.getModel('collections'); + await Collection.import(table.getOptions(), { migrate: false }); + } + }); + + it('import examples', async () => { await app.database.getModel('collections').import({ title: '示例', name: 'examples',