// external lib import * as Parser from "rss-parser"; // tested class import { ListenerRSSInfos as ListenerRRSInfo, ListenerRss as Listeners, } from "./../src/index"; // Unit test import * as chai from "chai"; import * as sinon from "ts-sinon"; import { ImportMock, InPlaceMockManager } from "ts-mock-imports"; 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"], }, }; 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", }, ], }; describe("Building Ytb listener", function () { it("The build without issues (infosListener parameters)", function () { 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"], }); 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 () { 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 () { 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 () { 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 () { 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 () { it("fetch without issues", function () { const mockManager: InPlaceMockManager = ImportMock.mockClassInPlace( Parser ); let stubTest = mockManager.mock("parseURL"); stubTest.withArgs(infosListener.address).resolves(mockedRSSOutput); stubTest .withArgs("bad.rss.service") .rejects(new Error("connect ECONNREFUSED 127.0.0.1:80")); const myListener = new Listeners(infosListener); // fetch let res = myListener.fetchRSS(); //assertion // calls return res .then((obj: any) => { expect(obj).to.be.eql(mockedRSSOutput); }) .catch((err) => { expect(err).to.be.undefined; }); }); it("fetch with bad address", function () { const mockManager: InPlaceMockManager = ImportMock.mockClassInPlace( Parser ); let stubParser = mockManager.mock("parseURL"); stubParser.withArgs(infosListener.address).resolves(mockedRSSOutput); stubParser .withArgs("bad.rss.service") .rejects(new Error("connect ECONNREFUSED 127.0.0.1:80")); let myListener = new Listeners({ name: "my-test-service", address: "bad.rss.service", customfields: { description: ["media:group", "media:description"], icon: ["media:group", "media:thumbnail"], }, }); // fetch let res = myListener.fetchRSS(); //assertion // calls expect(stubParser).to.have.been.calledOnce; expect(stubParser).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")); }); }); }); describe("start", function () { it("Let's start the timer", async function () { let clock: sinon.default.SinonFakeTimers = sinon.default.useFakeTimers(); const mockManager: InPlaceMockManager = ImportMock.mockClassInPlace( Parser ); let stubParser = mockManager.mock("parseURL"); stubParser.withArgs(infosListener.address).resolves(mockedRSSOutput); stubParser .withArgs("bad.rss.service") .rejects(new Error("connect ECONNREFUSED 127.0.0.1:80")); // 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"], }, }); //spy const fun_spy: sinon.default.SinonSpy = sinon.default.spy((obj) => { expect(obj).to.be.eql(mockedRSSOutput); }); // start timer myListener.on("update", (obj) => fun_spy(obj)); myListener.start(); // wait and assertion // After 1ms await clock.tickAsync(1); expect(fun_spy).to.have.been.calledOnce; // After 60s await clock.tickAsync(59999); expect(fun_spy).to.have.been.calledTwice; myListener.stop(); }); it("Let's start the timer (with a bad address)", async function () { let clock: sinon.default.SinonFakeTimers = sinon.default.useFakeTimers(); const mockManager: InPlaceMockManager = ImportMock.mockClassInPlace( Parser ); let stubParser = mockManager.mock("parseURL"); stubParser.withArgs(infosListener.address).resolves(mockedRSSOutput); stubParser .withArgs("bad.rss.service") .rejects(new Error("connect ECONNREFUSED 127.0.0.1:80")); // classic build let myListener = new Listeners({ name: "my-test-service", address: "bad.rss.service", timeloop: 60, customfields: { description: ["media:group", "media:description"], icon: ["media:group", "media:thumbnail"], }, }); //spy let fun_spy_err: sinon.default.SinonSpy = sinon.default.spy((err) => { expect(err).to.not.be.eql(undefined); }); myListener.on("update_err", fun_spy_err); // start timer myListener.start(); // wait and assertion // After 1ms await clock.tickAsync(1); // expect(stubListener.fetchRSS).to.have.been.calledOnce; expect(fun_spy_err).to.have.been.calledOnce; // After 60s await clock.tickAsync(59999); // expect(stubListener.fetchRSS).to.have.been.calledTwice; expect(fun_spy_err).to.have.been.calledTwice; myListener.stop(); }); }); });