Ajout de test

Fix execution test
Quelques renames
This commit is contained in:
Amaury Joly 2021-01-30 14:02:59 +01:00
parent f1b1e23792
commit fa6f1abadb
7 changed files with 75 additions and 39 deletions

View File

@ -170,3 +170,11 @@ if (process.argv[2] && process.argv[2] === '--service') {
}) })
} }
module.exports = {
AbstractListenerRSSBuilder: require('./src/Models/Builders/AbstractListenerRSSBuilder'),
YoutubeListenerRSSBuilder: require('./src/Models/Builders/YoutubeListenerRSSBuilder'),
ListenerRssInfos: require('./src/Models/ListenerRSSInfos'),
ListenerBuildDirector: require('./src/ListenerDirector'),
ListenerRss: require('./src/ListenerRss')
}

View File

@ -1,5 +1,3 @@
const YoutubeListenerRSSBuilder = require('./Models/YoutubeListenerRSSBuilder');
class ListenerBuildDirector { class ListenerBuildDirector {
_builder = undefined; _builder = undefined;
@ -9,7 +7,7 @@ class ListenerBuildDirector {
} }
getListener() { getListener() {
return this._builder.listenerRSS(); return this._builder.listenerRSS;
} }
build(infos) { build(infos) {

View File

@ -1,4 +1,3 @@
const jsonFile = require('jsonfile');
const Parser = require('rss-parser'); const Parser = require('rss-parser');
@ -14,20 +13,29 @@ class ListenerRss {
loopRunning = false; loopRunning = false;
constructor(info) { constructor(info) {
if(info !== undefined) {
this.setData(info);
this.setParser();
}
}
setParser() {
// set parser // set parser
this.parser = new Parser({ this.parser = new Parser(this.customfields !== undefined ? {
customFields : { customFields: {
item: info._customfields.map((elt) => { item: this.customfields.map((elt) => {
return Array.isArray(elt[1]) ? elt[1][0] : elt[1]; return Array.isArray(elt[1]) ? elt[1][0] : elt[1];
}) })
} }
}); } : undefined); // if customfield is set -> let's set the parser with, else let the option empty
}
setData(infos) {
// Set data // Set data
this.name = info._name === undefined ? info._address : info._name; // if name is undefined let's take the address this.name = infos._name === undefined ? infos._address : infos._name; // if name is undefined let's take the address
this.address = info._address; this.address = infos._address;
this.timeloop = info._timeloop; this.timeloop = infos._timeloop;
this.customfields = info._customfields; this.customfields = infos._customfields;
} }
fetchRSS() { fetchRSS() {

View File

@ -1,10 +1,10 @@
const ListenerRSS = require('../listener-rss'); const ListenerRSS = require('../../ListenerRss')
class AbstractListnerRSSBuilder { class AbstractListenerRSSBuilder {
_listenerRSS = undefined; _listenerRSS = undefined;
constructor() { constructor() {
if(this.constructor === AbstractListnerRSSBuilder) if(this.constructor === AbstractListenerRSSBuilder)
throw new Error('The Abstract class "AbstractListnerRSSBuilder" cannot be instantiated'); throw new Error('The Abstract class "AbstractListnerRSSBuilder" cannot be instantiated');
} }
@ -17,6 +17,7 @@ class AbstractListnerRSSBuilder {
this.setInfos(infos); this.setInfos(infos);
this.setSpecificInfos(); this.setSpecificInfos();
this.listenerRSS.setParser()
} }
/** /**
@ -31,10 +32,7 @@ class AbstractListnerRSSBuilder {
} }
setInfos(infos) { // Nominal Infos (like name, addresse, and other) setInfos(infos) { // Nominal Infos (like name, addresse, and other)
this._listenerRSS.name = infos._name; this._listenerRSS.setData(infos);
this._listenerRSS.address = infos._address;
this._listenerRSS.timeloop = infos._timeloop;
this._listenerRSS.name = infos._name;
} }
setSpecificInfos() { // More generic infos who's depend of platforms setSpecificInfos() { // More generic infos who's depend of platforms
@ -44,4 +42,4 @@ class AbstractListnerRSSBuilder {
} }
module.exports = AbstractListnerRSSBuilder module.exports = AbstractListenerRSSBuilder

View File

@ -1,4 +1,4 @@
const AbstractListenerRSSBuilder = require('AbstractListenerRSSBuilder') const AbstractListenerRSSBuilder = require('./AbstractListenerRSSBuilder');
class YoutubeListenerRSSBuilder extends AbstractListenerRSSBuilder { class YoutubeListenerRSSBuilder extends AbstractListenerRSSBuilder {
constructor() { constructor() {
@ -6,7 +6,7 @@ class YoutubeListenerRSSBuilder extends AbstractListenerRSSBuilder {
} }
setSpecificInfos() { setSpecificInfos() {
this.listenerRSS._customfields = [ this.listenerRSS.customfields = [
['description', ['media:group', 'media:description']], ['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']] ['icon', ['media:group', 'media:thumbnail']]
] ]

View File

@ -39,4 +39,4 @@ class ListenerRSSInfos {
} }
} }
module.exports = ListenerRSSInfos module.exports = ListenerRSSInfos;

View File

@ -1,13 +1,20 @@
// external lib
const Parser = require("rss-parser");
// tested class
const ListenerRssPackage = require("../index");
const Listeners = ListenerRssPackage.ListenerRss
const ListenerRRSInfo = ListenerRssPackage.ListenerRssInfos
const YtbBuilder = ListenerRssPackage.YoutubeListenerRSSBuilder
const Director = ListenerRssPackage.ListenerBuildDirector
// Unit test
const chai = require("chai"); const chai = require("chai");
const sinon = require("sinon"); const sinon = require("sinon");
const sinon_chai = require("sinon-chai"); const sinon_chai = require("sinon-chai");
chai.use(sinon_chai); chai.use(sinon_chai);
const Parser = require("rss-parser");
const Listeners = require('../src/listener-rss')
const ListenerRRSInfo = require('../src/Models/listenerRSSInfos')
const YtbBuilder = require('../src/Models/YoutubeListenerRSSBuilder')
const YtbDirector = require('../src/listener-director')
const expect = chai.expect; const expect = chai.expect;
@ -58,11 +65,11 @@ describe("test class RSS: jsonfile", function () {
beforeEach(function () { beforeEach(function () {
// stubs // stubs
stubParser = sinon.stub(Parser.prototype, 'parseURL') stubParser = sinon.stub(Parser.prototype, 'parseURL')
.withArgs(listenerInfo.address) .withArgs(infosListener.address)
.resolves(mockedRSSOutput); .resolves(mockedRSSOutput);
// constructor // constructor
myListener = new Listeners(); myListener = undefined;
}) })
afterEach(function () { afterEach(function () {
@ -71,27 +78,44 @@ describe("test class RSS: jsonfile", function () {
}); });
describe("Building Ytb listener", function () { describe("Building Ytb listener", function () {
it("The build without problems", function () { it("The build without issues", function () {
let builder = new YtbBuilder(); let builder = new YtbBuilder();
let director = new YtbDirector(builder); let director = new Director(builder);
director.build(infosListener); director.build(infosListener);
myListener = director.getListener(); myListener = director.getListener();
// assertions // assertions
expect(stubParser).to.have.been.calledOnce; // myListener data
expect(stubParser).to.have.been.calledWith(infosListener._address); expect(myListener.timeloop).to.eql(infosListener._timeloop);
expect(myListener.customFields).to.eql([ expect(myListener.name).to.eql(infosListener._name)
expect(myListener.address).to.eql(infosListener._address)
expect(myListener.customfields).to.eql([
['description', ['media:group', 'media:description']], ['description', ['media:group', 'media:description']],
['icon', ['media:group', 'media:thumbnail']] ['icon', ['media:group', 'media:thumbnail']]
]); ]);
//TODO test les infos dans "myListener"
}); });
}); });
//Todo describe("fetch some data", function () {
it("fetch without issues", function () {
// classic build
let builder = new YtbBuilder();
let director = new Director(builder);
director.build(infosListener);
myListener = director.getListener();
// fetch
myListener.fetchRSS();
//assertion
// calls
expect(stubParser).to.have.been.calledOnce;
expect(stubParser).to.have.been.calledWith(infosListener._address);
})
})
describe.skip("start", function () { describe.skip("start", function () {
it("Let's start the timer", function () { it("Let's start the timer", function () {
myListener.setDatas(listenerInfo); myListener.setDatas(infosListener);
myListener.start(); myListener.start();