Adding missing packege
[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       const json = suite.sandbox.stub();
94       const fetchPromise = Promise.resolve({json});
95       global.fetch = suite.sandbox.stub();
96
97       global.fetch
98       .withArgs('URL', {
99         credentials: 'same-origin',
100         method: 'GET'
101       })
102       .returns(fetchPromise);
103
104       NetworkCalls.getRequest("URL", "GET");
105
106       return fetchPromise.then(() => {
107         sinon.assert.calledOnce(json);
108       });
109     });
110   });
111
112   describe('#genericRequest', () => {
113     it('should fetch any generic request', () => {
114       global.fetch = suite.sandbox.stub();
115       const then = suite.sandbox.stub();
116       fetch.returns({then});
117       NetworkCalls.genericRequest("localhost", "/relativeUrl", "GET");
118
119       expect(then.firstCall.args[0]({json: () => "d"})).toEqual("d");
120
121       sinon.assert.calledOnce(fetch);
122     });
123   });
124 });