youtube-listener-rss/tests/index-spec.ts

135 lines
3.8 KiB
TypeScript
Raw Normal View History

// tested class
import { YoutubeListenerRss } from "./../src/";
// Unit test
import nock from "nock";
import * as chai from "chai";
import sinon from "ts-sinon";
import sinonChai from "sinon-chai";
chai.use(sinonChai);
const expect = chai.expect;
// default value (more easy when it's aliases)
const default_channel_ID = "UCh2YBKhYIy-_LtfCIn2Jycg";
const default_timeloop = 15;
// expected value during my test
const expected_default_address = "http://www.youtube.com/feeds/videos.xml?channel_id=".concat(
default_channel_ID
);
const expected_custom_fields = {
description: ["media:group", "media:description"],
icon: ["media:group", "media:thumbnail"],
};
const expected_resolve_fetch = {}; // TODO charger le fichier "youtube_feed.rss"
// let's test
describe("test ytbListener_RSS class", function () {
describe("test constructor", function () {
it("construct with 2 params", function () {
// given
const listener = new YoutubeListenerRss(
default_channel_ID,
default_timeloop // 15 sec
);
// assertions
expect(listener.address).to.be.eql(expected_default_address);
expect(listener.timeloop).to.be.eql(default_timeloop);
expect(listener.customfields).to.be.eql(expected_custom_fields);
});
it("construct with 1 params (without timeloop)", function () {
// given
const listener = new YoutubeListenerRss(
default_channel_ID,
default_timeloop // 15 sec
);
// assertions
expect(listener.address).to.be.eql(expected_default_address);
expect(listener.timeloop).to.be.eql(5 * 60);
expect(listener.customfields).to.be.eql(expected_custom_fields);
});
});
describe("test fetch", function () {
it("fetches", async function () {
// given
nock.disableNetConnect();
nock("http://www.youtube.com")
.get("/feeds/videos.xml?channel_id=UCh2YBKhYIy-_LtfCIn2Jycg")
.replyWithFile(200, __dirname + "RessourcesTest/youtube_feed.rss", {
"content-type": "text/xml",
charset: "UTF-8",
});
const listener = new YoutubeListenerRss(
default_channel_ID,
default_timeloop
);
//when
const res = await listener.fetchRSS();
// assertion
expect(res).to.be.eql(expected_resolve_fetch);
});
});
describe("start", function () {
it("fetches with start (bad name, find a better)", async function () {
// given
const clock = sinon.useFakeTimers();
nock.disableNetConnect();
nock("http://www.youtube.com")
.get("/feeds/videos.xml?channel_id=UCh2YBKhYIy-_LtfCIn2Jycg")
.replyWithFile(200, __dirname + "RessourcesTest/youtube_feed.rss", {
"content-type": "text/xml",
charset: "UTF-8",
});
const listener = new YoutubeListenerRss(
default_channel_ID,
default_timeloop
);
// spy
const updateListenerSpy = sinon.spy();
const newEntriesListenerSpy = sinon.spy();
// start timer
listener.on("update", updateListenerSpy);
listener.on("newnEntries", newEntriesListenerSpy);
listener.start();
// when
await clock.tickAsync(1);
// assertion
expect(updateListenerSpy).to.have.been.calledOnce;
expect(updateListenerSpy).to.have.been.calledWith(expected_resolve_fetch);
// todo update the rss feed
// when
await clock.tickAsync(default_timeloop + 1);
// assertion
expect(updateListenerSpy).to.have.been.calledTwice;
expect(updateListenerSpy).to.have.been.calledWith(expected_resolve_fetch);
expect(newEntriesListenerSpy).to.have.been.calledOnce;
expect(
newEntriesListenerSpy
).to.have.been.calledWith(/* Put the added item */);
// then
listener.stop();
});
});
});