better stubs done
This commit is contained in:
		@@ -5,13 +5,11 @@ import Parser from "rss-parser";
 | 
			
		||||
import {
 | 
			
		||||
  ListenerRSSInfos as ListenerRRSInfo,
 | 
			
		||||
  ListenerRss as Listeners,
 | 
			
		||||
  ListenerRss,
 | 
			
		||||
} from "./../src/index";
 | 
			
		||||
 | 
			
		||||
// Unit test
 | 
			
		||||
import * as chai from "chai";
 | 
			
		||||
import * as sinon from "ts-sinon";
 | 
			
		||||
//import sinonChai from "sinon-chai";
 | 
			
		||||
 | 
			
		||||
const sinonChai = require("sinon-chai");
 | 
			
		||||
 | 
			
		||||
@@ -33,7 +31,9 @@ describe("test class RSS: jsonfile", function () {
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  // parseURL tests
 | 
			
		||||
  let stubListener: sinon.StubbedInstance<Listeners>;
 | 
			
		||||
  let stubParser: sinon.StubbedInstance<Parser>;
 | 
			
		||||
 | 
			
		||||
  const mockedRSSOutput: any = {
 | 
			
		||||
    // TODO any = pas bien
 | 
			
		||||
    items: [
 | 
			
		||||
@@ -73,24 +73,36 @@ describe("test class RSS: jsonfile", function () {
 | 
			
		||||
    ],
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  beforeEach(function () {
 | 
			
		||||
    // stubs
 | 
			
		||||
    stubParser = sinon.stubObject<Parser>(Parser.prototype, ["parseURL"]);
 | 
			
		||||
 | 
			
		||||
    stubParser.parseURL
 | 
			
		||||
      .withArgs(infosListener.address)
 | 
			
		||||
      .resolves(mockedRSSOutput);
 | 
			
		||||
    stubParser.parseURL
 | 
			
		||||
      .withArgs("bad.rss.service")
 | 
			
		||||
      .rejects(new Error("connect ECONNREFUSED 127.0.0.1:80"));
 | 
			
		||||
 | 
			
		||||
    // constructor
 | 
			
		||||
    myListener = undefined;
 | 
			
		||||
  });
 | 
			
		||||
  /**
 | 
			
		||||
   * The function create my Stubs for my Listener and my Parser
 | 
			
		||||
   */
 | 
			
		||||
  const fun_initStub: () => void = () => {
 | 
			
		||||
    if (myListener !== undefined && myListener.parser !== undefined) {
 | 
			
		||||
      stubListener = sinon.stubObject<Listeners>(myListener, [
 | 
			
		||||
        "setParser",
 | 
			
		||||
        "fetchRSS",
 | 
			
		||||
      ]);
 | 
			
		||||
      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.setParser();
 | 
			
		||||
      stubListener.fetchRSS.resolves(stubParser.parseURL(stubListener.address));
 | 
			
		||||
    } else throw new Error("myListener need to be initiliaze before the stub");
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  afterEach(function () {
 | 
			
		||||
    // restore stubs
 | 
			
		||||
    stubParser.parseURL.reset();
 | 
			
		||||
    myListener = undefined;
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  describe("Building Ytb listener", function () {
 | 
			
		||||
@@ -106,11 +118,13 @@ describe("test class RSS: jsonfile", function () {
 | 
			
		||||
        description: ["media:group", "media:description"],
 | 
			
		||||
        icon: ["media:group", "media:thumbnail"],
 | 
			
		||||
      });
 | 
			
		||||
      /* // TODO test des champs dans l'objet parser
 | 
			
		||||
      expect(myListener.parser.options.customFields).to.eql({
 | 
			
		||||
        feed: [],
 | 
			
		||||
        item: ["media:group", "media:group"],
 | 
			
		||||
      });*/
 | 
			
		||||
      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 () {
 | 
			
		||||
      myListener = new Listeners({
 | 
			
		||||
@@ -212,31 +226,32 @@ describe("test class RSS: jsonfile", function () {
 | 
			
		||||
 | 
			
		||||
  describe("fetch some data", function () {
 | 
			
		||||
    it("fetch without issues", function () {
 | 
			
		||||
      // classic build
 | 
			
		||||
      myListener = new Listeners(infosListener);
 | 
			
		||||
      fun_initStub();
 | 
			
		||||
 | 
			
		||||
      myListener.parser = stubParser; // replace the parser by my fake parser
 | 
			
		||||
      // fetch
 | 
			
		||||
      let res = myListener.fetchRSS();
 | 
			
		||||
      expect(myListener).to.not.be.undefined;
 | 
			
		||||
      if (myListener !== undefined) {
 | 
			
		||||
        // fetch
 | 
			
		||||
        let res = stubListener.fetchRSS();
 | 
			
		||||
 | 
			
		||||
      //assertion
 | 
			
		||||
      // calls
 | 
			
		||||
      expect(stubParser.parseURL).to.have.been.calledOnce;
 | 
			
		||||
      expect(stubParser.parseURL).to.have.been.calledWith(
 | 
			
		||||
        infosListener.address
 | 
			
		||||
      );
 | 
			
		||||
      // Promise
 | 
			
		||||
      //await expect(Promise.resolve(res)).to.be.eql(mockedRSSOutput);
 | 
			
		||||
      res
 | 
			
		||||
        .then((obj: any) => {
 | 
			
		||||
          expect(obj).to.be.eql(mockedRSSOutput);
 | 
			
		||||
        })
 | 
			
		||||
        .catch((err) => {
 | 
			
		||||
          expect(err).to.be.undefined;
 | 
			
		||||
        });
 | 
			
		||||
        //assertion
 | 
			
		||||
        // calls
 | 
			
		||||
        expect(stubParser.parseURL).to.have.been.calledOnce;
 | 
			
		||||
        expect(stubParser.parseURL).to.have.been.calledWith(
 | 
			
		||||
          infosListener.address
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        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");
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it("fetch with bad address", function () {
 | 
			
		||||
      // classic build
 | 
			
		||||
      myListener = new Listeners({
 | 
			
		||||
        name: "my-test-service",
 | 
			
		||||
        address: "bad.rss.service",
 | 
			
		||||
@@ -245,9 +260,9 @@ describe("test class RSS: jsonfile", function () {
 | 
			
		||||
          icon: ["media:group", "media:thumbnail"],
 | 
			
		||||
        },
 | 
			
		||||
      });
 | 
			
		||||
      myListener.parser = stubParser; // replace the parser by my fake parser
 | 
			
		||||
      fun_initStub();
 | 
			
		||||
      // fetch
 | 
			
		||||
      let res = myListener.fetchRSS();
 | 
			
		||||
      let res = stubListener.fetchRSS();
 | 
			
		||||
 | 
			
		||||
      //assertion
 | 
			
		||||
      // calls
 | 
			
		||||
@@ -265,86 +280,86 @@ describe("test class RSS: jsonfile", function () {
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  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();
 | 
			
		||||
      //   })
 | 
			
		||||
      // );
 | 
			
		||||
    it("Let's start the timer", async function () {
 | 
			
		||||
      let clock: sinon.default.SinonFakeTimers = sinon.default.useFakeTimers();
 | 
			
		||||
 | 
			
		||||
      // classic build
 | 
			
		||||
      myListener = new Listeners({
 | 
			
		||||
        name: "my-test-service",
 | 
			
		||||
        address: "fake.rss.service",
 | 
			
		||||
        timeloop: 2,
 | 
			
		||||
        timeloop: 60,
 | 
			
		||||
        customfields: {
 | 
			
		||||
          description: ["media:group", "media:description"],
 | 
			
		||||
          icon: ["media:group", "media:thumbnail"],
 | 
			
		||||
        },
 | 
			
		||||
      });
 | 
			
		||||
      fun_initStub();
 | 
			
		||||
      stubListener.fetchRSS.reset();
 | 
			
		||||
      stubListener.fetchRSS.resolves(mockedRSSOutput);
 | 
			
		||||
 | 
			
		||||
      //spy
 | 
			
		||||
      let fun_spy: sinon.default.SinonSpy = sinon.default.spy((obj) => {
 | 
			
		||||
        console.log("ok");
 | 
			
		||||
      let fun_spy: sinon.default.SinonSpy = sinon.default.spy((obj, err) => {
 | 
			
		||||
        expect(obj).to.be.eql(mockedRSSOutput);
 | 
			
		||||
        done();
 | 
			
		||||
        expect(err).to.be.eql(undefined);
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      // start timer
 | 
			
		||||
      myListener.start(fun_spy);
 | 
			
		||||
      stubListener.start(fun_spy);
 | 
			
		||||
 | 
			
		||||
      await clock.tickAsync(100);
 | 
			
		||||
      // wait and assertion
 | 
			
		||||
      // After 1ms
 | 
			
		||||
      await clock.tickAsync(1);
 | 
			
		||||
      expect(stubListener.fetchRSS).to.have.been.calledOnce;
 | 
			
		||||
      expect(fun_spy).to.have.been.calledOnce;
 | 
			
		||||
 | 
			
		||||
      //
 | 
			
		||||
      // await clock.tickAsync(4000);
 | 
			
		||||
      //
 | 
			
		||||
      // expect(fun_spy).to.have.been.calledThrice;
 | 
			
		||||
      // After 60s
 | 
			
		||||
      await clock.tickAsync(59999);
 | 
			
		||||
      expect(stubListener.fetchRSS).to.have.been.calledTwice;
 | 
			
		||||
      expect(fun_spy).to.have.been.calledTwice;
 | 
			
		||||
 | 
			
		||||
      myListener.stop();
 | 
			
		||||
      //assertion
 | 
			
		||||
      // calls
 | 
			
		||||
      // expect(fun_spy).to.have.been.callCount(3);
 | 
			
		||||
      // expect(fun_spy).to.have.been.calledWith(mockedRSSOutput, undefined);
 | 
			
		||||
      stubListener.stop();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it.skip("Let's start the timer (with a bad address)", async function () {
 | 
			
		||||
      //custom timeout
 | 
			
		||||
      // this.timeout(15000);
 | 
			
		||||
    it("Let's start the timer (with a bad address)", async function () {
 | 
			
		||||
      let clock: sinon.default.SinonFakeTimers = sinon.default.useFakeTimers();
 | 
			
		||||
 | 
			
		||||
      // classic build
 | 
			
		||||
      myListener = new Listeners({
 | 
			
		||||
        name: "my-test-service",
 | 
			
		||||
        address: "fake.rss.service",
 | 
			
		||||
        timeloop: 2,
 | 
			
		||||
        timeloop: 60,
 | 
			
		||||
        customfields: {
 | 
			
		||||
          description: ["media:group", "media:description"],
 | 
			
		||||
          icon: ["media:group", "media:thumbnail"],
 | 
			
		||||
        },
 | 
			
		||||
      });
 | 
			
		||||
      fun_initStub();
 | 
			
		||||
      stubListener.fetchRSS.reset();
 | 
			
		||||
      stubListener.fetchRSS.rejects(
 | 
			
		||||
        new Error("connect ECONNREFUSED 127.0.0.1:80")
 | 
			
		||||
      );
 | 
			
		||||
 | 
			
		||||
      //spy
 | 
			
		||||
      const fun_spy: sinon.default.SinonSpy = sinon.default.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
 | 
			
		||||
      myListener.start(fun_spy);
 | 
			
		||||
      stubListener.start(fun_spy);
 | 
			
		||||
 | 
			
		||||
      await new Promise((res) => setTimeout(res, 5 * 1000));
 | 
			
		||||
      // wait and assertion
 | 
			
		||||
      // After 1ms
 | 
			
		||||
      await clock.tickAsync(1);
 | 
			
		||||
      expect(stubListener.fetchRSS).to.have.been.calledOnce;
 | 
			
		||||
      expect(fun_spy).to.have.been.calledOnce;
 | 
			
		||||
 | 
			
		||||
      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(undefined, Error); //yagni
 | 
			
		||||
      // After 60s
 | 
			
		||||
      await clock.tickAsync(59999);
 | 
			
		||||
      expect(stubListener.fetchRSS).to.have.been.calledTwice;
 | 
			
		||||
      expect(fun_spy).to.have.been.calledTwice;
 | 
			
		||||
 | 
			
		||||
      stubListener.stop();
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user