Implements ts-mock-imports
This commit is contained in:
parent
eacf90b161
commit
920c160632
|
@ -1,5 +1,5 @@
|
|||
// external lib
|
||||
import Parser from "rss-parser";
|
||||
import * as Parser from "rss-parser";
|
||||
|
||||
// tested class
|
||||
import {
|
||||
|
@ -10,6 +10,7 @@ import {
|
|||
// 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");
|
||||
|
||||
|
@ -18,8 +19,6 @@ chai.use(sinonChai);
|
|||
const expect = chai.expect;
|
||||
|
||||
describe("test class RSS: jsonfile", function () {
|
||||
// let myListener: Listeners | undefined = undefined;
|
||||
|
||||
const infosListener: ListenerRRSInfo = {
|
||||
name: "my-test-service",
|
||||
address: "fake.rss.service",
|
||||
|
@ -30,10 +29,6 @@ describe("test class RSS: jsonfile", function () {
|
|||
},
|
||||
};
|
||||
|
||||
// parseURL tests
|
||||
let stubListener: sinon.StubbedInstance<Listeners>;
|
||||
let stubParser: sinon.StubbedInstance<Parser>;
|
||||
|
||||
const mockedRSSOutput: Parser.Output<{
|
||||
"media:group": { [key: string]: string | [any] };
|
||||
}> = {
|
||||
|
@ -74,37 +69,6 @@ describe("test class RSS: jsonfile", function () {
|
|||
],
|
||||
};
|
||||
|
||||
/**
|
||||
* The function create my Stubs for my Listener and my Parser
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
// afterEach(function () {
|
||||
// // restore stubs
|
||||
// myListener = undefined;
|
||||
// });
|
||||
|
||||
describe("Building Ytb listener", function () {
|
||||
it("The build without issues (infosListener parameters)", function () {
|
||||
let myListener = new Listeners(infosListener);
|
||||
|
@ -226,32 +190,41 @@ describe("test class RSS: jsonfile", function () {
|
|||
|
||||
describe("fetch some data", function () {
|
||||
it("fetch without issues", function () {
|
||||
let myListener = new Listeners(infosListener);
|
||||
fun_initStub();
|
||||
const mockManager: InPlaceMockManager<Parser> = ImportMock.mockClassInPlace<Parser>(
|
||||
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"));
|
||||
|
||||
expect(myListener).to.not.be.undefined;
|
||||
if (myListener !== undefined) {
|
||||
// fetch
|
||||
let res = stubListener.fetchRSS();
|
||||
const myListener = new Listeners(infosListener);
|
||||
|
||||
//assertion
|
||||
// calls
|
||||
expect(stubParser.parseURL).to.have.been.calledOnce;
|
||||
expect(stubParser.parseURL).to.have.been.calledWith(
|
||||
infosListener.address
|
||||
);
|
||||
// fetch
|
||||
let res = myListener.fetchRSS();
|
||||
|
||||
res
|
||||
.then((obj: any) => {
|
||||
expect(obj).to.be.eql(mockedRSSOutput);
|
||||
})
|
||||
.catch((err) => {
|
||||
expect(err).to.be.undefined;
|
||||
});
|
||||
} else throw new Error("Error into the before instruction");
|
||||
//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<Parser> = ImportMock.mockClassInPlace<Parser>(
|
||||
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",
|
||||
|
@ -260,14 +233,13 @@ describe("test class RSS: jsonfile", function () {
|
|||
icon: ["media:group", "media:thumbnail"],
|
||||
},
|
||||
});
|
||||
let stubListener = fun_initStub(myListener);
|
||||
// fetch
|
||||
let res = stubListener.fetchRSS();
|
||||
let res = myListener.fetchRSS();
|
||||
|
||||
//assertion
|
||||
// calls
|
||||
expect(stubParser.parseURL).to.have.been.calledOnce;
|
||||
expect(stubParser.parseURL).to.have.been.calledWith("bad.rss.service");
|
||||
expect(stubParser).to.have.been.calledOnce;
|
||||
expect(stubParser).to.have.been.calledWith("bad.rss.service");
|
||||
// Promise
|
||||
res
|
||||
.then((obj: any) => {
|
||||
|
@ -279,10 +251,19 @@ describe("test class RSS: jsonfile", function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe.skip("start", function () {
|
||||
describe("start", function () {
|
||||
it("Let's start the timer", async function () {
|
||||
let clock: sinon.default.SinonFakeTimers = sinon.default.useFakeTimers();
|
||||
|
||||
const mockManager: InPlaceMockManager<Parser> = ImportMock.mockClassInPlace<Parser>(
|
||||
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",
|
||||
|
@ -293,74 +274,73 @@ describe("test class RSS: jsonfile", function () {
|
|||
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) => {
|
||||
const fun_spy: sinon.default.SinonSpy = sinon.default.spy((obj) => {
|
||||
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);
|
||||
myListener.on("update", (obj) => fun_spy(obj));
|
||||
myListener.start();
|
||||
|
||||
// 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();
|
||||
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<Parser> = ImportMock.mockClassInPlace<Parser>(
|
||||
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",
|
||||
address: "bad.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);
|
||||
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
|
||||
// stubListener.start(fun_spy);
|
||||
myListener.start();
|
||||
|
||||
// wait and assertion
|
||||
// After 1ms
|
||||
await clock.tickAsync(1);
|
||||
expect(stubListener.fetchRSS).to.have.been.calledOnce;
|
||||
expect(fun_spy).to.have.been.calledOnce;
|
||||
// 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).to.have.been.calledTwice;
|
||||
// expect(stubListener.fetchRSS).to.have.been.calledTwice;
|
||||
expect(fun_spy_err).to.have.been.calledTwice;
|
||||
|
||||
stubListener.stop();
|
||||
myListener.stop();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user