From 400a9e81ebd06808be94e19ffa0b5d4facb9a24e Mon Sep 17 00:00:00 2001 From: Amaury Date: Sun, 21 Feb 2021 00:14:04 +0100 Subject: [PATCH] Refactor some test to TypeScript + try with FakeTimers for two lasts. --- tests/index-spec.ts | 238 ++++++++++++++++++++++++++------------------ 1 file changed, 143 insertions(+), 95 deletions(-) diff --git a/tests/index-spec.ts b/tests/index-spec.ts index cda001f..b99de22 100644 --- a/tests/index-spec.ts +++ b/tests/index-spec.ts @@ -5,6 +5,7 @@ import Parser from "rss-parser"; import { ListenerRSSInfos as ListenerRRSInfo, ListenerRss as Listeners, + ListenerRss, } from "./../src/index"; // Unit test @@ -111,80 +112,102 @@ 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({ + 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"], + }); }); describe("fetch some data", function () { @@ -204,11 +227,13 @@ describe("test class RSS: jsonfile", function () { ); // Promise //await expect(Promise.resolve(res)).to.be.eql(mockedRSSOutput); - res.then((obj: any, err: Error) => { - // TODO - expect(obj).to.be.eql(mockedRSSOutput); - expect(err).to.be.undefined; - }); + res + .then((obj: any) => { + expect(obj).to.be.eql(mockedRSSOutput); + }) + .catch((err) => { + expect(err).to.be.undefined; + }); }); it("fetch with bad address", function () { // classic build @@ -229,17 +254,31 @@ describe("test class RSS: jsonfile", function () { expect(stubParser.parseURL).to.have.been.calledOnce; expect(stubParser.parseURL).to.have.been.calledWith("bad.rss.service"); // Promise - res.then((obj: any, err: Error) => { - expect(obj).to.be.undefined; - expect(err).to.be.eql(new Error("connect ECONNREFUSED 127.0.0.1:80")); - }); + 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.skip("start", function () { - it("Let's start the timer", async function () { - //custom timeout - this.timeout(15000); + 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.prototype, [ + // "fetchRSS", + // ]); + // stubTempo.fetchRSS.returns( + // new Promise((callback: any) => { + // callback(); + // }) + // ); // classic build myListener = new Listeners({ @@ -253,24 +292,33 @@ describe("test class RSS: jsonfile", function () { }); //spy - const fun_spy: sinon.default.SinonSpy = sinon.default.spy(); + let fun_spy: sinon.default.SinonSpy = sinon.default.spy((obj) => { + console.log("ok"); + expect(obj).to.be.eql(mockedRSSOutput); + done(); + }); // start timer myListener.start(fun_spy); - await new Promise((res) => setTimeout(res, 5 * 1000)); + await clock.tickAsync(100); + expect(fun_spy).to.have.been.calledOnce; + + // + // await clock.tickAsync(4000); + // + // expect(fun_spy).to.have.been.calledThrice; 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(mockedRSSOutput, undefined); + // expect(fun_spy).to.have.been.callCount(3); + // expect(fun_spy).to.have.been.calledWith(mockedRSSOutput, undefined); }); - it("Let's start the timer (with a bad address)", async function () { + + it.skip("Let's start the timer (with a bad address)", async function () { //custom timeout - this.timeout(15000); + // this.timeout(15000); // classic build myListener = new Listeners({