tachybase_todo/packages/actions/src/__tests__/middleware.test.ts
Junyi 53729e188a
Refactor: change global injection of test for actions package. (#15)
* Refactor: change global injection to index.ts to simplify all test files.

* Fix: typo.
2020-11-16 20:38:56 +08:00

66 lines
1.4 KiB
TypeScript

import actions from '..';
import { Context } from '../actions';
import jsonReponse from '../middlewares/json-reponse';
import { initDatabase, agent, resourcer } from './index';
describe('list', () => {
let db;
beforeAll(async () => {
resourcer.define({
name: 'posts',
middlewares: [
jsonReponse,
],
actions: actions.common,
});
db = await initDatabase();
db.table({
name: 'posts',
tableName: 'actions__m__posts',
fields: [
{
type: 'string',
name: 'title',
},
{
type: 'string',
name: 'status',
defaultValue: 'publish',
}
],
scopes: {
customTitle: (title, ctx: Context) => {
return {
where: {
title: title,
},
}
},
}
});
await db.sync({
force: true,
});
});
afterAll(() => db.close());
it('create', async () => {
const response = await agent
.post('/posts')
.send({
title: 'title1',
});
expect(response.body.data.title).toBe('title1');
});
it('list', async () => {
const response = await agent.get('/posts?fields=title&page=1');
expect(response.body).toEqual({
data: [ { title: 'title1' } ],
meta: { count: 1, page: 1, per_page: 20 }
});
});
});