Skip to content

Commit 2dcc0f7

Browse files
committed
Add client test
1 parent be0f7be commit 2dcc0f7

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

app/components/Client/Client.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
import React from 'react';
22

33
import ClientService from 'services/Client';
4+
import Error from 'components/Error/Error'
45

56
class Client extends React.Component
67
{
78
constructor(props) {
89
super(props);
910
this.state = {
10-
clients : []
11+
clients : [],
12+
error : ''
1113
};
1214
this.getClients();
1315
}
1416

1517
getClients() {
1618
ClientService.getClients().then((response) => {
1719
this.setState({clients: response.data.clients});
20+
}).catch((error) => {
21+
this.setState({error: 'Error Found: Trying get client'});
22+
if (error.response) {
23+
this.setState({error: error.response.data.error});
24+
}
1825
});
1926
}
2027

2128
render() {
29+
if (this.state.error) {
30+
return (<Error error={this.state.error} />);
31+
}
2232
const clientList = this.state.clients.map((client, key) => {
2333
return (
2434
<tr key={key} >

tests/Client.test.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
jest.enableAutomock();
2+
jest.dontMock('components/Client/Client');
3+
jest.dontMock('components/Error/Error');
4+
jest.dontMock('react');
5+
jest.dontMock('axios');
6+
jest.dontMock('enzyme');
7+
8+
9+
describe('Test Client', () => {
10+
const React = require('react');
11+
const Enzyme = require('enzyme');
12+
const shallow = Enzyme.shallow;
13+
let ClientService = require('services/Client').default;
14+
15+
it('Client should show error message', (done) => {
16+
17+
let error = {response:{data:{error:"Client Not Found"}}};
18+
let promises = [];
19+
let Client;
20+
let component;
21+
22+
promises.push(
23+
(() => {
24+
ClientService.getClients = jest.genMockFunction().mockImplementation(() => {
25+
return new Promise((resolve, reject) => {
26+
throw error;
27+
});
28+
})
29+
})()
30+
);
31+
32+
promises.push(
33+
(() => {
34+
Client = require('components/Client/Client').default;
35+
})()
36+
);
37+
38+
promises.push(
39+
(() => {
40+
component = shallow(
41+
<Client />
42+
);
43+
})()
44+
);
45+
46+
Promise.all(promises).then(() => {
47+
component.update();
48+
expect(component.render().text()).toEqual('Client Not Found');
49+
done();
50+
}).catch((error) => {
51+
console.log(error);
52+
});
53+
});
54+
55+
it('Client should show default error message', (done) => {
56+
57+
let error = {error:"Client Not Found"};
58+
let promises = [];
59+
let Client;
60+
let component;
61+
62+
promises.push(
63+
(() => {
64+
ClientService.getClients = jest.genMockFunction().mockImplementation(() => {
65+
return new Promise((resolve, reject) => {
66+
throw error;
67+
});
68+
})
69+
})()
70+
);
71+
72+
promises.push(
73+
(() => {
74+
Client = require('components/Client/Client').default;
75+
})()
76+
);
77+
78+
promises.push(
79+
(() => {
80+
component = shallow(
81+
<Client />
82+
);
83+
})()
84+
);
85+
86+
Promise.all(promises).then(() => {
87+
component.update();
88+
expect(component.render().text()).toEqual('Error Found: Trying get client');
89+
done();
90+
}).catch((error) => {
91+
console.log(error);
92+
});
93+
});
94+
95+
it('Client should show mocked data', (done) => {
96+
97+
let response = {
98+
data: {
99+
clients: [
100+
{name: 'Jon Snow', address: '7 Street', city: 'Winterfell'}
101+
]
102+
}
103+
};
104+
let promises = [];
105+
let Client;
106+
let component;
107+
108+
promises.push(
109+
(() => {
110+
ClientService.getClients = jest.genMockFunction().mockImplementation(() => {
111+
return new Promise((resolve, reject) => {
112+
resolve(response);
113+
});
114+
})
115+
})()
116+
);
117+
118+
promises.push(
119+
(() => {
120+
Client = require('components/Client/Client').default;
121+
})()
122+
);
123+
124+
promises.push(
125+
(() => {
126+
component = shallow(
127+
<Client />
128+
);
129+
})()
130+
);
131+
132+
Promise.all(promises).then(() => {
133+
component.update();
134+
expect(component.find('td').at(0).text()).toEqual('Jon Snow');
135+
expect(component.find('td').at(1).text()).toEqual('7 Street');
136+
expect(component.find('td').at(2).text()).toEqual('Winterfell');
137+
done();
138+
}).catch((error) => {
139+
console.log(error);
140+
});
141+
});
142+
143+
});
144+

0 commit comments

Comments
 (0)