2021-02-13 13:40:18 +01:00
|
|
|
// external lib
|
2021-02-14 17:07:23 +01:00
|
|
|
import Parser from "rss-parser";
|
2021-02-07 17:50:54 +01:00
|
|
|
|
2021-02-13 13:40:18 +01:00
|
|
|
// tested class
|
|
|
|
import {
|
2021-02-14 15:00:33 +01:00
|
|
|
ListenerRSSInfos as ListenerRRSInfo,
|
2021-02-13 13:40:18 +01:00
|
|
|
ListenerRss as Listeners,
|
2021-02-21 00:14:04 +01:00
|
|
|
ListenerRss,
|
2021-02-14 15:00:33 +01:00
|
|
|
} from "./../src/index";
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
// Unit test
|
2021-02-14 15:00:33 +01:00
|
|
|
import * as chai from "chai";
|
|
|
|
import * as sinon from "ts-sinon";
|
|
|
|
//import sinonChai from "sinon-chai";
|
|
|
|
|
|
|
|
const sinonChai = require("sinon-chai");
|
|
|
|
|
|
|
|
chai.use(sinonChai);
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
const expect = chai.expect;
|
|
|
|
|
|
|
|
describe("test class RSS: jsonfile", function () {
|
|
|
|
let myListener: Listeners | undefined = undefined;
|
|
|
|
|
2021-02-14 17:07:23 +01:00
|
|
|
const infosListener: ListenerRRSInfo = {
|
|
|
|
name: "my-test-service",
|
|
|
|
address: "fake.rss.service",
|
|
|
|
timeloop: 15,
|
2021-02-14 17:37:03 +01:00
|
|
|
customfields: {
|
|
|
|
description: ["media:group", "media:description"],
|
|
|
|
icon: ["media:group", "media:thumbnail"],
|
|
|
|
},
|
2021-02-14 17:07:23 +01:00
|
|
|
};
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
// parseURL tests
|
2021-02-14 15:00:33 +01:00
|
|
|
let stubParser: sinon.StubbedInstance<Parser>;
|
2021-02-13 13:40:18 +01:00
|
|
|
const mockedRSSOutput: any = {
|
|
|
|
// TODO any = pas bien
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
title: "my title 00",
|
|
|
|
"media:group": {
|
|
|
|
"media:description": "my description 00",
|
|
|
|
"media:thumbnail": [
|
|
|
|
{ $: { height: 360, width: 420, url: "my_image00.jpg" } },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
link: "my_url_00.com",
|
|
|
|
pubDate: "myDate00",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "my title 01",
|
|
|
|
"media:group": {
|
|
|
|
"media:description": "my description 01",
|
|
|
|
"media:thumbnail": [
|
|
|
|
{ $: { height: 360, width: 420, url: "my_image01.jpg" } },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
link: "my_url_01.com",
|
|
|
|
pubDate: "myDate01",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "my title 02",
|
|
|
|
"media:group": {
|
|
|
|
"media:description": "my description 02",
|
|
|
|
"media:thumbnail": [
|
|
|
|
{ $: { height: 360, width: 420, url: "my_image02.jpg" } },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
link: "my_url_02.com",
|
|
|
|
pubDate: "myDate02",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
// stubs
|
2021-02-14 15:00:33 +01:00
|
|
|
stubParser = sinon.stubObject<Parser>(Parser.prototype, ["parseURL"]);
|
|
|
|
|
|
|
|
stubParser.parseURL
|
|
|
|
.withArgs(infosListener.address)
|
|
|
|
.resolves(mockedRSSOutput);
|
|
|
|
stubParser.parseURL
|
2021-02-13 13:40:18 +01:00
|
|
|
.withArgs("bad.rss.service")
|
2021-02-14 15:00:33 +01:00
|
|
|
.rejects(new Error("connect ECONNREFUSED 127.0.0.1:80"));
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
// constructor
|
|
|
|
myListener = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
// restore stubs
|
2021-02-14 17:07:23 +01:00
|
|
|
stubParser.parseURL.reset();
|
2021-02-13 13:40:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("Building Ytb listener", function () {
|
|
|
|
it("The build without issues (infosListener parameters)", function () {
|
|
|
|
myListener = new Listeners(infosListener);
|
|
|
|
|
|
|
|
// 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");
|
2021-02-14 17:37:03 +01:00
|
|
|
expect(myListener.customfields).to.eql({
|
|
|
|
description: ["media:group", "media:description"],
|
|
|
|
icon: ["media:group", "media:thumbnail"],
|
|
|
|
});
|
2021-02-14 15:00:33 +01:00
|
|
|
/* // TODO test des champs dans l'objet parser
|
2021-02-13 13:40:18 +01:00
|
|
|
expect(myListener.parser.options.customFields).to.eql({
|
|
|
|
feed: [],
|
|
|
|
item: ["media:group", "media:group"],
|
2021-02-14 15:00:33 +01:00
|
|
|
});*/
|
2021-02-13 13:40:18 +01:00
|
|
|
});
|
2021-02-21 00:14:04 +01:00
|
|
|
it("The build without issues (raw infos : 4 params)", function () {
|
|
|
|
myListener = new Listeners({
|
|
|
|
name: "my-test-service",
|
|
|
|
address: "fake.rss.service",
|
|
|
|
timeloop: 15,
|
|
|
|
customfields: {
|
|
|
|
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)
|
|
|
|
.to.have.property("options")
|
|
|
|
.to.have.property("customFields")
|
|
|
|
.to.be.eql({
|
|
|
|
feed: [],
|
|
|
|
item: ["media:group", "media:group"],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("The build without issues (raw infos : just 2 params)", function () {
|
|
|
|
myListener = new Listeners({
|
|
|
|
name: "my-test-service",
|
|
|
|
address: "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)
|
|
|
|
.to.have.property("options")
|
|
|
|
.to.have.property("customFields")
|
|
|
|
.to.be.eql({
|
|
|
|
feed: [],
|
|
|
|
item: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("The build without issues (raw infos : just 3 params (no custom fields))", function () {
|
|
|
|
myListener = new Listeners({
|
|
|
|
name: "my-test-service",
|
|
|
|
address: "fake.rss.service",
|
|
|
|
timeloop: 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)
|
|
|
|
.to.have.property("options")
|
|
|
|
.to.have.property("customFields")
|
|
|
|
.to.be.eql({
|
|
|
|
feed: [],
|
|
|
|
item: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("The build without issues (raw infos : just 3 params (no timeloop))", function () {
|
|
|
|
myListener = new Listeners({
|
|
|
|
name: "my-test-service",
|
|
|
|
address: "fake.rss.service",
|
|
|
|
customfields: {
|
|
|
|
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)
|
|
|
|
.to.have.property("options")
|
|
|
|
.to.have.property("customFields")
|
|
|
|
.to.be.eql({
|
|
|
|
feed: [],
|
|
|
|
item: ["media:group", "media:group"],
|
|
|
|
});
|
2021-02-13 13:40:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("fetch some data", function () {
|
|
|
|
it("fetch without issues", function () {
|
|
|
|
// classic build
|
|
|
|
myListener = new Listeners(infosListener);
|
2021-02-14 15:00:33 +01:00
|
|
|
|
|
|
|
myListener.parser = stubParser; // replace the parser by my fake parser
|
2021-02-13 13:40:18 +01:00
|
|
|
// fetch
|
|
|
|
let res = myListener.fetchRSS();
|
|
|
|
|
|
|
|
//assertion
|
|
|
|
// calls
|
2021-02-14 15:00:33 +01:00
|
|
|
expect(stubParser.parseURL).to.have.been.calledOnce;
|
|
|
|
expect(stubParser.parseURL).to.have.been.calledWith(
|
2021-02-14 17:07:23 +01:00
|
|
|
infosListener.address
|
2021-02-14 15:00:33 +01:00
|
|
|
);
|
2021-02-13 13:40:18 +01:00
|
|
|
// Promise
|
|
|
|
//await expect(Promise.resolve(res)).to.be.eql(mockedRSSOutput);
|
2021-02-21 00:14:04 +01:00
|
|
|
res
|
|
|
|
.then((obj: any) => {
|
|
|
|
expect(obj).to.be.eql(mockedRSSOutput);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
expect(err).to.be.undefined;
|
|
|
|
});
|
2021-02-13 13:40:18 +01:00
|
|
|
});
|
|
|
|
it("fetch with bad address", function () {
|
|
|
|
// classic build
|
2021-02-14 17:07:23 +01:00
|
|
|
myListener = new Listeners({
|
|
|
|
name: "my-test-service",
|
|
|
|
address: "bad.rss.service",
|
2021-02-14 17:37:03 +01:00
|
|
|
customfields: {
|
|
|
|
description: ["media:group", "media:description"],
|
|
|
|
icon: ["media:group", "media:thumbnail"],
|
|
|
|
},
|
2021-02-14 17:07:23 +01:00
|
|
|
});
|
2021-02-14 15:00:33 +01:00
|
|
|
myListener.parser = stubParser; // replace the parser by my fake parser
|
2021-02-13 13:40:18 +01:00
|
|
|
// fetch
|
|
|
|
let res = myListener.fetchRSS();
|
|
|
|
|
|
|
|
//assertion
|
|
|
|
// calls
|
2021-02-14 15:00:33 +01:00
|
|
|
expect(stubParser.parseURL).to.have.been.calledOnce;
|
|
|
|
expect(stubParser.parseURL).to.have.been.calledWith("bad.rss.service");
|
2021-02-13 13:40:18 +01:00
|
|
|
// Promise
|
2021-02-21 00:14:04 +01:00
|
|
|
res
|
|
|
|
.then((obj: any) => {
|
|
|
|
expect(obj).to.be.undefined;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
expect(err).to.be.eql(new Error("connect ECONNREFUSED 127.0.0.1:80"));
|
|
|
|
});
|
2021-02-13 13:40:18 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-21 00:14:04 +01:00
|
|
|
describe("start", function () {
|
|
|
|
it("Let's start the timer", async function (done) {
|
|
|
|
this.timeout(5000);
|
|
|
|
let clock: sinon.default.SinonFakeTimers = sinon.default.useFakeTimers({
|
|
|
|
toFake: ["setTimeout"],
|
|
|
|
});
|
|
|
|
|
|
|
|
// let stubTempo = sinon.stubObject<Listeners>(Listeners.prototype, [
|
|
|
|
// "fetchRSS",
|
|
|
|
// ]);
|
|
|
|
// stubTempo.fetchRSS.returns(
|
|
|
|
// new Promise<any>((callback: any) => {
|
|
|
|
// callback();
|
|
|
|
// })
|
|
|
|
// );
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
// classic build
|
2021-02-14 17:07:23 +01:00
|
|
|
myListener = new Listeners({
|
|
|
|
name: "my-test-service",
|
|
|
|
address: "fake.rss.service",
|
|
|
|
timeloop: 2,
|
2021-02-14 17:37:03 +01:00
|
|
|
customfields: {
|
|
|
|
description: ["media:group", "media:description"],
|
|
|
|
icon: ["media:group", "media:thumbnail"],
|
|
|
|
},
|
2021-02-14 17:07:23 +01:00
|
|
|
});
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
//spy
|
2021-02-21 00:14:04 +01:00
|
|
|
let fun_spy: sinon.default.SinonSpy = sinon.default.spy((obj) => {
|
|
|
|
console.log("ok");
|
|
|
|
expect(obj).to.be.eql(mockedRSSOutput);
|
|
|
|
done();
|
|
|
|
});
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
// start timer
|
|
|
|
myListener.start(fun_spy);
|
|
|
|
|
2021-02-21 00:14:04 +01:00
|
|
|
await clock.tickAsync(100);
|
|
|
|
expect(fun_spy).to.have.been.calledOnce;
|
2021-02-13 13:40:18 +01:00
|
|
|
|
2021-02-21 00:14:04 +01:00
|
|
|
//
|
|
|
|
// await clock.tickAsync(4000);
|
|
|
|
//
|
|
|
|
// expect(fun_spy).to.have.been.calledThrice;
|
2021-02-13 13:40:18 +01:00
|
|
|
|
2021-02-21 00:14:04 +01:00
|
|
|
myListener.stop();
|
2021-02-13 13:40:18 +01:00
|
|
|
//assertion
|
|
|
|
// calls
|
2021-02-21 00:14:04 +01:00
|
|
|
// expect(fun_spy).to.have.been.callCount(3);
|
|
|
|
// expect(fun_spy).to.have.been.calledWith(mockedRSSOutput, undefined);
|
2021-02-13 13:40:18 +01:00
|
|
|
});
|
2021-02-21 00:14:04 +01:00
|
|
|
|
|
|
|
it.skip("Let's start the timer (with a bad address)", async function () {
|
2021-02-13 13:40:18 +01:00
|
|
|
//custom timeout
|
2021-02-21 00:14:04 +01:00
|
|
|
// this.timeout(15000);
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
// classic build
|
2021-02-14 17:07:23 +01:00
|
|
|
myListener = new Listeners({
|
|
|
|
name: "my-test-service",
|
|
|
|
address: "fake.rss.service",
|
|
|
|
timeloop: 2,
|
2021-02-14 17:37:03 +01:00
|
|
|
customfields: {
|
|
|
|
description: ["media:group", "media:description"],
|
|
|
|
icon: ["media:group", "media:thumbnail"],
|
|
|
|
},
|
2021-02-14 17:07:23 +01:00
|
|
|
});
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
//spy
|
2021-02-14 15:00:33 +01:00
|
|
|
const fun_spy: sinon.default.SinonSpy = sinon.default.spy();
|
2021-02-13 13:40:18 +01:00
|
|
|
|
|
|
|
// start timer
|
|
|
|
myListener.start(fun_spy);
|
|
|
|
|
|
|
|
await new Promise((res) => setTimeout(res, 5 * 1000));
|
|
|
|
|
|
|
|
myListener.stop();
|
|
|
|
//assertion
|
|
|
|
// calls
|
|
|
|
expect(1).to.be.eql(1);
|
|
|
|
expect(fun_spy).to.have.been.callCount(3);
|
|
|
|
expect(fun_spy).to.have.been.calledWith(undefined, Error); //yagni
|
2021-02-07 17:50:54 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|