From ef081243164c1baf480fef7ba191a18f8d176e8d Mon Sep 17 00:00:00 2001 From: icebox1234 <464752812@qq.com> Date: Fri, 19 Aug 2022 14:06:08 +0800 Subject: [PATCH 1/3] feat: complete test --- lib/db.js | 46 ++++++++-- package.json | 1 + test/test.js | 244 ++++++++++++++++++++------------------------------- 3 files changed, 134 insertions(+), 157 deletions(-) diff --git a/lib/db.js b/lib/db.js index a1fe32c..76a1f4b 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,13 +1,45 @@ -const Tapable = require('tapable') +const { + SyncWaterfallHook, + AsyncSeriesWaterfallHook +} = require('tapable'); -class DB extends Tapable { - constructor() { - // TODO + + +class DB { + constructor(options) { + this.options = options || {}; + this.hooks = { + endpoint: new AsyncSeriesWaterfallHook(['options']), + options: new SyncWaterfallHook(['options']), + judge: new SyncWaterfallHook(['res']) + } + } + + useExtendOptions(options) { + const pluginOptions = this.hooks.options.call(this.options); + Object.keys(pluginOptions).forEach(item => { + if (pluginOptions.hasOwnProperty(item)) { + options[item] = pluginOptions[item]; + } + }); + return options; } - request() { - // TODO + request(options) { + const extendsOptions = this.useExtendOptions(options); + return this.hooks.endpoint.promise(extendsOptions) + .then( + res => { + if (!this.hooks.judge.call(res)) { + return Promise.reject(res); + } + return res; + } + ) + .catch(err => { + return Promise.reject(err); + }); } } -module.exports = DB \ No newline at end of file +module.exports = DB; \ No newline at end of file diff --git a/package.json b/package.json index 94bd5ff..0c9b8b6 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "karma-mocha": "^1.3.0", "karma-webpack": "^2.0.4", "mocha": "^3.5.0", + "tapable": "^1.1.3", "webpack": "^3.5.3" } } diff --git a/test/test.js b/test/test.js index f8fecbb..0d43424 100644 --- a/test/test.js +++ b/test/test.js @@ -1,9 +1,9 @@ -let DB = require('../lib/db') +let DB = require('../lib/db'); // just for the real answer, please ignore // if (!DB.prototype.request) { // DB = require('../lib/.db') // } -const assert = require('assert') +const assert = require('assert'); describe('DB', function () { it('可以设置options', function () { @@ -13,21 +13,14 @@ describe('DB', function () { }) it('可以设置endpoint插件,使得该请求用制定的方式处理', function (done) { - class XX extends DB { - constructor(options) { - super(options) - - this.plugin('endpoint', function () { - return new Promise((resolve) => { - setTimeout(() => { - resolve({ retcode: 0, res: { msg: 'hello world' } }) - }, 0) - }) - }) - } - } - - const xx = new XX() + const xx = new DB(); + xx.hooks.endpoint.tapPromise('endpointPlugin1', () => { + return new Promise((resolve) => { + setTimeout(() => { + resolve({ retcode: 0, res: { msg: 'hello world' } }); + }, 0); + }) + }); xx.request() .then((res) => { assert.equal(res.res.msg, 'hello world') @@ -36,31 +29,19 @@ describe('DB', function () { }) it('可以根据不同的options,使用不同的endpoint', function (done) { - class AA extends DB { - constructor(options) { - super(options) - this.plugin('endpoint', function (options) { - if (options.type === 1) { - return new Promise((resolve) => { - setTimeout(() => { - resolve({ retcode: 1, msg: 'logout' }) - }, 0) - }) - } - }) - this.plugin('endpoint', function (options) { - if (options.type === 0) { - return new Promise((resolve) => { - setTimeout(() => { - resolve({ retcode: 0, res: { msg: 'hello world' } }) - }, 0) - }) - } - }) - } - } - - const aa = new AA + const aa = new DB(); + aa.hooks.endpoint.tapPromise('endpointPlugin2', (options) => { + return new Promise((resolve) => { + if (options.type === 1) { + setTimeout(() => { + resolve({ retcode: 1 }); + }, 0); + } + if (options.type === 0) { + resolve({ retcode: 0 }); + } + }); + }); // 如果 options.type === 1,则返回第一个答案 aa.request({ type: 1 }) .then(res => { @@ -74,74 +55,52 @@ describe('DB', function () { }) it('可以设置options插件来处理options', function (done) { - class YY extends DB { - constructor(options) { - super(options) - this.plugin('options', (options) => { - // modify options - options.flag = true - return options - }) - this.plugin('endpoint', (options) => { - // init - assert.equal(options.init, true) - // merge - assert.equal(options.url, 'my://hello') - // options plugin modify - assert.equal(options.flag, true) - - return new Promise((resolve, reject) => { - setTimeout(() => { - resolve({ retcode: 0, res: { msg: 'hello world' } }) - }, 0) - }) - }) - } - } - - const yy = new YY({ init: true }) + const yy = new DB({ init: true }); + yy.hooks.options.tap('optionsPlugin1', (options) => { + options.flag = true; + return options; + }); + yy.hooks.endpoint.tapPromise('endpointPlugin3', (options) => { + assert.equal(options.init, true); + assert.equal(options.url, 'my://hello'); + assert.equal(options.flag, true); + return new Promise((resolve) => { + setTimeout(() => { + resolve({ retcode: 0, res: { msg: 'hello world' } }) + }, 0) + }); + }) yy.request({ url: 'my://hello' }) .then((res) => { + assert.equal(res.res.msg, 'hello world'); done() - }) + }); }) it('可以设置多个options插件', function (done) { - class BB extends DB { - constructor(options) { - super(options) - this.plugin('options', (options) => { - // modify options - options.flag = true - return options - }) - this.plugin('options', (options) => { - // modify options,后面的覆盖前面的 - options.flag = false - return options - }) - this.plugin('options', (options) => { - options.url = 'you://hello' - return options - }) - this.plugin('endpoint', (options) => { - // init - assert.equal(options.init, true) - // merge - assert.equal(options.url, 'you://hello') - // options plugin modify - assert.equal(options.flag, false) - - return new Promise((resolve, reject) => { - setTimeout(() => { - resolve({ retcode: 0, res: { msg: 'hello world' } }) - }, 0) - }) - }) - } - } - - const bb = new BB({ init: true }) + const bb = new DB({ init: true }); + bb.hooks.options.tap('optionsPlugin2', (options) => { + options.flag = true; + return options; + }); + bb.hooks.options.tap('optionsPlugin3', (options) => { + options.flag = false; + return options; + }); + bb.hooks.options.tap('optionsPlugin4', (options) => { + options.url = 'you://hello'; + return options; + }); + bb.hooks.endpoint.tapPromise('endpointPlugin4', (options) => { + assert.equal(options.init, true); + assert.equal(options.url, 'you://hello'); + assert.equal(options.flag, false); + return new Promise((resolve) => { + setTimeout(() => { + resolve({ retcode: 0, res: { msg: 'hello world' } }); + }, 0); + }) + }) bb.request({ url: 'my://hello' }) .then((res) => { done() @@ -149,61 +108,46 @@ describe('DB', function () { }) it('可以通过judge插件判断返回是否正确', function (done) { - class CC extends DB { - constructor(options) { - super(options) - this.plugin('endpoint', function (options) { - if (options.type === 1) { - return new Promise((resolve) => { - setTimeout(() => { - resolve({ retcode: 1, msg: 'logout' }) - }, 0) - }) - } - }) - this.plugin('endpoint', function (options) { - if (options.type === 0) { - return new Promise((resolve) => { - setTimeout(() => { - resolve({ retcode: 0, res: { msg: 'hello world' } }) - }, 0) - }) - } - }) - - this.plugin('judge', function (res) { - if (res.retcode !== 0) return true - }) + const cc = new DB(); + cc.hooks.endpoint.tapPromise('endpoingPlugin5', (options) => { + if (options.type === 0) { + return new Promise((resolve) => { + setTimeout(() => { + resolve({ retcode: 0, res: { msg: 'hello world' } }) + }, 0); + }); } - } - - const cc = new CC + if (options.type === 1) { + return new Promise((resolve) => { + setTimeout(() => { + resolve({ retcode: 1, msg: 'logout' }) + }, 0); + }); + } + }); + cc.hooks.judge.tap('judgePlugin1', (res) => { + return res.retcode === 0; + }); cc.request({ type: 0 }) .then((res) => { - assert.equal(res.res.msg, 'hello world') - return cc.request({ type: 1 }) + assert.equal(res.res.msg, 'hello world'); + return cc.request({ type: 1 }); }).then((res) => { - done(new Error('不应该进入正确回调,应当进入失败回调,因为retcode为1')) + done(new Error('不应该进入正确回调,应当进入失败回调,因为retcode为1')); }, (res) => { - assert.equal(res.retcode, 1) - assert.equal(res.msg, 'logout') - done() + assert.equal(res.retcode, 1); + assert.equal(res.msg, 'logout'); + done(); }) }) it('可以reject数据', function (done) { - class ZZ extends DB { - constructor(options) { - super(options) - this.plugin('endpoint', function () { - return new Promise((resolve, reject) => { - reject() - }) - }) - } - } - - const zz = new ZZ + const zz = new DB(); + zz.hooks.endpoint.tapPromise('endpointPlugin6', () => { + return new Promise((resolve, reject) => { + reject(); + }); + }); zz.request() .then(() => { From b539bf29b89408b640ecc59a86f1b910784838f4 Mon Sep 17 00:00:00 2001 From: icebox1234 <464752812@qq.com> Date: Fri, 19 Aug 2022 14:24:04 +0800 Subject: [PATCH 2/3] feat: for git workflow --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0c9b8b6..bb98d4d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "test", "main": "index.js", "scripts": { - "test": "karma start" + "test": "karma start --single-run" }, "repository": { "type": "git", From 2144c136f551b215d754f0022d69da6b2462a15a Mon Sep 17 00:00:00 2001 From: icebox1234 <464752812@qq.com> Date: Fri, 19 Aug 2022 14:32:07 +0800 Subject: [PATCH 3/3] feat: git workflow --- package.json | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index bb98d4d..1734cef 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "test", "main": "index.js", "scripts": { - "test": "karma start --single-run" + "test": "jest" }, "repository": { "type": "git", @@ -20,12 +20,7 @@ }, "homepage": "https://github.com/FE-star/homework2#readme", "devDependencies": { - "karma": "^6.3.16", - "karma-chrome-launcher": "^2.2.0", - "karma-mocha": "^1.3.0", - "karma-webpack": "^2.0.4", - "mocha": "^3.5.0", - "tapable": "^1.1.3", - "webpack": "^3.5.3" + "jest": "^28.1.3", + "tapable": "^1.1.3" } }