listener-rss/tests/index-spec.ts

362 lines
11 KiB
TypeScript
Raw Normal View History

// external lib
2021-02-28 17:49:31 +01:00
import * as parserModule from "rss-parser";
2021-02-07 17:50:54 +01:00
// tested class
import {
2021-02-14 15:00:33 +01:00
ListenerRSSInfos as ListenerRRSInfo,
ListenerRss as Listeners,
2021-02-14 15:00:33 +01:00
} from "./../src/index";
// Unit test
2021-02-14 15:00:33 +01:00
import * as chai from "chai";
import * as sinon from "ts-sinon";
2021-02-28 17:49:31 +01:00
import { ImportMock } from "ts-mock-imports";
2021-02-14 15:00:33 +01:00
const sinonChai = require("sinon-chai");
chai.use(sinonChai);
const expect = chai.expect;
describe("test class RSS: jsonfile", function () {
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
2021-02-25 13:14:34 +01:00
let stubListener: sinon.StubbedInstance<Listeners>;
2021-02-14 15:00:33 +01:00
let stubParser: sinon.StubbedInstance<Parser>;
2021-02-25 13:14:34 +01:00
const mockedRSSOutput: Parser.Output<{
"media:group": { [key: string]: string | [any] };
}> = {
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",
},
],
};
2021-02-25 13:14:34 +01:00
/**
* The function create my Stubs for my Listener and my Parser
*/
2021-02-28 17:49:31 +01:00
// function fun_initStub(
// myListener: Listeners
// ): sinon.StubbedInstance<Listeners> {
// stubListener = sinon.stubObject<Listeners>(myListener, ["setParser"]);
// stubListener.setParser.callsFake(() => {
// if (stubListener.parser !== undefined) {
// stubParser = sinon.stubObject<Parser>(stubListener.parser, [
// "parseURL",
// ]);
// stubParser.parseURL
// .withArgs(infosListener.address)
// .resolves(mockedRSSOutput);
// stubParser.parseURL
// .withArgs("bad.rss.service")
// .rejects(new Error("connect ECONNREFUSED 127.0.0.1:80"));
// }
// stubListener.parser = stubParser;
// });
// stubListener.setParser();
// // stubListener.fetchRSS.returns(stubParser.parseURL(stubListener.address));
// return stubListener;
// }
2021-02-28 16:37:27 +01:00
// afterEach(function () {
// // restore stubs
// myListener = undefined;
// });
describe("Building Ytb listener", function () {
it("The build without issues (infosListener parameters)", function () {
2021-02-28 16:37:27 +01:00
let 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");
expect(myListener.customfields).to.eql({
description: ["media:group", "media:description"],
icon: ["media:group", "media:thumbnail"],
});
2021-02-25 13:14:34 +01:00
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 : 4 params)", function () {
2021-02-28 16:37:27 +01:00
let 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 () {
2021-02-28 16:37:27 +01:00
let 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 () {
2021-02-28 16:37:27 +01:00
let 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 () {
2021-02-28 16:37:27 +01:00
let 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"],
});
});
describe("fetch some data", function () {
2021-02-28 17:49:31 +01:00
it.only("fetch without issues", function () {
const mockManager = ImportMock.mockClass<
parserModule.default,
typeof parserModule
>(parserModule.default);
mockManager.mock("parseURL").resolves(mockedRSSOutput);
const myListener = new Listeners(infosListener);
2021-02-25 13:14:34 +01:00
// fetch
2021-02-28 17:49:31 +01:00
let res = myListener.fetchRSS();
//assertion
// calls
2021-02-28 17:49:31 +01:00
return res
.then((obj: any) => {
2021-02-28 17:49:31 +01:00
expect(obj).to.be.eql(mockedRSSOutput);
})
.catch((err) => {
2021-02-28 17:49:31 +01:00
expect(err).to.be.undefined;
});
});
2021-02-28 17:49:31 +01:00
// it("fetch with bad address", function () {
// let myListener = new Listeners({
// name: "my-test-service",
// address: "bad.rss.service",
// customfields: {
// description: ["media:group", "media:description"],
// icon: ["media:group", "media:thumbnail"],
// },
// });
// let stubListener = fun_initStub(myListener);
// // fetch
// let res = stubListener.fetchRSS();
// //assertion
// // calls
// expect(stubParser.parseURL).to.have.been.calledOnce;
// expect(stubParser.parseURL).to.have.been.calledWith("bad.rss.service");
// // Promise
// 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-07 17:50:54 +01:00
});
2021-02-28 17:49:31 +01:00
// describe.skip("start", function () {
// it("Let's start the timer", async function () {
// let clock: sinon.default.SinonFakeTimers = sinon.default.useFakeTimers();
// // classic build
// let myListener = new Listeners({
// name: "my-test-service",
// address: "fake.rss.service",
// timeloop: 60,
// customfields: {
// description: ["media:group", "media:description"],
// icon: ["media:group", "media:thumbnail"],
// },
// });
// let stubListener = fun_initStub(myListener);
// stubListener.fetchRSS.reset();
// stubListener.fetchRSS.resolves(mockedRSSOutput);
// //spy
// let fun_spy: sinon.default.SinonSpy = sinon.default.spy((obj, err) => {
// expect(obj).to.be.eql(mockedRSSOutput);
// expect(err).to.be.eql(undefined);
// });
// // start timer
// stubListener.on("update", (obj) => fun_spy(obj));
// // stubListener.start(fun_spy);
// // wait and assertion
// // After 1ms
// await clock.tickAsync(1);
// expect(stubListener.fetchRSS).to.have.been.calledOnce;
// expect(fun_spy).to.have.been.calledOnce;
// // After 60s
// await clock.tickAsync(59999);
// expect(stubListener.fetchRSS).to.have.been.calledTwice;
// expect(fun_spy).to.have.been.calledTwice;
// stubListener.stop();
// });
// it("Let's start the timer (with a bad address)", async function () {
// let clock: sinon.default.SinonFakeTimers = sinon.default.useFakeTimers();
// // classic build
// let myListener = new Listeners({
// name: "my-test-service",
// address: "fake.rss.service",
// timeloop: 60,
// customfields: {
// description: ["media:group", "media:description"],
// icon: ["media:group", "media:thumbnail"],
// },
// });
// let stubListener = fun_initStub(myListener);
// stubListener.fetchRSS.reset();
// stubListener.fetchRSS.rejects(
// new Error("connect ECONNREFUSED 127.0.0.1:80")
// );
// //spy
// let fun_spy: sinon.default.SinonSpy = sinon.default.spy((obj, err) => {
// expect(obj).to.be.eql(undefined);
// expect(err).to.not.be.eql(undefined);
// });
// // start timer
// // stubListener.start(fun_spy);
// // wait and assertion
// // After 1ms
// await clock.tickAsync(1);
// expect(stubListener.fetchRSS).to.have.been.calledOnce;
// expect(fun_spy).to.have.been.calledOnce;
// // After 60s
// await clock.tickAsync(59999);
// expect(stubListener.fetchRSS).to.have.been.calledTwice;
// expect(fun_spy).to.have.been.calledTwice;
// stubListener.stop();
// });
// });
2021-02-07 17:50:54 +01:00
});