From 24cf95b55322d5562de3dda4e99b493974de7675 Mon Sep 17 00:00:00 2001 From: npmstudy Date: Thu, 30 Nov 2023 17:26:05 +0800 Subject: [PATCH] chore: refact --- packages/core/src/__tests__/app.test.ts | 23 +++++++++++++ packages/core/src/__tests__/fn.test.ts | 12 +++---- packages/core/src/__tests__/utils.test.ts | 39 +++++++++++++++++++++++ packages/core/src/fn.ts | 10 +++--- packages/core/src/server.ts | 1 + packages/core/src/utils.ts | 3 +- 6 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 packages/core/src/__tests__/utils.test.ts diff --git a/packages/core/src/__tests__/app.test.ts b/packages/core/src/__tests__/app.test.ts index 95de2d0..4dbd6f7 100644 --- a/packages/core/src/__tests__/app.test.ts +++ b/packages/core/src/__tests__/app.test.ts @@ -39,6 +39,14 @@ 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'); @@ -46,6 +54,21 @@ describe('app', async () => { 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'); diff --git a/packages/core/src/__tests__/fn.test.ts b/packages/core/src/__tests__/fn.test.ts index 5e0df7d..313221e 100644 --- a/packages/core/src/__tests__/fn.test.ts +++ b/packages/core/src/__tests__/fn.test.ts @@ -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) { @@ -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 () => { @@ -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'); diff --git a/packages/core/src/__tests__/utils.test.ts b/packages/core/src/__tests__/utils.test.ts new file mode 100644 index 0000000..78f8c69 --- /dev/null +++ b/packages/core/src/__tests__/utils.test.ts @@ -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(); + }); +}); diff --git a/packages/core/src/fn.ts b/packages/core/src/fn.ts index fc9dfc3..9cfa67a 100644 --- a/packages/core/src/fn.ts +++ b/packages/core/src/fn.ts @@ -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) { @@ -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等开头的函数'); diff --git a/packages/core/src/server.ts b/packages/core/src/server.ts index 7916e6b..b1793de 100644 --- a/packages/core/src/server.ts +++ b/packages/core/src/server.ts @@ -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 diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index 8221243..f39d019 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -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() {