a3b6176d2fc50d9010eb270e52305f52422a93c8
[aai/sparky-fe.git] / test / app / networking / NetworkCalls.test.js
1 import NetworkCalls from 'app/networking/NetworkCalls';
2 import * as sinon from "sinon";
3
4 describe("Network Utils", () => {
5
6   let suite;
7
8   beforeEach(() => {
9     suite = {};
10     suite.sandbox = sinon.createSandbox();
11   });
12
13   afterEach(() => {
14     suite.sandbox.reset();
15   });
16
17   describe('#fetchRequest', () => {
18     it('should fetch request', () => {
19       global.fetch = suite.sandbox.stub();
20
21       const then = suite.sandbox.stub();
22
23       fetch.returns({then});
24
25       NetworkCalls.fetchRequest("URL", "POST", "POST", "HEADER", "BODY");
26       sinon.assert.calledOnce(then);
27
28       expect(then.firstCall.args[0]({json: () => "json"})).toEqual("json");
29       sinon.assert.calledOnce(fetch);
30     });
31   });
32
33   describe('#fetchConfigurableViewRequest', () => {
34     it('fetch configurable request', () => {
35       const queryData = {
36         api: "api",
37         method: "method",
38         headers: "headers",
39         componentDataDescriptor: {object: "object"}
40       };
41
42       const fetchPromise = Promise.resolve();
43       global.fetch = suite.sandbox.stub();
44
45       global.fetch
46       .withArgs(queryData.api, {
47         method: queryData.method,
48         headers: queryData.headers,
49         body: queryData.body
50       })
51       .returns(fetchPromise);
52
53       NetworkCalls.fetchConfigurableViewRequest(queryData);
54
55       sinon.assert.calledWith(fetch, "http://localhost:api", {
56         method: queryData.method,
57         headers: queryData.headers,
58         credentials: "same-origin",
59         body: '{"object":"object"}'
60       });
61     });
62   });
63
64   describe('#fetchRequestObj', () => {
65     it('fetch request object', () => {
66
67       const fetchPromise = Promise.resolve();
68       global.fetch = suite.sandbox.stub();
69       const url = 'url';
70
71       global.fetch
72       .withArgs(url, {
73         method: 'GET',
74         headers: 'POST_HEADER',
75         body: 'BODY'
76
77       })
78       .returns(fetchPromise);
79
80       NetworkCalls.fetchRequestObj(url, "GET", "POST_HEADER", "BODY");
81
82       sinon.assert.calledWith(fetch, url, {
83         credentials: 'same-origin',
84         method: "GET",
85         headers: "POST_HEADER",
86         body: "BODY"
87       });
88     });
89   });
90
91   describe('#getRequest', () => {
92     it("should fetch any request", () => {
93         // given
94         global.fetch = suite.sandbox.stub();
95         const json = suite.sandbox.stub();
96         const url = "localhost";
97
98         global.fetch
99             .withArgs(url, {
100                 credentials: 'same-origin',
101                 method: 'GET'
102             })
103             .returns(json);
104
105         // when
106         const request = NetworkCalls.getRequest(url, "GET");
107
108         //then
109         expect(request).toBe(json)
110         sinon.assert.calledOnce(global.fetch);
111     });
112   });
113
114   describe('#genericRequest', () => {
115     it('should fetch any generic request', () => {
116       // given
117       global.fetch = suite.sandbox.stub();
118       const then = suite.sandbox.stub();
119       fetch.returns({then});
120
121       // when
122       NetworkCalls.genericRequest("localhost", "/relativeUrl", "GET");
123
124       // then
125       expect(then.firstCall.args[0]({json: () => "d"})).toEqual("d");
126       sinon.assert.calledOnce(fetch);
127     });
128
129     it('should fetch any generic request - non relative', () => {
130       // given
131       global.fetch = suite.sandbox.stub();
132       const then = suite.sandbox.stub();
133       fetch.returns({then});
134
135       // when
136       NetworkCalls.genericRequest("localhost", false, "GET");
137
138       // then
139       expect(then.firstCall.args[0]({json: () => "d"})).toEqual("d");
140       sinon.assert.calledOnce(fetch);
141     });
142   });
143 });