Skip to content

Commit 014df3b

Browse files
committed
Merge branch 'feature/stub/json' into develop
2 parents 84b13ec + d979d82 commit 014df3b

File tree

11 files changed

+162
-9
lines changed

11 files changed

+162
-9
lines changed

app/constants/Server.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/services/Area.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ const Area = {
77
},
88

99
getConfig() {
10-
return {
10+
let config = {
1111
headers: {
1212
Authorization : window.localStorage.getItem('token')
13-
}
13+
},
14+
data: {}
1415
};
16+
17+
if (jsonStubHeaders != undefined && jsonStubHeaders != '') {
18+
Object.assign(config.headers, JSON.parse(jsonStubHeaders));
19+
}
20+
return config;
1521
},
1622

1723
getAll() {

app/services/Client.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ const Client = {
77
},
88

99
getConfig() {
10-
return {
10+
let config = {
1111
headers: {
1212
Authorization : window.localStorage.getItem('token')
13-
}
13+
},
14+
data: {}
1415
};
16+
17+
if (jsonStubHeaders != undefined && jsonStubHeaders != '') {
18+
Object.assign(config.headers, JSON.parse(jsonStubHeaders));
19+
}
20+
return config;
1521
},
1622

1723
getClients() {

app/services/User.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
import axios from 'axios';
22

33
const User = {
4+
5+
getConfig() {
6+
let config = {
7+
headers: {
8+
}
9+
};
10+
11+
if (jsonStubHeaders != undefined && jsonStubHeaders != '') {
12+
Object.assign(config.headers, JSON.parse(jsonStubHeaders));
13+
}
14+
return config;
15+
},
16+
417
login(username, password) {
518
let auth = {
619
username: username,
720
password: password
821
};
9-
return axios.post(`${HOST}/authenticate`, auth);
22+
return axios.post(`${HOST}/authenticate`, auth, this.getConfig());
1023
}
1124
};
1225

app/services/Visit.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ const Visit = {
77
},
88

99
getConfig() {
10-
return {
10+
let config = {
1111
headers: {
1212
Authorization : window.localStorage.getItem('token')
13-
}
13+
},
14+
data: {}
1415
};
16+
17+
if (jsonStubHeaders != undefined && jsonStubHeaders != '') {
18+
Object.assign(config.headers, JSON.parse(jsonStubHeaders));
19+
}
20+
return config;
1521
},
1622

1723
getGroupByArea() {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
],
4949
"collectCoverage": true,
5050
"globals": {
51-
"HOST": "http://localhost:3000"
51+
"HOST": "http://localhost:3000",
52+
"jsonStubHeaders": ""
5253
},
5354
"moduleNameMapper": {
5455
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/tests/__mocks__/fileMock.js",

tests/Area.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,35 @@ describe('Test Area', () => {
8989
}, 0);
9090
});
9191

92+
it('Should show headers to stub json', (done) => {
93+
94+
let response = require('AreasResponseMock').default;
95+
let Area;
96+
let component;
97+
let mockAdapter = new MockAdapter(axios);
98+
jsonStubHeaders = '{ "JsonStub-User-Key": "user-key", "JsonStub-Project-Key": "project-key" }';
99+
100+
mockAdapter.onGet(HOST + '/api/v1/visit/group/area').reply(200, response);
101+
102+
Area = require('components/Area/Area').default;
103+
104+
component = shallow(
105+
<Area />
106+
);
107+
108+
setTimeout(() => {
109+
expect(
110+
shallow(
111+
component.state().areas[0]
112+
).find('.title').at(0).text()
113+
).toEqual('Center');
114+
expect(
115+
shallow(
116+
component.state().areas[1]
117+
).find('.title').at(0).text()
118+
).toEqual('South');
119+
done();
120+
}, 0);
121+
});
122+
92123
});

tests/List.Area.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,35 @@ describe('Test Area', () => {
101101
}, 0);
102102
});
103103

104+
it('Should show headers to stub json', (done) => {
105+
let response = {
106+
areas: [
107+
{_id: 'South', parent: 'Center', ancestors: 'Center'},
108+
{_id: 'North', parent: 'Center', ancestors: 'Center'},
109+
]
110+
};
111+
let Area;
112+
let component;
113+
let mockAdapter = new MockAdapter(axios);
114+
jsonStubHeaders = '{ "JsonStub-User-Key": "user-key", "JsonStub-Project-Key": "project-key" }';
115+
116+
mockAdapter.onGet(HOST + '/api/v1/area').reply(200, response);
117+
118+
Area = require('components/Area/List/Area').default;
119+
120+
component = shallow(
121+
<Area />
122+
);
123+
124+
setTimeout(() => {
125+
try {
126+
component.update();
127+
expect(component.find('tbody td').at(0).text()).toEqual('South');
128+
done();
129+
} catch (e) {
130+
console.log(e);
131+
}
132+
}, 0);
133+
});
104134
});
105135

tests/List.Client.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,34 @@ describe('Test Client', () => {
8989
}, 0);
9090
});
9191

92+
it('Should show headers to stub json', (done) => {
93+
94+
let response = {
95+
clients: [
96+
{name: 'Jon Snow', address: '7 Street', city: 'Winterfell'},
97+
{name: 'Cotter Pyke', address: '0 Street', city: 'Castle Black'},
98+
]
99+
};
100+
let Client;
101+
let component;
102+
let mockAdapter = new MockAdapter(axios);
103+
jsonStubHeaders = '{ "JsonStub-User-Key": "user-key", "JsonStub-Project-Key": "project-key" }';
104+
105+
mockAdapter.onGet(HOST + '/api/v1/client').reply(200, response);
106+
107+
Client = require('components/Client/List/Client').default;
108+
109+
component = shallow(
110+
<Client />
111+
);
112+
113+
setTimeout(() => {
114+
component.update();
115+
expect(component.find('tbody td').at(0).text()).toEqual('Jon Snow');
116+
expect(component.find('tbody td').at(1).text()).toEqual('7 Street - Winterfell');
117+
done();
118+
}, 0);
119+
});
120+
92121
});
93122

tests/Login.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,36 @@ describe('Test Login', () => {
161161
}, 0);
162162
});
163163

164+
it('Should show headers to stub json', (done) => {
165+
166+
let Login = require('components/Login/Login').default;
167+
let response = { error:"User Not Found" };
168+
let mockAdapter = new MockAdapter(axios);
169+
jsonStubHeaders = '{ "JsonStub-User-Key": "user-key", "JsonStub-Project-Key": "project-key" }';
170+
171+
mockAdapter.onPost(HOST + '/authenticate').reply(401, response);
172+
173+
let component = mount(
174+
<Login />,
175+
{ context }
176+
);
177+
178+
let inputLogin = component.find('form div p input[type="text"]');
179+
let inputPassword = component.find('form div p input[type="password"]');
180+
181+
inputLogin.node.value = 'Astolfo';
182+
inputLogin.simulate('change', inputLogin);
183+
184+
inputPassword.node.value = 'abcd';
185+
inputPassword.simulate('change', inputPassword);
186+
187+
component.find('form').simulate('submit', { target: component.find('form').get(0) });
188+
189+
setTimeout(() => {
190+
expect(component.state().error).toEqual('User Not Found');
191+
done();
192+
}, 0);
193+
});
194+
164195
});
165196

0 commit comments

Comments
 (0)