371 lines
11 KiB
TypeScript
371 lines
11 KiB
TypeScript
// external lib
|
|
import * as Parser from "rss-parser";
|
|
|
|
// tested class
|
|
import {
|
|
ListenerRSSInfos as ListenerRRSInfo,
|
|
ListenerRss as Listeners,
|
|
} from "./../src/index";
|
|
|
|
// Unit test
|
|
import assert from "assert";
|
|
import * as chai from "chai";
|
|
import sinon from "ts-sinon";
|
|
import sinonChai from "sinon-chai";
|
|
import { ImportMock, InPlaceMockManager } from "ts-mock-imports";
|
|
|
|
chai.use(sinonChai);
|
|
|
|
const expect = chai.expect;
|
|
|
|
describe("test class RSS: jsonfile", function () {
|
|
const infosListener: ListenerRRSInfo = {
|
|
name: "my-test-service",
|
|
address: "fake.rss.service",
|
|
timeloop: 15,
|
|
customfields: {
|
|
description: ["media:group", "media:description"],
|
|
icon: ["media:group", "media:thumbnail"],
|
|
},
|
|
};
|
|
|
|
const mockedRSSOutput: Parser.Output<any> = {
|
|
items: [
|
|
{
|
|
title: "my title 00",
|
|
"media:group": {
|
|
"media:description": "my description 00",
|
|
"media:thumbnail": [
|
|
{ $: { height: 360, width: 420, url: "my_image00.jpg" } },
|
|
],
|
|
},
|
|
link: "my_url_00.com",
|
|
pubDate: "myDate00",
|
|
},
|
|
{
|
|
title: "my title 01",
|
|
"media:group": {
|
|
"media:description": "my description 01",
|
|
"media:thumbnail": [
|
|
{ $: { height: 360, width: 420, url: "my_image01.jpg" } },
|
|
],
|
|
},
|
|
link: "my_url_01.com",
|
|
pubDate: "myDate01",
|
|
},
|
|
{
|
|
title: "my title 02",
|
|
"media:group": {
|
|
"media:description": "my description 02",
|
|
"media:thumbnail": [
|
|
{ $: { height: 360, width: 420, url: "my_image02.jpg" } },
|
|
],
|
|
},
|
|
link: "my_url_02.com",
|
|
pubDate: "myDate02",
|
|
},
|
|
],
|
|
};
|
|
|
|
describe("Building Ytb listener", function () {
|
|
it("builds without issues (infosListener parameters)", function () {
|
|
const myListener = new Listeners(infosListener);
|
|
|
|
// 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("builds without issues (raw infos : 4 params)", function () {
|
|
const 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("builds without issues (raw infos : just 2 params)", function () {
|
|
const 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("builds without issues (raw infos: just 3 params (no custom fields))", function () {
|
|
const 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 () {
|
|
const 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("data fetching", function () {
|
|
it("fetches without issues", async function () {
|
|
// given
|
|
const mockManager: InPlaceMockManager<Parser> = ImportMock.mockClassInPlace<Parser>(
|
|
Parser
|
|
);
|
|
const stubParser = mockManager.mock("parseURL");
|
|
stubParser.resolves(mockedRSSOutput);
|
|
|
|
const myListener = new Listeners(infosListener);
|
|
|
|
// when
|
|
const res = await myListener.fetchRSS();
|
|
|
|
// then
|
|
expect(stubParser).to.have.been.calledOnce;
|
|
expect(stubParser).to.have.been.calledWith(infosListener.address);
|
|
expect(res).to.be.eql(mockedRSSOutput);
|
|
});
|
|
|
|
it("rejects when fetching fails", async function () {
|
|
// given
|
|
const mockManager: InPlaceMockManager<Parser> = ImportMock.mockClassInPlace<Parser>(
|
|
Parser
|
|
);
|
|
const stubParser = mockManager.mock("parseURL");
|
|
const err = new Error("connect ECONNREFUSED 127.0.0.1:80");
|
|
stubParser.rejects(err);
|
|
|
|
const myListener = new Listeners({
|
|
name: "my-test-service",
|
|
address: "bad.rss.service",
|
|
customfields: {
|
|
description: ["media:group", "media:description"],
|
|
icon: ["media:group", "media:thumbnail"],
|
|
},
|
|
});
|
|
// when
|
|
await assert.rejects(() => myListener.fetchRSS(), err);
|
|
});
|
|
});
|
|
|
|
describe("start", function () {
|
|
it("fetches immediately the RSS information", async function () {
|
|
// given
|
|
const clock = sinon.useFakeTimers();
|
|
|
|
const mockManager: InPlaceMockManager<Parser> = ImportMock.mockClassInPlace<Parser>(
|
|
Parser
|
|
);
|
|
const stubParser = mockManager.mock("parseURL");
|
|
stubParser.resolves(mockedRSSOutput);
|
|
|
|
// classic build
|
|
const myListener = new Listeners(infosListener);
|
|
|
|
//spy
|
|
const updateListenerSpy = sinon.spy();
|
|
|
|
// start timer
|
|
myListener.on("update", updateListenerSpy);
|
|
|
|
myListener.start();
|
|
|
|
// when
|
|
await clock.tickAsync(1);
|
|
|
|
// then
|
|
expect(updateListenerSpy).to.have.been.calledOnce;
|
|
expect(updateListenerSpy).to.have.been.calledWith(mockedRSSOutput);
|
|
expect(stubParser).to.have.calledWith(myListener.address);
|
|
myListener.stop();
|
|
});
|
|
|
|
it("has fetched multiple times after a while", async function () {
|
|
// given
|
|
const clock = sinon.useFakeTimers();
|
|
|
|
const mockManager: InPlaceMockManager<Parser> = ImportMock.mockClassInPlace<Parser>(
|
|
Parser
|
|
);
|
|
const stubParser = mockManager.mock("parseURL");
|
|
stubParser.resolves(mockedRSSOutput);
|
|
|
|
// classic build
|
|
const myListener = new Listeners({
|
|
...infosListener,
|
|
timeloop: 15,
|
|
});
|
|
|
|
//spy
|
|
const updateListenerSpy: sinon.SinonSpy = sinon.spy();
|
|
const newRSSOutput = {
|
|
...mockedRSSOutput,
|
|
items: mockedRSSOutput.items.concat({
|
|
title: "my title 03",
|
|
"media:group": {
|
|
"media:description": "my description 03",
|
|
"media:thumbnail": [
|
|
{ $: { height: 360, width: 420, url: "my_image03.jpg" } },
|
|
],
|
|
},
|
|
link: "my_url_03.com",
|
|
pubDate: "myDate03",
|
|
}),
|
|
};
|
|
|
|
// start timer
|
|
myListener.on("update", updateListenerSpy);
|
|
|
|
myListener.start();
|
|
|
|
// when
|
|
await clock.tickAsync(1);
|
|
stubParser.resolves(newRSSOutput);
|
|
await clock.tickAsync(60000);
|
|
|
|
// then
|
|
expect(updateListenerSpy).to.have.been.callCount(5);
|
|
expect(updateListenerSpy.firstCall).to.have.been.calledWith(
|
|
mockedRSSOutput
|
|
);
|
|
expect(updateListenerSpy.secondCall).to.have.been.calledWith(
|
|
newRSSOutput
|
|
);
|
|
|
|
myListener.stop();
|
|
});
|
|
|
|
it("notifies with a 'error' when fetching fails", async function () {
|
|
const clock = sinon.useFakeTimers();
|
|
|
|
const mockManager: InPlaceMockManager<Parser> = ImportMock.mockClassInPlace<Parser>(
|
|
Parser
|
|
);
|
|
const stubParser = mockManager.mock("parseURL");
|
|
const expectedErr = new Error("connect ECONNREFUSED 127.0.0.1:80");
|
|
stubParser.rejects(expectedErr);
|
|
|
|
// classic build
|
|
const myListener = new Listeners({
|
|
...infosListener,
|
|
timeloop: 60,
|
|
});
|
|
|
|
//spy
|
|
const updateListenerSpy = sinon.spy();
|
|
const updateErrorListenerSpy = sinon.spy();
|
|
|
|
myListener.on("error", updateErrorListenerSpy);
|
|
myListener.on("update", updateListenerSpy);
|
|
|
|
// start timer
|
|
myListener.start();
|
|
|
|
// wait and assertion
|
|
// After 1ms
|
|
await clock.tickAsync(1);
|
|
expect(updateErrorListenerSpy).to.have.been.calledOnce;
|
|
expect(updateListenerSpy).to.not.have.been.called;
|
|
expect(updateErrorListenerSpy).to.have.been.calledWith(expectedErr);
|
|
|
|
// After 60s
|
|
await clock.tickAsync(60000);
|
|
expect(updateErrorListenerSpy).to.have.been.calledTwice;
|
|
expect(updateListenerSpy).to.not.have.been.called;
|
|
|
|
// When the service is back
|
|
stubParser.resolves(mockedRSSOutput);
|
|
await clock.tickAsync(60000);
|
|
|
|
expect(updateListenerSpy).to.have.been.calledOnce;
|
|
expect(updateListenerSpy).to.have.been.calledWith(mockedRSSOutput);
|
|
|
|
myListener.stop();
|
|
});
|
|
});
|
|
});
|