Add cross-env and ListenerRSSInfo is an interface

This commit is contained in:
Amaury 2021-02-14 17:07:23 +01:00
parent 43e93671dd
commit f8c9eb7d35
5 changed files with 134 additions and 163 deletions

9
package-lock.json generated
View File

@ -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",

View File

@ -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",

View File

@ -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
}

View File

@ -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 {

View File

@ -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<Parser>;
@ -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();