From 30f5e576d07f156ba768690eed0e6b7cb80c6e60 Mon Sep 17 00:00:00 2001 From: Amaury Joly Date: Mon, 8 Mar 2021 09:27:32 +0100 Subject: [PATCH] add new test + some little refactors --- tests/index-spec.ts | 85 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 10 deletions(-) diff --git a/tests/index-spec.ts b/tests/index-spec.ts index c2b1dd4..9abc0f4 100644 --- a/tests/index-spec.ts +++ b/tests/index-spec.ts @@ -32,15 +32,15 @@ describe("test class RSS: jsonfile", function () { const mockedRSSOutput: Parser.Output = { items: [ { - title: "my title 00", + title: "my title 02", "media:group": { - "media:description": "my description 00", + "media:description": "my description 02", "media:thumbnail": [ - { $: { height: 360, width: 420, url: "my_image00.jpg" } }, + { $: { height: 360, width: 420, url: "my_image02.jpg" } }, ], }, - link: "my_url_00.com", - pubDate: "myDate00", + link: "my_url_02.com", + pubDate: "myDate02", }, { title: "my title 01", @@ -54,15 +54,15 @@ describe("test class RSS: jsonfile", function () { pubDate: "myDate01", }, { - title: "my title 02", + title: "my title 00", "media:group": { - "media:description": "my description 02", + "media:description": "my description 00", "media:thumbnail": [ - { $: { height: 360, width: 420, url: "my_image02.jpg" } }, + { $: { height: 360, width: 420, url: "my_image00.jpg" } }, ], }, - link: "my_url_02.com", - pubDate: "myDate02", + link: "my_url_00.com", + pubDate: "myDate00", }, ], }; @@ -366,5 +366,70 @@ describe("test class RSS: jsonfile", function () { myListener.stop(); }); + + it("notifies with 'newEntries' when a new entry is detected", async function () { + // given + const clock = sinon.useFakeTimers(); + + const mockManager = ImportMock.mockClassInPlace(Parser); + const stubParser = mockManager.mock("parseURL"); + stubParser.resolves(mockedRSSOutput); + const newEntry = { + 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", + }; + const newRSSOutput = { + ...mockedRSSOutput, + items: [newEntry, ...mockedRSSOutput.items], + }; + + // classic build + const myListener = new Listeners({ + ...infosListener, + timeloop: 60, + }); + + //spy + const updateListenerSpy = sinon.spy(); + const newEntriesListenerSpy = sinon.spy(); + + myListener.on("update", updateListenerSpy); + myListener.on("newEntries", newEntriesListenerSpy); + + // when + myListener.start(); + + // then + await clock.tickAsync(1); + expect(updateListenerSpy).to.have.been.calledOnce; + expect(newEntriesListenerSpy).to.not.have.been.called; + + // given + stubParser.resolves(newRSSOutput); + + // then + await clock.tickAsync(60000); + expect(updateListenerSpy).to.have.been.calledTwice; + expect(newEntriesListenerSpy).to.have.been.calledOnce; + expect(newEntriesListenerSpy).to.have.been.calledWith([newEntry]); + + // given + newEntriesListenerSpy.resetHistory(); + + // then + await clock.tickAsync(60000); + expect(updateListenerSpy).to.have.been.calledThrice; + expect(updateListenerSpy).to.have.been.calledWith(mockedRSSOutput); + expect(newEntriesListenerSpy).to.not.have.been.called; + + myListener.stop(); + }); }); });