refactor: collections test cases

This commit is contained in:
chenos 2020-12-13 17:17:41 +08:00
parent a7c9461cc0
commit d03f927bc1
4 changed files with 54 additions and 167 deletions

View File

@ -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();
});
});

View File

@ -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);
});
});

View File

@ -11,6 +11,7 @@ function getTestKey() {
const { id } = require.main; const { id } = require.main;
const key = id const key = id
.replace(`${process.env.PWD}/packages`, '') .replace(`${process.env.PWD}/packages`, '')
.replace(/src\/__tests__/g, '')
.replace('.test.ts', '') .replace('.test.ts', '')
.replace(/[^\w]/g, '_') .replace(/[^\w]/g, '_')
.replace(/_+/g, '_'); .replace(/_+/g, '_');

View File

@ -13,7 +13,15 @@ describe('models.collection', () => {
afterEach(() => app.database.close()); 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({ await app.database.getModel('collections').import({
title: '示例', title: '示例',
name: 'examples', name: 'examples',