From 12af2f20c2313a3fc1fe5761eaf2e885d3c8b751 Mon Sep 17 00:00:00 2001 From: Mariusz Sygnowski Date: Tue, 9 Apr 2019 11:32:23 +0200 Subject: Fixing failing test + covarage improvement Change-Id: Id13e334cc1493274081758079a1f896f35af6b80 Issue-ID: AAI-2341 Signed-off-by: Mariusz Sygnowski --- test/app/networking/NetworkCalls.test.js | 51 ++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/test/app/networking/NetworkCalls.test.js b/test/app/networking/NetworkCalls.test.js index 373fbac..a3b6176 100644 --- a/test/app/networking/NetworkCalls.test.js +++ b/test/app/networking/NetworkCalls.test.js @@ -90,34 +90,53 @@ describe("Network Utils", () => { describe('#getRequest', () => { it("should fetch any request", () => { - const json = suite.sandbox.stub(); - const fetchPromise = Promise.resolve({json}); - global.fetch = suite.sandbox.stub(); - - global.fetch - .withArgs('URL', { - credentials: 'same-origin', - method: 'GET' - }) - .returns(fetchPromise); - - NetworkCalls.getRequest("URL", "GET"); - - return fetchPromise.then(() => { - sinon.assert.calledOnce(json); - }); + // given + global.fetch = suite.sandbox.stub(); + const json = suite.sandbox.stub(); + const url = "localhost"; + + global.fetch + .withArgs(url, { + credentials: 'same-origin', + method: 'GET' + }) + .returns(json); + + // when + const request = NetworkCalls.getRequest(url, "GET"); + + //then + expect(request).toBe(json) + sinon.assert.calledOnce(global.fetch); }); }); describe('#genericRequest', () => { it('should fetch any generic request', () => { + // given global.fetch = suite.sandbox.stub(); const then = suite.sandbox.stub(); fetch.returns({then}); + + // when NetworkCalls.genericRequest("localhost", "/relativeUrl", "GET"); + // then expect(then.firstCall.args[0]({json: () => "d"})).toEqual("d"); + sinon.assert.calledOnce(fetch); + }); + it('should fetch any generic request - non relative', () => { + // given + global.fetch = suite.sandbox.stub(); + const then = suite.sandbox.stub(); + fetch.returns({then}); + + // when + NetworkCalls.genericRequest("localhost", false, "GET"); + + // then + expect(then.firstCall.args[0]({json: () => "d"})).toEqual("d"); sinon.assert.calledOnce(fetch); }); }); -- cgit 1.2.3-korg