Skip to content

Commit

Permalink
chore: refact
Browse files Browse the repository at this point in the history
  • Loading branch information
npmstudy committed Nov 30, 2023
1 parent ec91697 commit 24cf95b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 13 deletions.
23 changes: 23 additions & 0 deletions packages/core/src/__tests__/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,36 @@ describe('app', async () => {

const request = supertest(rpc.callback());

it('should start === rpc.callback', async () => {
const request2 = supertest(rpc.start(3000));
const res = await request2.get('/api/a?$p=["hello"]');
expect(res.type).toEqual('application/json');
expect(res.status).toEqual(200);
expect(res.body).toEqual({ a: 'hello' });
});

it('should access /a', async () => {
const res = await request.get('/api/a?$p=["hello"]');
expect(res.type).toEqual('application/json');
expect(res.status).toEqual(200);
expect(res.body).toEqual({ a: 'hello' });
});

it('should access no routers[key]', async () => {
// if (!['POST', 'PUT', 'PATCH'].includes(ctx.method) && !ctx.query.$p) {
const res = await request.get('/api/a123');
expect(res.type).toEqual('text/plain');
expect(res.status).toEqual(200);
expect(res.text).toEqual('not match $p param, no process');
});

it('should access no routers[key]', async () => {
const res = await request.get('/api/a123?$p=["hello"]');
expect(res.type).toEqual('text/plain');
expect(res.status).toEqual(200);
expect(res.text).toEqual('[fn plugin] no fn repsonse, please check your fn if exist');
});

it('should access /add', async () => {
const res = await request.get('/api/add?$p=["hello"]');
expect(res.type).toEqual('application/json');
Expand Down
12 changes: 5 additions & 7 deletions packages/core/src/__tests__/fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('app', async () => {
expect(res.body).toEqual({ a: 'hello' });
});

// it('use fn add a function', async () => {
// it('use fn add a get function', async () => {
// const fn = new Fn({ prefix: '/apk' });

// fn.fn('/postAbc', function (a) {
Expand All @@ -81,12 +81,10 @@ describe('app', async () => {
// const request = supertest(fn.app.callback());

// const res = await request.post('/postAbc').send(['hello']).set('Accept', 'application/json');

// console.dir(res);

// expect(res.type).toEqual('application/json');
// // console.dir(res);
// expect(res.type).toEqual('text/plain');
// expect(res.status).toEqual(200);
// expect(res.body).toEqual({ a: 'hello' });
// expect(res.text).toEqual('process fn:Abclock , you need send lock request from client');
// });

it('use fn add a lock function', async () => {
Expand All @@ -102,7 +100,7 @@ describe('app', async () => {
const request = supertest(fn.app.callback());

const res = await request.get('/Abclock?$p=["hello"]');

// console.dir(res);
expect(res.type).toEqual('text/plain');
expect(res.status).toEqual(200);
expect(res.text).toEqual('process fn:Abclock , you need send lock request from client');
Expand Down
39 changes: 39 additions & 0 deletions packages/core/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it, vi } from 'vitest';

import { isArrowFunction, log } from '../index';

describe('app', async () => {
it('should render lib', () => {
expect('lib').toBe('lib');
});

it('isArrowFunction = true', () => {
const a = (b) => {
return b;
};
const result = isArrowFunction(a);

expect(result).toBe(true);
});

it('isArrowFunction = false', () => {
const a = function (b) {
return b;
};
const result = isArrowFunction(a);

expect(result).toBe(false);
});

it('isArrowFunction = false', () => {
const result = isArrowFunction('a');

expect(result).toBe(false);
});

it('mock log', () => {
const spy = vi.spyOn(console, 'log');
log('2323', true);
expect(spy).toHaveBeenCalled();
});
});
10 changes: 5 additions & 5 deletions packages/core/src/fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export class Fn extends Plugable {

const lastKey = key.split('.').pop();
const httpMethods = getHttpMethods();
console.dir('lastKey');
console.dir(lastKey);
// console.dir('lastKey');
// console.dir(lastKey);

const supportMethods = [];
httpMethods.forEach(function (m) {
Expand All @@ -110,9 +110,9 @@ export class Fn extends Plugable {
}
});

console.dir('supportMethods');
console.dir(ctx.method);
console.dir(supportMethods);
// console.dir('supportMethods');
// console.dir(ctx.method);
// console.dir(supportMethods);

if (supportMethods.length === 0) {
log(ctx.path + ',没有匹配到包含get/post等开头的函数');
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export class RpcServer {
/**
* Start @tomrpc/core server with port
*/
/* v8 ignore next 3 */
public start(port?: number) {
log('start');
// make app ready
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export const isArrowFunction = (func) => {
if (typeof func === 'function') {
const source = func.toString();
return /^\([^)]*\)\s*=>/.test(source);
} else {
return false;
}
return false;
};

export function getHttpMethods() {
Expand Down

0 comments on commit 24cf95b

Please sign in to comment.