From f8c9eb7d35185ce46d2ec49a23982dc73e3872ba Mon Sep 17 00:00:00 2001 From: Amaury Date: Sun, 14 Feb 2021 17:07:23 +0100 Subject: [PATCH] Add cross-env and ListenerRSSInfo is an interface --- package-lock.json | 9 ++ package.json | 3 +- src/Models/ListenerRSSInfos.ts | 48 +------- src/listener-rss.ts | 25 ++-- tests/index-spec.ts | 212 +++++++++++++++++---------------- 5 files changed, 134 insertions(+), 163 deletions(-) diff --git a/package-lock.json b/package-lock.json index 92572b2..e2c3507 100644 --- a/package-lock.json +++ b/package-lock.json @@ -629,6 +629,15 @@ "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", "dev": true }, + "cross-env": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz", + "integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==", + "dev": true, + "requires": { + "cross-spawn": "^7.0.1" + } + }, "cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", diff --git a/package.json b/package.json index e34514f..241e0f7 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "A lightweight library to give some additions for the [rss-parser package](https://github.com/rbren/rss-parser).", "main": "index.js", "scripts": { - "test": "TS_NODE_PROJECT='./tests/tsconfig.json' mocha --require ts-node/register ./tests/**/*-spec.ts", + "test": "cross-env TS_NODE_PROJECT='./tests/tsconfig.json' mocha --require ts-node/register ./tests/**/*-spec.ts", "build": "tsc -p ./src" }, "repository": { @@ -26,6 +26,7 @@ "@types/node": "^14.14.25", "@typescript-eslint/eslint-plugin": "^4.14.2", "@typescript-eslint/parser": "^4.14.2", + "cross-env": "7.0.3", "chai": "4.3.0", "eslint": "^7.19.0", "eslint-config-airbnb-base": "^14.2.1", diff --git a/src/Models/ListenerRSSInfos.ts b/src/Models/ListenerRSSInfos.ts index 75dae15..c193919 100644 --- a/src/Models/ListenerRSSInfos.ts +++ b/src/Models/ListenerRSSInfos.ts @@ -1,44 +1,6 @@ -export class ListenerRSSInfos { - _name: string = ""; // name of the listener - _address: string = ""; // feed's address - _timeloop: number | undefined = 5 * 60; // update time RSS feed - _customfields: [string, string | [string, string]][] | undefined = undefined; // rss fields custom - - constructor( - name: string, - address: string, - timeloop?: number, - customfields?: [string, string | [string, string]][] - ) { - if (name !== undefined && address !== undefined) { - this._name = name; - this._address = address; - this._timeloop = timeloop; - this._customfields = customfields; - } else throw new Error("Bad constructor's args"); - } - - set timeloop(value) { - this._timeloop = value; - } - - set customfields(value) { - this._customfields = value; - } - - get name() { - return this._name; - } - - get address() { - return this._address; - } - - get timeloop() { - return this._timeloop; - } - - get customfields() { - return this._customfields; - } +export interface ListenerRSSInfos { + name: string; // name of the listener + address: string; // feed's address + timeloop?: number; // update time RSS feed + customfields?: [string, string | [string, string]][]; // rss fields custom } diff --git a/src/listener-rss.ts b/src/listener-rss.ts index 36e4f4f..69a0b01 100644 --- a/src/listener-rss.ts +++ b/src/listener-rss.ts @@ -7,25 +7,14 @@ export class ListenerRss { name: string = ""; address: string = ""; timeloop: number = DEFAULT_TIMELOOP; // time in seconds - customfields: [string, string | [string, string]][] | undefined = undefined; + customfields?: [string, string | [string, string]][]; // private fields parser: Parser | undefined = undefined; loopRunning: boolean = false; - constructor( - name: string | ListenerInfo, - address?: string, - timeloop?: number, - customfields?: [string, string | [string, string]][] - ) { - if (name instanceof ListenerInfo) { - // constructor with 1 arg - this.setData(name); - } else if (address !== undefined) { - // construct with between 2 and 4 args - this.setData(new ListenerInfo(name, address, timeloop, customfields)); - } else throw new Error("the constructor must have args"); + constructor(config: ListenerInfo) { + this.setData(config); this.setParser(); } @@ -46,12 +35,12 @@ export class ListenerRss { setData(infos: ListenerInfo) { // Set data - this.name = infos._name; - this.address = infos._address; + this.name = infos.name; + this.address = infos.address; this.timeloop = - infos._timeloop === undefined ? DEFAULT_TIMELOOP : infos._timeloop; + infos.timeloop === undefined ? DEFAULT_TIMELOOP : infos.timeloop; this.customfields = - infos._customfields === undefined ? undefined : infos._customfields; + infos.customfields === undefined ? undefined : infos.customfields; } fetchRSS(): any { diff --git a/tests/index-spec.ts b/tests/index-spec.ts index 2e36ef5..93c76b1 100644 --- a/tests/index-spec.ts +++ b/tests/index-spec.ts @@ -1,5 +1,5 @@ // external lib -import * as Parser from "rss-parser"; +import Parser from "rss-parser"; // tested class import { @@ -21,15 +21,15 @@ const expect = chai.expect; describe("test class RSS: jsonfile", function () { let myListener: Listeners | undefined = undefined; - const infosListener: ListenerRRSInfo = new ListenerRRSInfo( - "my-test-service", - "fake.rss.service", - 15, - [ + const infosListener: ListenerRRSInfo = { + name: "my-test-service", + address: "fake.rss.service", + timeloop: 15, + customfields: [ ["description", ["media:group", "media:description"]], ["icon", ["media:group", "media:thumbnail"]], - ] - ); + ], + }; // parseURL tests let stubParser: sinon.StubbedInstance; @@ -89,6 +89,7 @@ describe("test class RSS: jsonfile", function () { afterEach(function () { // restore stubs + stubParser.parseURL.reset(); }); describe("Building Ytb listener", function () { @@ -110,83 +111,83 @@ describe("test class RSS: jsonfile", function () { item: ["media:group", "media:group"], });*/ }); - it("The build without issues (raw infos : 4 params)", function () { - myListener = new Listeners("my-test-service", "fake.rss.service", 15, [ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ]); - - // assertions - // myListener data - expect(myListener.timeloop).to.eql(15); - expect(myListener.name).to.eql("my-test-service"); - expect(myListener.address).to.eql("fake.rss.service"); - expect(myListener.customfields).to.eql([ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ]); - /* - expect(myListener.parser.options.customFields).to.eql({ - feed: [], - item: ["media:group", "media:group"], - });*/ - }); - it("The build without issues (raw infos : just 2 params)", function () { - myListener = new Listeners("my-test-service", "fake.rss.service"); - - // assertions - // myListener data - expect(myListener.timeloop).to.eql(5 * 60); - expect(myListener.name).to.eql("my-test-service"); - expect(myListener.address).to.eql("fake.rss.service"); - expect(myListener.customfields).to.eql(undefined); - /* - expect(myListener.parser.options.customFields).to.eql({ - feed: [], - item: [], - });*/ - }); - it("The build without issues (raw infos : just 3 params (no custom fields))", function () { - myListener = new Listeners("my-test-service", "fake.rss.service", 15); - - // assertions - // myListener data - expect(myListener.timeloop).to.eql(15); - expect(myListener.name).to.eql("my-test-service"); - expect(myListener.address).to.eql("fake.rss.service"); - expect(myListener.customfields).to.eql(undefined); - /* - expect(myListener.parser.options.customFields).to.eql({ - feed: [], - item: [], - });*/ - }); - it("The build without issues (raw infos : just 3 params (no timeloop))", function () { - myListener = new Listeners( - "my-test-service", - "fake.rss.service", - undefined, - [ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ] - ); - - // assertions - // myListener data - expect(myListener.timeloop).to.eql(5 * 60); - expect(myListener.name).to.eql("my-test-service"); - expect(myListener.address).to.eql("fake.rss.service"); - expect(myListener.customfields).to.eql([ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ]); - /* - expect(myListener.parser.options.customFields).to.eql({ - feed: [], - item: ["media:group", "media:group"], - });*/ - }); + // it("The build without issues (raw infos : 4 params)", function () { + // myListener = new Listeners("my-test-service", "fake.rss.service", 15, [ + // ["description", ["media:group", "media:description"]], + // ["icon", ["media:group", "media:thumbnail"]], + // ]); + // + // // assertions + // // myListener data + // expect(myListener.timeloop).to.eql(15); + // expect(myListener.name).to.eql("my-test-service"); + // expect(myListener.address).to.eql("fake.rss.service"); + // expect(myListener.customfields).to.eql([ + // ["description", ["media:group", "media:description"]], + // ["icon", ["media:group", "media:thumbnail"]], + // ]); + // /* + // expect(myListener.parser.options.customFields).to.eql({ + // feed: [], + // item: ["media:group", "media:group"], + // });*/ + // }); + // it("The build without issues (raw infos : just 2 params)", function () { + // myListener = new Listeners("my-test-service", "fake.rss.service"); + // + // // assertions + // // myListener data + // expect(myListener.timeloop).to.eql(5 * 60); + // expect(myListener.name).to.eql("my-test-service"); + // expect(myListener.address).to.eql("fake.rss.service"); + // expect(myListener.customfields).to.eql(undefined); + // /* + // expect(myListener.parser.options.customFields).to.eql({ + // feed: [], + // item: [], + // });*/ + // }); + // it("The build without issues (raw infos : just 3 params (no custom fields))", function () { + // myListener = new Listeners("my-test-service", "fake.rss.service", 15); + // + // // assertions + // // myListener data + // expect(myListener.timeloop).to.eql(15); + // expect(myListener.name).to.eql("my-test-service"); + // expect(myListener.address).to.eql("fake.rss.service"); + // expect(myListener.customfields).to.eql(undefined); + // /* + // expect(myListener.parser.options.customFields).to.eql({ + // feed: [], + // item: [], + // });*/ + // }); + // it("The build without issues (raw infos : just 3 params (no timeloop))", function () { + // myListener = new Listeners( + // "my-test-service", + // "fake.rss.service", + // undefined, + // [ + // ["description", ["media:group", "media:description"]], + // ["icon", ["media:group", "media:thumbnail"]], + // ] + // ); + // + // // assertions + // // myListener data + // expect(myListener.timeloop).to.eql(5 * 60); + // expect(myListener.name).to.eql("my-test-service"); + // expect(myListener.address).to.eql("fake.rss.service"); + // expect(myListener.customfields).to.eql([ + // ["description", ["media:group", "media:description"]], + // ["icon", ["media:group", "media:thumbnail"]], + // ]); + // /* + // expect(myListener.parser.options.customFields).to.eql({ + // feed: [], + // item: ["media:group", "media:group"], + // });*/ + // }); }); describe("fetch some data", function () { @@ -202,7 +203,7 @@ describe("test class RSS: jsonfile", function () { // calls expect(stubParser.parseURL).to.have.been.calledOnce; expect(stubParser.parseURL).to.have.been.calledWith( - infosListener._address + infosListener.address ); // Promise //await expect(Promise.resolve(res)).to.be.eql(mockedRSSOutput); @@ -214,15 +215,14 @@ describe("test class RSS: jsonfile", function () { }); it("fetch with bad address", function () { // classic build - myListener = new Listeners( - "my-test-service", - "bad.rss.service", - undefined, - [ + myListener = new Listeners({ + name: "my-test-service", + address: "bad.rss.service", + customfields: [ ["description", ["media:group", "media:description"]], ["icon", ["media:group", "media:thumbnail"]], - ] - ); + ], + }); myListener.parser = stubParser; // replace the parser by my fake parser // fetch let res = myListener.fetchRSS(); @@ -245,10 +245,15 @@ describe("test class RSS: jsonfile", function () { this.timeout(15000); // classic build - myListener = new Listeners("my-test-service", "fake.rss.service", 2, [ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ]); + myListener = new Listeners({ + name: "my-test-service", + address: "fake.rss.service", + timeloop: 2, + customfields: [ + ["description", ["media:group", "media:description"]], + ["icon", ["media:group", "media:thumbnail"]], + ], + }); //spy const fun_spy: sinon.default.SinonSpy = sinon.default.spy(); @@ -271,10 +276,15 @@ describe("test class RSS: jsonfile", function () { this.timeout(15000); // classic build - myListener = new Listeners("my-test-service", "bad.rss.service", 2, [ - ["description", ["media:group", "media:description"]], - ["icon", ["media:group", "media:thumbnail"]], - ]); + myListener = new Listeners({ + name: "my-test-service", + address: "fake.rss.service", + timeloop: 2, + customfields: [ + ["description", ["media:group", "media:description"]], + ["icon", ["media:group", "media:thumbnail"]], + ], + }); //spy const fun_spy: sinon.default.SinonSpy = sinon.default.spy();