tachybase_todo/packages/core/test/src/mockServer.ts
ChengLei Shao 24ea83f0ff
Feat/create nocobase app (#273)
* create-nocobase-app template from [develop]

* change create-nocobase-app package.json config

* feat: load configuration from directory

* feat: configuration repository toObject

* feat: create application from configuration dir

* feat: application factory with plugins options

* export type

* feat: read application config &  application with plugins options

* feat: release command

* fix: database release

* chore: workflow package.json

* feat: nocobase cli package

* feat: console command

* chore: load application in command

* fix: load packages from process.cwd

* feat: cli load env file

* feat: create-nocobase-app

* fix: gitignore create-nocobase-app lib

* fix: sqlite path

* feat: create plugin

* chore: plugin files template

* chore: move cli into application

* chore: create-nocobase-app

* fix: create plugin

* chore: app-client && app-server

* chore: package.json

* feat: create-nocobase-app download template from npm

* chore: create-nocobase-app template

* fix: config of plugin-users

* fix: yarn.lock

* fix: database build error

* fix: yarn.lock

* fix: resourcer config

* chore: cross-env

* chore: app-client dependents

* fix: env

* chore: v0.6.0-alpha.1

* chore: verdaccio

* chore(versions): 😊 publish v0.6.0

* chore(versions): 😊 publish v0.6.1-alpha.0

* chore(versions): 😊 publish v0.6.2-alpha.0

* chore(versions): 😊 publish v0.6.2-alpha.1

* chore: 0.6.2-alpha.2

* feat: workspaces

* chore(versions): 😊 publish v0.6.2-alpha.3

* chore(versions): 😊 publish v0.6.2-alpha.4

* chore: create-nocobase-app

* chore: create-nocobase-app lib

* fix: update tsconfig.jest.json

* chore: .env

* chore(versions): 😊 publish v0.6.2-alpha.5

* chore(versions): 😊 publish v0.6.2-alpha.6

* feat: improve code

* chore(versions): 😊 publish v0.6.2-alpha.7

* fix: cleanup

* chore(versions): 😊 publish v0.6.2-alpha.8

* chore: tsconfig for app server package

* fix: move files

* fix: move files

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-04-17 10:00:42 +08:00

149 lines
4.1 KiB
TypeScript

import { Database, mockDatabase } from '@nocobase/database';
import Application, { ApplicationOptions } from '@nocobase/server';
import qs from 'qs';
import supertest, { SuperAgentTest } from 'supertest';
interface ActionParams {
filterByTk?: any;
fields?: string[];
filter?: any;
sort?: string[];
page?: number;
pageSize?: number;
values?: any;
/**
* @deprecated
*/
resourceName?: string;
/**
* @deprecated
*/
resourceIndex?: string;
/**
* @deprecated
*/
associatedName?: string;
/**
* @deprecated
*/
associatedIndex?: string;
[key: string]: any;
}
interface SortActionParams {
resourceName?: string;
resourceIndex?: any;
associatedName?: string;
associatedIndex?: any;
sourceId?: any;
targetId?: any;
sortField?: string;
method?: string;
target?: any;
sticky?: boolean;
[key: string]: any;
}
interface Resource {
get: (params?: ActionParams) => Promise<supertest.Response>;
list: (params?: ActionParams) => Promise<supertest.Response>;
create: (params?: ActionParams) => Promise<supertest.Response>;
update: (params?: ActionParams) => Promise<supertest.Response>;
destroy: (params?: ActionParams) => Promise<supertest.Response>;
sort: (params?: SortActionParams) => Promise<supertest.Response>;
[name: string]: (params?: ActionParams) => Promise<supertest.Response>;
}
export class MockServer extends Application {
async loadAndInstall() {
await this.load();
await this.install({
sync: {
force: true,
alter: {
drop: false,
},
},
});
}
async cleanDb() {
await this.db.sequelize.getQueryInterface().dropAllTables();
}
agent(): SuperAgentTest & { resource: (name: string, resourceOf?: any) => Resource } {
const agent = supertest.agent(this.appManager.callback());
const prefix = this.resourcer.options.prefix;
const proxy = new Proxy(agent, {
get(target, method: string, receiver) {
if (method === 'resource') {
return (name: string, resourceOf?: any) => {
const keys = name.split('.');
const proxy = new Proxy(
{},
{
get(target, method: string, receiver) {
return (params: ActionParams = {}) => {
let { filterByTk, values = {}, file, ...restParams } = params;
if (params.associatedIndex) {
resourceOf = params.associatedIndex;
}
if (params.resourceIndex) {
filterByTk = params.resourceIndex;
}
let url = prefix || '';
if (keys.length > 1) {
url += `/${keys[0]}/${resourceOf}/${keys[1]}`;
} else {
url += `/${name}`;
}
url += `:${method as string}`;
if (filterByTk) {
url += `/${filterByTk}`;
}
const queryString = qs.stringify(restParams, { arrayFormat: 'brackets' });
switch (method) {
case 'upload':
return agent.post(`${url}?${queryString}`).attach('file', file).field(values);
case 'list':
case 'get':
return agent.get(`${url}?${queryString}`);
default:
return agent.post(`${url}?${queryString}`).send(values);
}
};
},
},
);
return proxy;
};
}
return (...args: any[]) => {
return agent[method](...args);
};
},
});
return proxy as any;
}
}
export function mockServer(options: ApplicationOptions = {}) {
let database;
if (options?.database instanceof Database) {
database = options.database;
} else {
database = mockDatabase(<any>options?.database || {});
}
return new MockServer({
...options,
database,
});
}
export function createMockServer() {}
export default mockServer;