1 import NetworkCalls from 'app/networking/NetworkCalls';
2 import * as sinon from "sinon";
4 describe("Network Utils", () => {
10 suite.sandbox = sinon.createSandbox();
14 suite.sandbox.reset();
17 describe('#fetchRequest', () => {
18 it('should fetch request', () => {
19 global.fetch = suite.sandbox.stub();
21 const then = suite.sandbox.stub();
23 fetch.returns({then});
25 NetworkCalls.fetchRequest("URL", "POST", "POST", "HEADER", "BODY");
26 sinon.assert.calledOnce(then);
28 expect(then.firstCall.args[0]({json: () => "json"})).toEqual("json");
29 sinon.assert.calledOnce(fetch);
33 describe('#fetchConfigurableViewRequest', () => {
34 it('fetch configurable request', () => {
39 componentDataDescriptor: {object: "object"}
42 const fetchPromise = Promise.resolve();
43 global.fetch = suite.sandbox.stub();
46 .withArgs(queryData.api, {
47 method: queryData.method,
48 headers: queryData.headers,
51 .returns(fetchPromise);
53 NetworkCalls.fetchConfigurableViewRequest(queryData);
55 sinon.assert.calledWith(fetch, "http://localhost:api", {
56 method: queryData.method,
57 headers: queryData.headers,
58 credentials: "same-origin",
59 body: '{"object":"object"}'
64 describe('#fetchRequestObj', () => {
65 it('fetch request object', () => {
67 const fetchPromise = Promise.resolve();
68 global.fetch = suite.sandbox.stub();
74 headers: 'POST_HEADER',
78 .returns(fetchPromise);
80 NetworkCalls.fetchRequestObj(url, "GET", "POST_HEADER", "BODY");
82 sinon.assert.calledWith(fetch, url, {
83 credentials: 'same-origin',
85 headers: "POST_HEADER",
91 describe('#getRequest', () => {
92 it("should fetch any request", () => {
94 global.fetch = suite.sandbox.stub();
95 const json = suite.sandbox.stub();
96 const url = "localhost";
100 credentials: 'same-origin',
106 const request = NetworkCalls.getRequest(url, "GET");
109 expect(request).toBe(json)
110 sinon.assert.calledOnce(global.fetch);
114 describe('#genericRequest', () => {
115 it('should fetch any generic request', () => {
117 global.fetch = suite.sandbox.stub();
118 const then = suite.sandbox.stub();
119 fetch.returns({then});
122 NetworkCalls.genericRequest("localhost", "/relativeUrl", "GET");
125 expect(then.firstCall.args[0]({json: () => "d"})).toEqual("d");
126 sinon.assert.calledOnce(fetch);
129 it('should fetch any generic request - non relative', () => {
131 global.fetch = suite.sandbox.stub();
132 const then = suite.sandbox.stub();
133 fetch.returns({then});
136 NetworkCalls.genericRequest("localhost", false, "GET");
139 expect(then.firstCall.args[0]({json: () => "d"})).toEqual("d");
140 sinon.assert.calledOnce(fetch);