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