diff --git a/app/App.js b/app/App.js
index 2d2e6a1..fbf35cb 100644
--- a/app/App.js
+++ b/app/App.js
@@ -1,22 +1,32 @@
-import React from 'react';
-import ReactDOM from 'react-dom';
+import React from 'react'
+import ReactDOM from 'react-dom'
import { Router, Route, IndexRoute, hashHistory } from 'react-router'
import 'bulma/css/bulma.css'
import 'font-awesome-webpack'
-import iClientComponent from 'components/IClient/IClient';
-import HomeComponent from 'components/Home/Home';
-import ClientComponent from 'components/Client/Client';
-import AreaComponent from 'components/Area/Area';
+import iClientComponent from 'components/IClient/IClient'
+import HomeComponent from 'components/Home/Home'
+import AreaComponent from 'components/Area/Area'
+import ListClientComponent from 'components/Client/List/Client'
+import ProfileClientComponent from 'components/Client/Profile/Client'
+import SaveClientComponent from 'components/Client/Save/Client'
+import CreateVisitComponent from 'components/Visit/Create/Visit'
+import CreateAreaComponent from 'components/Area/Create/Area'
ReactDOM.render(
-
-
+
+
+
+
+
+
+
,
document.getElementById('app')
);
+
diff --git a/app/components/Area/Area.js b/app/components/Area/Area.js
index f97ac3b..ac8cbf3 100644
--- a/app/components/Area/Area.js
+++ b/app/components/Area/Area.js
@@ -27,7 +27,7 @@ class Area extends React.Component
areas: this.generate(response.data.visits)
});
}).catch((error) => {
- if (error.response) {
+ if (typeof error.response.data.error !== 'undefined') {
this.setState({error: error.response.data.error});
}
});
@@ -57,6 +57,19 @@ class Area extends React.Component
}
return (
Loading...
;
+ }
+
+ const select = Object.assign({}, this.props.select);
+
+ return (
+ Loading...
;
+ }
+ const clientList = this.state.clients.map((client, key) => {
+ let line = ((key % 2) ? 'is-success' : 'is-info');
+ return (
+ Loading...
;
+ }
+
+ return (
+ Loading...
;
+ }
+
+ return (
+
-
+
+
-
+
+
diff --git a/app/components/Menu/Menu.js b/app/components/Menu/Menu.js
index 51edf30..af21a47 100644
--- a/app/components/Menu/Menu.js
+++ b/app/components/Menu/Menu.js
@@ -13,8 +13,8 @@ class Menu extends React.Component{
this.state = {
links: [
[ '/', 'Home'],
- ['/client', 'Client'],
- ['/area', 'Area']
+ ['/clients', 'Clients'],
+ ['/areas', 'Areas']
]
};
}
diff --git a/app/components/Visit/Create/Visit.js b/app/components/Visit/Create/Visit.js
new file mode 100644
index 0000000..b0b63a5
--- /dev/null
+++ b/app/components/Visit/Create/Visit.js
@@ -0,0 +1,135 @@
+import React from 'react';
+import { Link, Router } from 'react-router'
+
+import Error from 'components/Error/Error'
+import VisitService from 'services/Visit'
+import ClientService from 'services/Client'
+
+class Visit extends React.Component
+{
+ constructor(props, context) {
+ super(props, context);
+ this.handleSubmit = this.handleSubmit.bind(this);
+ this.state = {
+ error : '',
+ client : null,
+ visit_date: new Date(),
+ visit : {}
+ };
+ }
+
+ componentWillMount() {
+ ClientService.find(this.props.params.clientId).then((response) => {
+ this.setState({client: response.data.client.shift()});
+ }).catch((error) => {
+ this.setState({error: 'Error Found: Trying get client'});
+ let validResponse = typeof error.response.data !== 'undefined';
+ if (validResponse && typeof error.response.data.error !== 'undefined') {
+ this.setState({error: error.response.data.error});
+ }
+ });
+ }
+
+ handleSubmit(e) {
+ e.preventDefault();
+
+ let visit = {
+ client : this.state.client,
+ visit_date: this.state.visit_date,
+ };
+
+ for (let i in this.refs) {
+ visit[i] = this.refs[i].value;
+ }
+
+ this.setState({visit: visit});
+
+ VisitService.save(visit).then((response) => {
+ this.context.router.push("/clients");
+ }).catch((error) => {
+
+ this.setState({error: 'Error trying create visit'});
+
+ let responseValid = typeof error.response.data !== 'undefined';
+
+ if (responseValid && typeof error.response.data.error !== 'undefined') {
+ this.setState({error: error.response.data.error});
+ }
+ });
+ }
+
+ render() {
+ if (this.state.error) {
+ return ();
+ }
+
+ if (!this.state.client) {
+ return
Loading...
;
+ }
+
+ return (
+
+ );
+ }
+}
+
+Visit.contextTypes = {
+ router: React.PropTypes.object.isRequired
+};
+
+export default Visit;
+
diff --git a/app/services/Area.js b/app/services/Area.js
new file mode 100644
index 0000000..816e580
--- /dev/null
+++ b/app/services/Area.js
@@ -0,0 +1,28 @@
+import axios from 'axios';
+
+const Area = {
+
+ getEntryPoint() {
+ return [ HOST, 'api', 'v1', 'area' ];
+ },
+
+ getConfig() {
+ return {
+ headers: {
+ Authorization : window.localStorage.getItem('token')
+ }
+ };
+ },
+
+ getAll() {
+ return axios.get(this.getEntryPoint().join('/'), this.getConfig());
+ },
+
+ save(data) {
+ return axios.post(this.getEntryPoint().join('/'), data, this.getConfig());
+ }
+};
+
+export default Area;
+
+
diff --git a/app/services/Client.js b/app/services/Client.js
index ebf31f9..a94a923 100644
--- a/app/services/Client.js
+++ b/app/services/Client.js
@@ -1,15 +1,38 @@
import axios from 'axios';
-import { HOST } from 'constants/Server';
-
const Client = {
- getClients() {
- let config = {
+
+ getEntryPoint() {
+ return [ HOST, 'api', 'v1', 'client' ];
+ },
+
+ getConfig() {
+ return {
headers: {
- Authorization : localStorage.token
+ Authorization : window.localStorage.getItem('token')
}
};
- return axios.get(`${HOST}/api/v1/client`, config);
+ },
+
+ getClients() {
+ return axios.get(this.getEntryPoint().join('/'), this.getConfig());
+ },
+
+ find(id) {
+
+ let url = this.getEntryPoint();
+ url.push(id);
+
+ return axios.get(url.join('/'), this.getConfig());
+ },
+
+ save(client, id = undefined) {
+ if (id !== undefined) {
+ let url = this.getEntryPoint();
+ url.push(id);
+ return axios.put(url.join('/'), client, this.getConfig());
+ }
+ return axios.post(this.getEntryPoint().join('/'), client, this.getConfig());
}
};
diff --git a/app/services/User.js b/app/services/User.js
index 000bd20..d05f311 100644
--- a/app/services/User.js
+++ b/app/services/User.js
@@ -1,5 +1,4 @@
import axios from 'axios';
-import { HOST } from 'constants/Server';
const User = {
login(username, password) {
diff --git a/app/services/Visit.js b/app/services/Visit.js
index 86772be..8c46aa5 100644
--- a/app/services/Visit.js
+++ b/app/services/Visit.js
@@ -1,15 +1,27 @@
import axios from 'axios';
-import { HOST } from 'constants/Server';
-
const Visit = {
- getGroupByArea() {
- let config = {
+
+ getEntryPoint() {
+ return [ HOST, 'api', 'v1', 'visit' ];
+ },
+
+ getConfig() {
+ return {
headers: {
- Authorization : localStorage.token
+ Authorization : window.localStorage.getItem('token')
}
};
- return axios.get(`${HOST}/api/v1/visit/group/area`, config);
+ },
+
+ getGroupByArea() {
+ let url = this.getEntryPoint();
+ url.push('group', 'area');
+ return axios.get(url.join('/'), this.getConfig());
+ },
+
+ save(data) {
+ return axios.post(this.getEntryPoint().join('/'), data, this.getConfig());
}
};
diff --git a/package.json b/package.json
index 68810a7..b6dd58f 100644
--- a/package.json
+++ b/package.json
@@ -6,12 +6,13 @@
"dependencies": {
"axios": "^0.14.0",
"bulma": "^0.2.1",
- "font-awesome": "^4.6.3",
+ "font-awesome": "^4.7.0",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"react-router": "^2.8.1"
},
"devDependencies": {
+ "axios-mock-adapter": "^1.7.1",
"babel-core": "^6.17.0",
"babel-jest": "^16.0.0",
"babel-loader": "^6.2.5",
@@ -45,7 +46,14 @@
"modulePaths": [
"app"
],
- "collectCoverage": true
+ "collectCoverage": true,
+ "globals" : {
+ "HOST" : "http://localhost:3000"
+ },
+ "moduleNameMapper": {
+ "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "
/tests/__mocks__/fileMock.js",
+ "\\.(css|less)$": "/tests/__mocks__/styleMock.js"
+ }
},
"author": "",
"license": "ISC"
diff --git a/tests/Area.test.js b/tests/Area.test.js
index 4c7cbb0..008a66d 100644
--- a/tests/Area.test.js
+++ b/tests/Area.test.js
@@ -3,132 +3,78 @@ jest.dontMock('components/Area/Area');
jest.dontMock('components/Error/Error');
jest.dontMock('react');
jest.dontMock('axios');
+jest.dontMock('axios-mock-adapter');
+jest.dontMock('services/Visit');
jest.dontMock('../tests/__mocks__/AreasResponseMock');
jest.dontMock('enzyme');
-
describe('Test Area', () => {
+ require('../tests/__mocks__/LocalStorageMock');
+
const React = require('react');
const Enzyme = require('enzyme');
const shallow = Enzyme.shallow;
+ let axios = require('axios');
+ let MockAdapter = require('axios-mock-adapter');
let Visit = require('services/Visit').default;
it('Area should show error message', (done) => {
- let error = {response:{data:{error:"Not Found"}}};
- let promises = [];
+ let response = {error:"Not Found"};
let Area;
let component;
+ let mockAdapter = new MockAdapter(axios);
- promises.push(
- (() => {
- Visit.getGroupByArea = jest.genMockFunction().mockImplementation(() => {
- return new Promise((resolve, reject) => {
- throw error;
- });
- })
- })()
- );
+ mockAdapter.onGet(HOST + '/api/v1/visit/group/area').reply(404, response);
- promises.push(
- (() => {
- Area = require('components/Area/Area').default;
- })()
- );
+ Area = require('components/Area/Area').default;
- promises.push(
- (() => {
- component = shallow(
-
- );
- })()
+ component = shallow(
+
);
- Promise.all(promises).then(() => {
- expect(component.state('error')).toEqual('Not Found');
-
- let errorComponent = shallow(
- component.instance().generateError(error.response.data.error)
- );
-
- expect(errorComponent.find('div').text()).toEqual('Not Found');
+ setTimeout(() => {
+ expect(component.render().text()).toEqual('Not Found');
done();
- }).catch((error) => {
- console.log(error);
- });
+ }, 0);
});
it('Area should show nothing if no data', (done) => {
- let error = {data:{error:"Not Found"}};
- let promises = [];
let Area;
let component;
+ let mockAdapter = new MockAdapter(axios);
- promises.push(
- (() => {
- Visit.getGroupByArea = jest.genMockFunction().mockImplementation(() => {
- return new Promise((resolve, reject) => {
- throw error;
- });
- })
- })()
- );
+ mockAdapter.onGet(HOST + '/api/v1/visit/group/area').reply(404, {});
- promises.push(
- (() => {
- Area = require('components/Area/Area').default;
- })()
- );
+ Area = require('components/Area/Area').default;
- promises.push(
- (() => {
- component = shallow(
-
- );
- })()
+ component = shallow(
+
);
- Promise.all(promises).then(() => {
+ setTimeout(() => {
expect(component.state('error')).toEqual('');
done();
- }).catch((error) => {
- console.log(error);
- });
+ }, 0);
});
it('Area should show mocked data', (done) => {
- let response = {data:require('AreasResponseMock').default};
- let promises = [];
+ let response = require('AreasResponseMock').default;
let Area;
let component;
+ let mockAdapter = new MockAdapter(axios);
- promises.push(
- (() => {
- Visit.getGroupByArea = jest.genMockFunction().mockImplementation(() => {
- return new Promise((resolve, reject) => {
- resolve(response);
- });
- })
- })()
- );
+ mockAdapter.onGet(HOST + '/api/v1/visit/group/area').reply(200, response);
- promises.push(
- (() => {
- Area = require('components/Area/Area').default;
- })()
- );
+ Area = require('components/Area/Area').default;
- promises.push(
- (() => {
- component = shallow(
-
- );
- })()
+ component = shallow(
+
);
- Promise.all(promises).then(() => {
+ setTimeout(() => {
expect(
shallow(
component.state().areas[0]
@@ -140,9 +86,7 @@ describe('Test Area', () => {
).find('.title').at(0).text()
).toEqual('South');
done();
- }).catch((error) => {
- console.log(error);
- });
+ }, 0);
});
});
diff --git a/tests/Client.test.js b/tests/Client.test.js
deleted file mode 100644
index c2731f3..0000000
--- a/tests/Client.test.js
+++ /dev/null
@@ -1,144 +0,0 @@
-jest.enableAutomock();
-jest.dontMock('components/Client/Client');
-jest.dontMock('components/Error/Error');
-jest.dontMock('react');
-jest.dontMock('axios');
-jest.dontMock('enzyme');
-
-
-describe('Test Client', () => {
- const React = require('react');
- const Enzyme = require('enzyme');
- const shallow = Enzyme.shallow;
- let ClientService = require('services/Client').default;
-
- it('Client should show error message', (done) => {
-
- let error = {response:{data:{error:"Client Not Found"}}};
- let promises = [];
- let Client;
- let component;
-
- promises.push(
- (() => {
- ClientService.getClients = jest.genMockFunction().mockImplementation(() => {
- return new Promise((resolve, reject) => {
- throw error;
- });
- })
- })()
- );
-
- promises.push(
- (() => {
- Client = require('components/Client/Client').default;
- })()
- );
-
- promises.push(
- (() => {
- component = shallow(
-
- );
- })()
- );
-
- Promise.all(promises).then(() => {
- component.update();
- expect(component.render().text()).toEqual('Client Not Found');
- done();
- }).catch((error) => {
- console.log(error);
- });
- });
-
- it('Client should show default error message', (done) => {
-
- let error = {error:"Client Not Found"};
- let promises = [];
- let Client;
- let component;
-
- promises.push(
- (() => {
- ClientService.getClients = jest.genMockFunction().mockImplementation(() => {
- return new Promise((resolve, reject) => {
- throw error;
- });
- })
- })()
- );
-
- promises.push(
- (() => {
- Client = require('components/Client/Client').default;
- })()
- );
-
- promises.push(
- (() => {
- component = shallow(
-
- );
- })()
- );
-
- Promise.all(promises).then(() => {
- component.update();
- expect(component.render().text()).toEqual('Error Found: Trying get client');
- done();
- }).catch((error) => {
- console.log(error);
- });
- });
-
- it('Client should show mocked data', (done) => {
-
- let response = {
- data: {
- clients: [
- {name: 'Jon Snow', address: '7 Street', city: 'Winterfell'}
- ]
- }
- };
- let promises = [];
- let Client;
- let component;
-
- promises.push(
- (() => {
- ClientService.getClients = jest.genMockFunction().mockImplementation(() => {
- return new Promise((resolve, reject) => {
- resolve(response);
- });
- })
- })()
- );
-
- promises.push(
- (() => {
- Client = require('components/Client/Client').default;
- })()
- );
-
- promises.push(
- (() => {
- component = shallow(
-
- );
- })()
- );
-
- Promise.all(promises).then(() => {
- component.update();
- expect(component.find('td').at(0).text()).toEqual('Jon Snow');
- expect(component.find('td').at(1).text()).toEqual('7 Street');
- expect(component.find('td').at(2).text()).toEqual('Winterfell');
- done();
- }).catch((error) => {
- console.log(error);
- });
- });
-
-});
-
diff --git a/tests/Create.Area.test.js b/tests/Create.Area.test.js
new file mode 100644
index 0000000..31f7924
--- /dev/null
+++ b/tests/Create.Area.test.js
@@ -0,0 +1,260 @@
+jest.enableAutomock();
+jest.dontMock('components/Area/Create/Area');
+jest.dontMock('components/Area/Select/Area');
+jest.dontMock('components/Error/Error');
+jest.dontMock('react');
+jest.dontMock('axios');
+jest.dontMock('axios-mock-adapter');
+jest.dontMock('enzyme');
+jest.dontMock('services/Area');
+
+describe('Test Create Area', () => {
+ require('../tests/__mocks__/LocalStorageMock');
+
+ const React = require('react');
+ const Enzyme = require('enzyme');
+ const shallow = Enzyme.shallow;
+ const mount = Enzyme.mount;
+ const context = {
+ router: {
+ push: (arg) => arg
+ }
+ };
+
+ let axios = require('axios');
+ let MockAdapter = require('axios-mock-adapter');
+ let response;
+
+ it('Should show default error when fails to create area', (done) => {
+
+ let areaInput;
+ let areaSelected;
+ let Area = require('components/Area/Create/Area').default;
+ let mockAdapter = new MockAdapter(axios);
+ let context = {
+ router: {
+ push: (arg) => {
+ expect(arg).toEqual('/clients');
+ }
+ }
+ };
+ let component = mount(
+ ,
+ { context }
+ );
+ let expected = {
+ _id : 'Test',
+ parent : 'South',
+ }
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ },
+ {
+ _id : "South",
+ }
+ ]
+ };
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+ .onPost(HOST + '/api/v1/area').reply(503, { error : 'Error on server'})
+
+ setTimeout(() => {
+
+ try {
+
+ let inputs = {
+ area : component.find('input'),
+ parent : component.find('select'),
+ }
+
+ areaInput = component.find('input');
+ areaInput.node.value = 'Test';
+ areaInput.simulate('change', areaInput);
+
+ areaSelected = component.find('select');
+ areaSelected.node.value = 'South';
+ areaSelected.simulate('change', areaSelected);
+
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
+ } catch (e) {
+ console.log(e);
+ }
+ }, 5);
+
+
+ setTimeout(() => {
+
+ try {
+
+ expect(component.state().area).toEqual(expected);
+ expect(component.state().error).toEqual('Error on server');
+ done();
+ } catch (e) {
+ console.log(e);
+ }
+ }, 25);
+ });
+
+ it('Should show default error when fails to create area', (done) => {
+
+ let areaInput;
+ let areaSelected;
+ let Area = require('components/Area/Create/Area').default;
+ let mockAdapter = new MockAdapter(axios);
+ let context = {
+ router: {
+ push: (arg) => {
+ expect(arg).toEqual('/clients');
+ }
+ }
+ };
+ let component = mount(
+ ,
+ { context }
+ );
+ let expected = {
+ _id : 'Test',
+ parent : 'South',
+ }
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ },
+ {
+ _id : "South",
+ }
+ ]
+ };
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+ .onPost(HOST + '/api/v1/area').reply(503)
+
+ setTimeout(() => {
+
+ try {
+
+ let inputs = {
+ area : component.find('input'),
+ parent : component.find('select'),
+ }
+
+ areaInput = component.find('input');
+ areaInput.node.value = 'Test';
+ areaInput.simulate('change', areaInput);
+
+ areaSelected = component.find('select');
+ areaSelected.node.value = 'South';
+ areaSelected.simulate('change', areaSelected);
+
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
+ } catch (e) {
+ console.log(e);
+ }
+ }, 5);
+
+
+ setTimeout(() => {
+
+ try {
+
+ expect(component.state().area).toEqual(expected);
+ expect(component.state().error).toEqual('Error trying create area');
+ done();
+ } catch (e) {
+ console.log(e);
+ }
+ }, 25);
+ });
+
+ it('Should return data on submit', (done) => {
+
+ let areaInput;
+ let areaSelected;
+ let Area = require('components/Area/Create/Area').default;
+ let mockAdapter = new MockAdapter(axios);
+ let context = {
+ router: {
+ push: (arg) => {
+ expect(arg).toEqual('/clients');
+ }
+ }
+ };
+ let component = mount(
+ ,
+ { context }
+ );
+ let expected = {
+ _id : 'Test',
+ parent : 'South',
+ }
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ },
+ {
+ _id : "South",
+ }
+ ]
+ };
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+ .onPost(HOST + '/api/v1/area').reply(201)
+
+ setTimeout(() => {
+
+ try {
+
+ let inputs = {
+ area : component.find('input'),
+ parent : component.find('select'),
+ }
+
+ areaInput = component.find('input');
+ areaInput.node.value = 'Test';
+ areaInput.simulate('change', areaInput);
+
+ areaSelected = component.find('select');
+ areaSelected.node.value = 'South';
+ areaSelected.simulate('change', areaSelected);
+
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
+
+ expect(component.state().area).toEqual(expected);
+ expect(component.state().error).toEqual('');
+ done();
+ } catch (e) {
+ console.log(e);
+ }
+ }, 25);
+ });
+
+ it('Should show default error message', (done) => {
+
+ let mockAdapter = new MockAdapter(axios);
+ let Area = require('components/Area/Create/Area').default;
+ let component = shallow(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ try {
+ expect(component.find('h1').text()).toEqual('Create Area');
+ expect(component.find('input').hasClass('input')).toBeTruthy();
+ expect(component.find('button').text()).toEqual('Save');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+});
+
diff --git a/tests/Create.Visit.test.js b/tests/Create.Visit.test.js
new file mode 100644
index 0000000..f62f8d0
--- /dev/null
+++ b/tests/Create.Visit.test.js
@@ -0,0 +1,281 @@
+jest.enableAutomock();
+jest.dontMock('components/Visit/Create/Visit');
+jest.dontMock('components/Error/Error');
+jest.dontMock('react');
+jest.dontMock('axios');
+jest.dontMock('axios-mock-adapter');
+jest.dontMock('enzyme');
+jest.dontMock('services/Visit');
+jest.dontMock('services/Client');
+
+describe('Test Create Visit', () => {
+ require('../tests/__mocks__/LocalStorageMock');
+
+ const React = require('react');
+ const Enzyme = require('enzyme');
+ const shallow = Enzyme.shallow;
+ const mount = Enzyme.mount;
+ const context = {
+ router: {
+ push: (arg) => arg
+ }
+ };
+
+ let axios = require('axios');
+ let MockAdapter = require('axios-mock-adapter');
+ let response;
+
+ beforeEach(() => {
+ response = {
+ client : [
+ {
+ _id : '1',
+ updatedAt : '2016-12-01T12:26:00.903Z',
+ createdAt : '2016-12-01T12:26:00.903Z',
+ name : 'Test',
+ address : 'fdajla',
+ city : 'fdajlkj',
+ area : {
+ _id : 'Center',
+ parent : '',
+ __v : 0,
+ ancestors :[]
+ },
+ frequency : 11,
+ ability : 201,
+ __v :0
+ }
+ ]
+ };
+ });
+
+ it('Visit should show error when fails to create visit', (done) => {
+
+ let expectedClient = response.client[0];
+ let Visit = require('components/Visit/Create/Visit').default;
+ let mockAdapter = new MockAdapter(axios);
+ let context = {
+ router: {
+ push: (arg) => {
+ expect(arg).toEqual('/clients');
+ }
+ }
+ };
+ let component = mount(
+ ,
+ { context }
+ );
+ let expected = {
+ client : expectedClient,
+ visit_date : component.state().visit_date,
+ value_received : '10',
+ sales_quantity : '10',
+ }
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/client/1').reply(200, response)
+ .onPost(HOST + '/api/v1/visit').reply(503, { error : 'Error on server'});
+
+ setTimeout(() => {
+
+ let inputs = {
+ value_received : component.find('input').at(0),
+ quantity : component.find('input').at(1),
+ }
+
+ for (let input in inputs) {
+ inputs[input].node.value = 10;
+ inputs[input].simulate('change', inputs[input]);
+ }
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
+
+ }, 5);
+
+ setTimeout(() => {
+
+ try {
+ expect(component.render().text()).toEqual('Error on server');
+ done();
+ } catch (e) {
+ console.log(e);
+ }
+ }, 10);
+ });
+
+ it('Should show error when fails to create visit', (done) => {
+
+ let expectedClient = response.client[0];
+ let Visit = require('components/Visit/Create/Visit').default;
+ let mockAdapter = new MockAdapter(axios);
+ let context = {
+ router: {
+ push: (arg) => {
+ expect(arg).toEqual('/clients');
+ }
+ }
+ };
+ let component = mount(
+ ,
+ { context }
+ );
+ let expected = {
+ client : expectedClient,
+ visit_date : component.state().visit_date,
+ value_received : '10',
+ sales_quantity : '10',
+ }
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/client/1').reply(200, response)
+ .onPost(HOST + '/api/v1/visit').reply(503);
+
+ setTimeout(() => {
+
+ let inputs = {
+ value_received : component.find('input').at(0),
+ quantity : component.find('input').at(1),
+ }
+
+ for (let input in inputs) {
+ inputs[input].node.value = 10;
+ inputs[input].simulate('change', inputs[input]);
+ }
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
+
+ }, 5);
+
+ setTimeout(() => {
+
+ try {
+ expect(component.render().text()).toEqual('Error trying create visit');
+ done();
+ } catch (e) {
+ console.log(e);
+ }
+ }, 10);
+ });
+
+ it('Should return data on submit', (done) => {
+
+ let expectedClient = response.client[0];
+ let Visit = require('components/Visit/Create/Visit').default;
+ let mockAdapter = new MockAdapter(axios);
+ let context = {
+ router: {
+ push: (arg) => {
+ expect(arg).toEqual('/clients');
+ }
+ }
+ };
+ let component = mount(
+ ,
+ { context }
+ );
+ let expected = {
+ client : expectedClient,
+ visit_date : component.state().visit_date,
+ value_received : '10',
+ sales_quantity : '10',
+ }
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/client/1').reply(200, response)
+ .onPost(HOST + '/api/v1/visit').reply(201);
+
+ setTimeout(() => {
+
+ try {
+
+ let inputs = {
+ value_received : component.find('input').at(0),
+ quantity : component.find('input').at(1),
+ }
+
+ for (let input in inputs) {
+ inputs[input].node.value = 10;
+ inputs[input].simulate('change', inputs[input]);
+ }
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
+
+ expect(component.state().client).toEqual(expectedClient);
+ expect(component.state().visit).toEqual(expected);
+ expect(component.state().error).toEqual('');
+ done();
+ } catch (e) {
+ console.log(e);
+ }
+ }, 25);
+ });
+
+ it('Should show mocked data', (done) => {
+
+ let mockAdapter = new MockAdapter(axios);
+ let Visit = require('components/Visit/Create/Visit').default;
+ let component = shallow(
+ ,
+ { context }
+ );
+
+ mockAdapter.onGet(HOST + '/api/v1/client/1').reply(200, response);
+
+ setTimeout(() => {
+
+ try {
+ expect(component.find('.content p').at(0).text()).toEqual('Name: Test');
+ expect(component.find('.content p').at(1).text()).toEqual('Address: fdajla');
+ expect(component.find('.content p').at(2).text()).toEqual('City: fdajlkj');
+ expect(component.find('.button').at(0).text()).toEqual('Save');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+ it('Should show error message', (done) => {
+
+ let response = { error:'Client 1 Not Found' };
+ let Visit = require('components/Visit/Create/Visit').default;
+ let mockAdapter = new MockAdapter(axios);
+ let component = shallow(
+ ,
+ { context }
+ );
+
+ mockAdapter.onGet(HOST + '/api/v1/client/1').reply(404, response);
+
+ setTimeout(() => {
+
+ try {
+ component.update();
+ expect(component.render().text()).toEqual('Client 1 Not Found');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+ it('Should show default error message', (done) => {
+
+ let mockAdapter = new MockAdapter(axios);
+ let Visit = require('components/Visit/Create/Visit').default;
+ let component = shallow(
+ ,
+ { context }
+ );
+
+ mockAdapter.onGet(HOST + '/api/v1/client/1').reply(503);
+
+ setTimeout(() => {
+
+ try {
+ expect(component.render().text()).toEqual('Error Found: Trying get client');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+});
+
diff --git a/tests/List.Client.test.js b/tests/List.Client.test.js
new file mode 100644
index 0000000..7e1a576
--- /dev/null
+++ b/tests/List.Client.test.js
@@ -0,0 +1,93 @@
+jest.enableAutomock();
+jest.dontMock('components/Client/List/Client');
+jest.dontMock('components/Error/Error');
+jest.dontMock('react');
+jest.dontMock('axios');
+jest.dontMock('axios-mock-adapter');
+jest.dontMock('enzyme');
+jest.dontMock('services/Client');
+
+describe('Test Client', () => {
+ require('../tests/__mocks__/LocalStorageMock');
+
+ const React = require('react');
+ const Enzyme = require('enzyme');
+ const shallow = Enzyme.shallow;
+
+ let axios = require('axios');
+ let MockAdapter = require('axios-mock-adapter');
+
+ it('Client should show error message', (done) => {
+
+ let response = {error:"Client Not Found"};
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+
+ mockAdapter.onGet(HOST + '/api/v1/client').reply(404, response);
+
+ Client = require('components/Client/List/Client').default;
+
+ component = shallow(
+
+ );
+
+ setTimeout(() => {
+ component.update();
+ expect(component.render().text()).toEqual('Client Not Found');
+ done();
+ }, 0);
+ });
+
+ it('Client should show default error message', (done) => {
+
+ let response = {};
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+
+ mockAdapter.onGet(HOST + '/api/v1/client').reply(503, response);
+
+ Client = require('components/Client/List/Client').default;
+
+ component = shallow(
+
+ );
+
+ setTimeout(() => {
+ component.update();
+ expect(component.render().text()).toEqual('Error Found: Trying get client');
+ done();
+ }, 0);
+ });
+
+ it('Client should show mocked data', (done) => {
+
+ let response = {
+ clients: [
+ {name: 'Jon Snow', address: '7 Street', city: 'Winterfell'},
+ {name: 'Cotter Pyke', address: '0 Street', city: 'Castle Black'},
+ ]
+ };
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+
+ mockAdapter.onGet(HOST + '/api/v1/client').reply(200, response);
+
+ Client = require('components/Client/List/Client').default;
+
+ component = shallow(
+
+ );
+
+ setTimeout(() => {
+ component.update();
+ expect(component.find('tbody td').at(0).text()).toEqual('Jon Snow');
+ expect(component.find('tbody td').at(1).text()).toEqual('7 Street - Winterfell');
+ done();
+ }, 0);
+ });
+
+});
+
diff --git a/tests/Login.test.js b/tests/Login.test.js
index 5c8a705..fdd4286 100644
--- a/tests/Login.test.js
+++ b/tests/Login.test.js
@@ -4,6 +4,8 @@ jest.dontMock('components/Error/Error');
jest.dontMock('react');
jest.dontMock('axios');
jest.dontMock('enzyme');
+jest.dontMock('axios-mock-adapter');
+jest.dontMock('services/User');
describe('Test Login', () => {
require('../tests/__mocks__/LocalStorageMock');
@@ -18,6 +20,9 @@ describe('Test Login', () => {
push: (arg) => arg
}
};
+ let axios = require('axios');
+ let MockAdapter = require('axios-mock-adapter');
+
let User = require('services/User').default;
it('Login should show form', (done) => {
@@ -27,59 +32,19 @@ describe('Test Login', () => {
{ context }
);
- expect(component.find('form').length).toEqual(1);
-
- done();
- });
-
- it('Login should get login values', (done) => {
-
- let Login = require('components/Login/Login').default;
- let loginInformed;
- let passwordInformed;
-
- User.login = jest.genMockFunction().mockImplementation((login, password) => {
- return new Promise((resolve, reject) => {
- loginInformed = login;
- passwordInformed = password;
- });
- })
-
- let component = mount(
- ,
- { context }
- );
-
- expect(loginInformed).toEqual(undefined);
- expect(passwordInformed).toEqual(undefined);
-
- let inputLogin = component.find('form div p input[type="text"]');
- let inputPassword = component.find('form div p input[type="password"]');
-
- inputLogin.node.value = 'Astolfo';
- inputLogin.simulate('change', inputLogin);
-
- inputPassword.node.value = 'abcd';
- inputPassword.simulate('change', inputPassword);
+ setTimeout(() => {
+ expect(component.find('form').length).toEqual(1);
- component.find('form').simulate('submit', { target: component.find('form').get(0) });
-
- expect(loginInformed).toEqual('Astolfo');
- expect(passwordInformed).toEqual('abcd');
-
- done();
+ done();
+ }, 0);
});
it('Login should not set localStorage if success is different from 200', (done) => {
let Login = require('components/Login/Login').default;
- let promises = [];
+ let mockAdapter = new MockAdapter(axios);
- User.login = jest.genMockFunction().mockImplementation((login, password) => {
- return new Promise((resolve, reject) => {
- resolve({data: {success: 201, token: 'token_test'}});
- });
- })
+ mockAdapter.onPost(HOST + '/authenticate').reply(201, {success: 201, token: 'token_test'});
let component = mount(
,
@@ -97,31 +62,20 @@ describe('Test Login', () => {
expect(window.localStorage.getItem('token')).toEqual(null);
- promises.push(
- (() => {
- component.find('form').simulate('submit', { target: component.find('form').get(0) });
- })()
- );
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
- Promise.all(promises).then(() => {
+ setTimeout(() => {
expect(window.localStorage.getItem('token')).toEqual(null);
done();
- }).catch((error) => {
- console.log(error);
- });
+ }, 0);
});
-
it('Login should set localStorage', (done) => {
let Login = require('components/Login/Login').default;
- let promises = [];
+ let mockAdapter = new MockAdapter(axios);
- User.login = jest.genMockFunction().mockImplementation((login, password) => {
- return new Promise((resolve, reject) => {
- resolve({data: {success: 200, token: 'token_test'}});
- });
- })
+ mockAdapter.onPost(HOST + '/authenticate').reply(200, {success: 200, token: 'token_test'});
let component = mount(
,
@@ -139,31 +93,21 @@ describe('Test Login', () => {
expect(window.localStorage.getItem('token')).toEqual(null);
- promises.push(
- (() => {
- component.find('form').simulate('submit', { target: component.find('form').get(0) });
- })()
- );
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
- Promise.all(promises).then(() => {
+ setTimeout(() => {
expect(window.localStorage.getItem('token')).toEqual('token_test');
done();
- }).catch((error) => {
- console.log(error);
- });
+ }, 0);
});
it('Login should show error default on state', (done) => {
let Login = require('components/Login/Login').default;
- let error = {response:{error:"Another error"}};
- let promises = [];
+ let response = {};
+ let mockAdapter = new MockAdapter(axios);
- User.login = jest.genMockFunction().mockImplementation((login, password) => {
- return new Promise((resolve, reject) => {
- throw error;
- });
- })
+ mockAdapter.onPost(HOST + '/authenticate').reply(503, response);
let component = mount(
,
@@ -179,32 +123,21 @@ describe('Test Login', () => {
inputPassword.node.value = 'abcd';
inputPassword.simulate('change', inputPassword);
- promises.push(
- (() => {
- component.find('form').simulate('submit', { target: component.find('form').get(0) });
- })()
- );
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
- Promise.all(promises).then(() => {
+ setTimeout(() => {
expect(component.state().error).toEqual('Authentication failed');
done();
- }).catch((error) => {
- console.log(error);
- });
+ }, 0);
});
-
it('Login should set error received on state', (done) => {
let Login = require('components/Login/Login').default;
- let error = {response:{data:{error:"User Not Found"}}};
- let promises = [];
+ let response = { error:"User Not Found" };
+ let mockAdapter = new MockAdapter(axios);
- User.login = jest.genMockFunction().mockImplementation((login, password) => {
- return new Promise((resolve, reject) => {
- throw error;
- });
- })
+ mockAdapter.onPost(HOST + '/authenticate').reply(401, response);
let component = mount(
,
@@ -220,18 +153,12 @@ describe('Test Login', () => {
inputPassword.node.value = 'abcd';
inputPassword.simulate('change', inputPassword);
- promises.push(
- (() => {
- component.find('form').simulate('submit', { target: component.find('form').get(0) });
- })()
- );
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
- Promise.all(promises).then(() => {
+ setTimeout(() => {
expect(component.state().error).toEqual('User Not Found');
done();
- }).catch((error) => {
- console.log(error);
- });
+ }, 0);
});
});
diff --git a/tests/Menu.test.js b/tests/Menu.test.js
index 68fe50b..ceb4b24 100644
--- a/tests/Menu.test.js
+++ b/tests/Menu.test.js
@@ -20,8 +20,8 @@ describe('Test Menu', () => {
let expectedLinks = [
['/', 'Home'],
- ['/client', 'Client'],
- ['/area', 'Area'],
+ ['/clients', 'Clients'],
+ ['/areas', 'Areas'],
];
expect(component.state().links).toEqual(expectedLinks);
diff --git a/tests/Profile.Client.test.js b/tests/Profile.Client.test.js
new file mode 100644
index 0000000..559fd35
--- /dev/null
+++ b/tests/Profile.Client.test.js
@@ -0,0 +1,122 @@
+jest.enableAutomock();
+jest.dontMock('components/Client/Profile/Client');
+jest.dontMock('components/Error/Error');
+jest.dontMock('react');
+jest.dontMock('react-router');
+jest.dontMock('axios');
+jest.dontMock('axios-mock-adapter');
+jest.dontMock('enzyme');
+jest.dontMock('services/Client');
+
+describe('Test Client', () => {
+ require('../tests/__mocks__/LocalStorageMock');
+
+ const React = require('react');
+ const Enzyme = require('enzyme');
+ const shallow = Enzyme.shallow;
+ const mount = Enzyme.mount;
+
+ let axios = require('axios');
+ let MockAdapter = require('axios-mock-adapter');
+
+ it('Client should show mocked data', (done) => {
+
+ let id = '123abc';
+ let response = {
+ client: [
+ {
+ id: id,
+ name: 'Jon Snow',
+ address: '7 Street',
+ city: 'Winterfell',
+ ability: 200,
+ frequency: 10,
+ area: {
+ _id: 'Center',
+ parents: 'Center'
+ }
+ },
+ ]
+ };
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+
+ mockAdapter.onGet(HOST + '/api/v1/client/' + id).reply(200, response);
+
+ Client = require('components/Client/Profile/Client').default;
+
+ component = mount(
+
+ );
+
+ setTimeout(() => {
+
+ try {
+ expect(component.find('.name p').at(0).text()).toEqual('Jon Snow');
+ expect(component.find('.name p').at(1).text()).toEqual('Address: 7 Street - Winterfell');
+ expect(component.find('.name p').at(2).text()).toEqual('Area: Center');
+ expect(component.find('.followers p').at(0).text()).toEqual('10');
+ expect(component.find('.followers p').at(1).text()).toEqual('Frequency');
+ expect(component.find('.followers p').at(2).text()).toEqual('200');
+ expect(component.find('.followers p').at(3).text()).toEqual('Ability');
+ expect(component.find('.nav-menu span a').at(0).text()).toEqual('Visited');
+ expect(component.find('.nav-menu span a').at(1).text()).toEqual('Update');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+ it('Client should show error message', (done) => {
+
+ let id = '123abc';
+ let response = { error:"Client Not Found" };
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+
+ mockAdapter.onGet(HOST + '/api/v1/client/' + id).reply(404, response);
+
+ Client = require('components/Client/Profile/Client').default;
+
+ component = shallow(
+
+ );
+
+ setTimeout(() => {
+
+ component.update();
+ expect(component.render().text()).toEqual('Client Not Found');
+ done();
+ }, 0);
+ });
+
+ it('Client should show default error message', (done) => {
+
+ let id = '123abc';
+ let response = {};
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+
+ mockAdapter.onGet(HOST + '/api/v1/client/' + id).reply(503, response);
+
+ Client = require('components/Client/Profile/Client').default;
+
+ component = shallow(
+
+ );
+
+ setTimeout(() => {
+
+ component.update();
+ expect(component.render().text()).toEqual('Error Found: Trying get client');
+ done();
+ }, 0);
+ });
+
+});
+
+
diff --git a/tests/Save.Client.test.js b/tests/Save.Client.test.js
new file mode 100644
index 0000000..e6dbe52
--- /dev/null
+++ b/tests/Save.Client.test.js
@@ -0,0 +1,590 @@
+jest.enableAutomock();
+jest.dontMock('components/Client/Save/Client');
+jest.dontMock('components/Error/Error');
+jest.dontMock('react');
+jest.dontMock('axios');
+jest.dontMock('axios-mock-adapter');
+jest.dontMock('enzyme');
+jest.dontMock('services/Area');
+jest.dontMock('services/Client');
+
+describe('Test Save Client', () => {
+ require('../tests/__mocks__/LocalStorageMock');
+
+ const React = require('react');
+ const Enzyme = require('enzyme');
+ const shallow = Enzyme.shallow;
+ const mount = Enzyme.mount;
+ const context = {
+ router: {
+ push: (arg) => arg
+ }
+ };
+
+ let axios = require('axios');
+ let MockAdapter = require('axios-mock-adapter');
+
+ it('Client should show error when fails to save client', (done) => {
+
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+ let context = {
+ router: {
+ push: (arg) => arg
+ }
+ };
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ },
+ {
+ _id : "South",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ }
+ ]
+ };
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+ .onPost(HOST + '/api/v1/client').reply(503);
+
+ Client = require('components/Client/Save/Client').default;
+
+ component = mount(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ let inputs = {
+ name : component.find('input').at(0),
+ phone : component.find('input').at(1),
+ address : component.find('input').at(2),
+ city : component.find('input').at(3),
+ frequency : component.find('input').at(4),
+ ability : component.find('input').at(5),
+ }
+
+ for (let input in inputs) {
+ inputs[input].node.value = input;
+ inputs[input].simulate('change', input);
+ }
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
+ }, 5);
+
+ setTimeout(() => {
+
+ try {
+ expect(component.render().text()).toEqual('Error trying save client');
+ done();
+ } catch (e) {
+ console.log(e);
+ }
+ }, 10);
+ });
+
+
+ it('Client should show error when fails to save client', (done) => {
+
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+ let context = {
+ router: {
+ push: (arg) => arg
+ }
+ };
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ },
+ {
+ _id : "South",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ }
+ ]
+ };
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+ .onPost(HOST + '/api/v1/client').reply(503, {error : 'Error save client'} );
+
+ Client = require('components/Client/Save/Client').default;
+
+ component = mount(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ let inputs = {
+ name : component.find('input').at(0),
+ phone : component.find('input').at(1),
+ address : component.find('input').at(2),
+ city : component.find('input').at(3),
+ frequency : component.find('input').at(4),
+ ability : component.find('input').at(5),
+ }
+
+ for (let input in inputs) {
+ inputs[input].node.value = input;
+ inputs[input].simulate('change', input);
+ }
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
+ }, 5);
+
+ setTimeout(() => {
+
+ try {
+ expect(component.render().text()).toEqual('Error save client');
+ done();
+ } catch (e) {
+ console.log(e);
+ }
+ }, 10);
+ });
+
+ it('Client should return data on submit', (done) => {
+
+ let Client;
+ let component;
+ let selectArea;
+ let mockAdapter = new MockAdapter(axios);
+ let context = {
+ router: {
+ push: (arg) => {
+ expect(arg).toEqual('/clients');
+ }
+ }
+ };
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ },
+ {
+ _id : "South",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ }
+ ]
+ };
+ let expected = {
+ name : 'name',
+ phone : 'phone',
+ address : 'address',
+ city : 'city',
+ frequency : 'frequency',
+ ability : 'ability',
+ area : {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ },
+ };
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+ .onPost(HOST + '/api/v1/client').reply(201);
+
+ Client = require('components/Client/Save/Client').default;
+
+ component = mount(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ try {
+
+ let inputs = {
+ name : component.find('input').at(0),
+ phone : component.find('input').at(1),
+ address : component.find('input').at(2),
+ city : component.find('input').at(3),
+ frequency : component.find('input').at(4),
+ ability : component.find('input').at(5),
+ }
+
+ selectArea = component.find('select')
+ selectArea.node.value = 'Center'
+ selectArea.simulate('change', selectArea);
+
+ for (let input in inputs) {
+ inputs[input].node.value = input;
+ inputs[input].simulate('change', input);
+ }
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
+ expect(component.state().client).toEqual(expected);
+ expect(component.state().error).toEqual('');
+ done();
+ } catch (e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+ it('Client should show mocked areas data', (done) => {
+
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ },
+ {
+ _id : "South",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ }
+ ]
+ };
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+
+ mockAdapter.onGet(HOST + '/api/v1/area').reply(200, response);
+
+ Client = require('components/Client/Save/Client').default;
+
+ component = shallow(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ expect(component.find('select option').at(0).text()).toEqual('Center');
+ expect(component.find('select option').at(1).text()).toEqual('South');
+ done();
+ }, 0);
+ });
+
+ it('Client should show error message', (done) => {
+
+ let response = { error:"Areas Not Found" };
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+
+ mockAdapter.onGet(HOST + '/api/v1/area').reply(404, response);
+
+ Client = require('components/Client/Save/Client').default;
+
+ component = shallow(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ component.update();
+ expect(component.render().text()).toEqual('Areas Not Found');
+ done();
+ }, 0);
+ });
+
+ it('Client should show default error message', (done) => {
+
+ let response = {};
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+
+ mockAdapter.onGet(HOST + '/api/v1/area').reply(503, response);
+
+ Client = require('components/Client/Save/Client').default;
+
+ component = shallow(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ try {
+ component.update();
+ expect(component.render().text()).toEqual('Error Found: Trying get areas');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+ it('Client should show client loaded', (done) => {
+
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ },
+ {
+ _id : "South",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ }
+ ]
+ };
+ let clientResponse = {
+ client : [
+ {
+ _id : "58407735f7b50400132d64e7",
+ updatedAt : "2016-12-01T19:17:09.262Z",
+ createdAt : "2016-12-01T19:17:09.262Z",
+ name : "lfdjal",
+ address : "fdjaljfdal",
+ city : "jfladkjl",
+ area : {
+ _id : "Test",
+ parent : "South",
+ __v : 0,
+ ancestors : []
+ },
+ frequency : 11,
+ ability : 300,
+ __v : 0
+ }
+ ]
+ };
+ let expectedClientState = Object.assign({}, ...clientResponse.client);
+ expectedClientState.updatedAt = '';
+ expectedClientState.createdAt = '';
+ expectedClientState.area = 'Test';
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+ .onGet(HOST + '/api/v1/client/1').reply(200, clientResponse)
+
+ Client = require('components/Client/Save/Client').default;
+
+ component = shallow(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ try {
+ component.update();
+ expect(component.state().client).toEqual(expectedClientState);
+ expect(component.find('input').at(0).props().value).toEqual('lfdjal');
+ expect(component.find('input').at(1).props().value).toEqual(undefined);
+ expect(component.find('input').at(3).props().value).toEqual('jfladkjl');
+ expect(component.find('input').at(4).props().value).toEqual(11);
+ expect(component.find('input').at(5).props().value).toEqual(300);
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+ it('Client should show error message', (done) => {
+
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ },
+ {
+ _id : "South",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ }
+ ]
+ };
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+ .onGet(HOST + '/api/v1/client/1').reply(503, { error : 'Error on server'})
+
+ Client = require('components/Client/Save/Client').default;
+
+ component = shallow(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ try {
+ component.update();
+ expect(component.render().text()).toEqual('Error on server');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+ it('Client should show default error message when error to find client', (done) => {
+
+ let Client;
+ let component;
+ let mockAdapter = new MockAdapter(axios);
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ },
+ {
+ _id : "South",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ }
+ ]
+ };
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+ .onGet(HOST + '/api/v1/client/1').reply(503);
+
+ Client = require('components/Client/Save/Client').default;
+
+ component = shallow(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ try {
+ component.update();
+ expect(component.render().text()).toEqual('Error Found: Trying get client 1');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+ it('Client should update client', (done) => {
+
+ let Client;
+ let component;
+ let selectArea;
+ let mockAdapter = new MockAdapter(axios);
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ },
+ {
+ _id : "South",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ }
+ ]
+ };
+ let clientResponse = {
+ client : [
+ {
+ _id : "58407735f7b50400132d64e7",
+ updatedAt : "2016-12-01T19:17:09.262Z",
+ createdAt : "2016-12-01T19:17:09.262Z",
+ name : "lfdjal",
+ address : "fdjaljfdal",
+ city : "jfladkjl",
+ area : {
+ _id : "Test",
+ parent : "South",
+ __v : 0,
+ ancestors : []
+ },
+ frequency : 11,
+ ability : 300,
+ __v : 0
+ }
+ ]
+ };
+ let expectedClientState = Object.assign({}, ...clientResponse.client);
+ expectedClientState.updatedAt = '';
+ expectedClientState.createdAt = '';
+ expectedClientState.area = {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ };
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+ .onGet(HOST + '/api/v1/client/1').reply(200, clientResponse)
+ .onPost(HOST + '/api/v1/client').reply(403)
+ .onPut(HOST + '/api/v1/client').reply(204)
+
+ Client = require('components/Client/Save/Client').default;
+
+ component = mount(
+ ,
+ { context }
+ );
+
+ setTimeout(() => {
+
+ try {
+
+ selectArea = component.find('select')
+ selectArea.node.value = 'Center'
+ selectArea.simulate('change', selectArea);
+
+ component.find('form').simulate('submit', { target: component.find('form').get(0) });
+
+ component.update();
+ expect(component.state().error).toEqual('');
+ expect(component.state().client).toEqual(expectedClientState);
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+});
+
diff --git a/tests/Select.Area.test.js b/tests/Select.Area.test.js
new file mode 100644
index 0000000..4c2cd2d
--- /dev/null
+++ b/tests/Select.Area.test.js
@@ -0,0 +1,143 @@
+jest.enableAutomock();
+jest.dontMock('components/Area/Select/Area');
+jest.dontMock('components/Error/Error');
+jest.dontMock('react');
+jest.dontMock('axios');
+jest.dontMock('axios-mock-adapter');
+jest.dontMock('enzyme');
+jest.dontMock('services/Area');
+
+describe('Test Select Area', () => {
+ require('../tests/__mocks__/LocalStorageMock');
+
+ const React = require('react');
+ const Enzyme = require('enzyme');
+ const shallow = Enzyme.shallow;
+ const mount = Enzyme.mount;
+ const context = {
+ router: {
+ push: (arg) => arg
+ }
+ };
+
+ let axios = require('axios');
+ let MockAdapter = require('axios-mock-adapter');
+
+ it('Should show props on select element', (done) => {
+
+ let Area = require('components/Area/Select/Area').default;
+ let mockAdapter = new MockAdapter(axios);
+ let component = mount(
+
+ );
+
+ mockAdapter.onGet(HOST + '/api/v1/area').reply(200, { areas : [] })
+
+ setTimeout(() => {
+
+ try {
+ expect(
+ component.find('select').html()
+ ).toEqual(
+ ''
+ );
+ expect(component.props()).toEqual({
+ className: 'Unknown',
+ select: {
+ name: 'TestOne',
+ id: 'TestTwo',
+ className: 'class'
+ }
+ });
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+
+ it('Should show mocked data', (done) => {
+
+ let Area = require('components/Area/Select/Area').default;
+ let mockAdapter = new MockAdapter(axios);
+ let component = mount(
+
+ );
+ let response = {
+ areas : [
+ {
+ _id : "Center",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ },
+ {
+ _id : "South",
+ parent : "",
+ __v : 0,
+ ancestors : []
+ }
+ ]
+ };
+
+ mockAdapter
+ .onGet(HOST + '/api/v1/area').reply(200, response)
+
+ setTimeout(() => {
+
+ try {
+ expect(component.find('option').at(0).text()).toEqual('Center');
+ expect(component.find('option').at(1).text()).toEqual('South');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+ it('Should show error message', (done) => {
+
+ let response = { error:'Areas Not Found' };
+ let Area = require('components/Area/Select/Area').default;
+ let mockAdapter = new MockAdapter(axios);
+ let component = shallow(
+
+ );
+
+ mockAdapter.onGet(HOST + '/api/v1/area').reply(404, response);
+
+ setTimeout(() => {
+
+ try {
+ component.update();
+ expect(component.render().text()).toEqual('Areas Not Found');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+
+ it('Should show default error message', (done) => {
+
+ let mockAdapter = new MockAdapter(axios);
+ let Area = require('components/Area/Select/Area').default;
+ let component = shallow(
+
+ );
+
+ mockAdapter.onGet(HOST + '/api/v1/area').reply(503);
+
+ setTimeout(() => {
+
+ try {
+ expect(component.render().text()).toEqual('Error Found: Trying get areas');
+ done();
+ } catch(e) {
+ console.log(e);
+ }
+ }, 0);
+ });
+});
+
diff --git a/tests/__mocks__/fileMock.js b/tests/__mocks__/fileMock.js
new file mode 100644
index 0000000..86059f3
--- /dev/null
+++ b/tests/__mocks__/fileMock.js
@@ -0,0 +1 @@
+module.exports = 'test-file-stub';
diff --git a/tests/__mocks__/styleMock.js b/tests/__mocks__/styleMock.js
new file mode 100644
index 0000000..f053ebf
--- /dev/null
+++ b/tests/__mocks__/styleMock.js
@@ -0,0 +1 @@
+module.exports = {};
diff --git a/webpack.config.js b/webpack.config.js
index 022ddc6..8bfac55 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -30,6 +30,9 @@ module.exports = {
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false }
+ }),
+ new webpack.DefinePlugin({
+ HOST: JSON.stringify(process.env.HOST || 'http://localhost:3000')
})
],
resolve: {