Refactor some test to TypeScript + try with FakeTimers for two lasts.

This commit is contained in:
Amaury 2021-02-21 00:14:04 +01:00
parent 83619780a4
commit 400a9e81eb

View File

@ -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>(Listeners.prototype, [
// "fetchRSS",
// ]);
// stubTempo.fetchRSS.returns(
// new Promise<any>((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({