-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient-get-component-data.js
More file actions
169 lines (149 loc) · 4.26 KB
/
client-get-component-data.js
File metadata and controls
169 lines (149 loc) · 4.26 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const injectr = require('injectr');
const sinon = require('sinon');
describe('client : get-component-data', () => {
describe('when invoked for one component', () => {
const baseUrl = 'http://localhost:3030';
const resultUrl = 'http://localhost:3030/hello-world/1.0.0/?p1=v1&p2=v2';
const minimalRequestStub = sinon.stub().yields(true);
const serverStub = sinon.stub().returns(baseUrl);
const prepareServerGetStub = sinon.stub().returns(resultUrl);
const options = {
parameters: { p1: 'v1', p2: 'v2' },
headers: { 'header-a': 'header-value b' },
httpMethod: 'POST',
timeout: 10
};
before(done => {
const getComponentDataPrototype = injectr(
'../../src/get-components-data.js',
{
'minimal-request': minimalRequestStub,
'./href-builder': function() {
return {
server: serverStub,
prepareServerGet: prepareServerGetStub
};
}
}
);
const getComponentData = new getComponentDataPrototype({
registries: { serverRendering: baseUrl }
});
const component = {
name: 'hello-world',
version: '1.0.0'
};
getComponentData(
[
{
component: component,
container: false,
pos: 0,
render: 'server',
result: {}
}
],
options,
() => {
done();
}
);
});
it('href-builder prepareServerGet method is invoked', () => {
sinon.assert.calledOnce(serverStub);
sinon.assert.calledOnce(prepareServerGetStub);
sinon.assert.calledWith(
prepareServerGetStub,
baseUrl,
{ name: 'hello-world', version: '1.0.0' },
options
);
});
it('a GET request is made with the URL returned by href-builder.prepareServerGet method', () => {
sinon.assert.calledWith(minimalRequestStub, {
headers: options.headers,
json: true,
method: 'get',
timeout: 10,
url: resultUrl
});
});
});
describe('when invoked for two components', () => {
const baseUrl = 'http://localhost:3030';
const minimalRequestStub = sinon.stub().yields(true); //Returns error for every request
const serverStub = sinon.stub().returns(baseUrl);
const prepareServerGetStub = sinon.stub().returns(baseUrl + '/test-route'); // Invalid - should never be called
const options = {
parameters: { p1: 'v1', p2: 'v2' },
headers: { 'header-a': 'header-value b' },
httpMethod: 'GET',
timeout: 10
};
const component0 = {
name: 'hello-world',
version: '1.0.0'
};
const component1 = {
name: 'test-component',
version: '2.0.0'
};
before(done => {
const getComponentDataPrototype = injectr(
'../../src/get-components-data.js',
{
'minimal-request': minimalRequestStub,
'./href-builder': function() {
return {
server: serverStub,
prepareServerGet: prepareServerGetStub
};
}
}
);
const getComponentData = new getComponentDataPrototype({
registries: { serverRendering: baseUrl }
});
getComponentData(
[
{
component: component0,
container: false,
pos: 0,
render: 'server',
result: {}
},
{
component: component1,
container: false,
pos: 1,
render: 'server',
result: {}
}
],
options,
() => {
done();
}
);
});
it('href-builder prepareServerGet method is not invoked', () => {
sinon.assert.calledOnce(serverStub);
sinon.assert.notCalled(prepareServerGetStub);
});
it('a POST request is made with the URL returned by href-builder.server method', () => {
sinon.assert.calledWith(minimalRequestStub, {
headers: options.headers,
json: true,
method: 'post',
timeout: 10,
url: baseUrl,
body: {
components: [component0, component1],
parameters: options.parameters
}
});
});
});
});