-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from GabrielDeveloper/develop
* Create component to list areas
- Loading branch information
Showing
6 changed files
with
186 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React from 'react'; | ||
import { Link } from 'react-router' | ||
|
||
import AreaService from 'services/Area' | ||
|
||
import Error from 'components/Error/Error' | ||
|
||
class Area extends React.Component | ||
{ | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
areas : null, | ||
error : '' | ||
}; | ||
this.getAreas(); | ||
} | ||
|
||
getAreas() { | ||
AreaService.getAll().then((response) => { | ||
this.setState({areas: response.data.areas}); | ||
}).catch((error) => { | ||
this.setState({error: 'Error Found: Trying get area'}); | ||
if (typeof error.response.data.error !== 'undefined') { | ||
this.setState({error: error.response.data.error}); | ||
} | ||
}); | ||
} | ||
|
||
render() { | ||
if (this.state.error) { | ||
return (<Error error={this.state.error} />); | ||
} | ||
if (!this.state.areas) { | ||
return <div>Loading...</div>; | ||
} | ||
const areaList = this.state.areas.map((area, key) => { | ||
let line = ((key % 2) ? 'is-success' : 'is-info'); | ||
return ( | ||
<tr key={key}> | ||
<td> | ||
{ area._id } | ||
</td> | ||
</tr> | ||
); | ||
}); | ||
|
||
return ( | ||
<section className=""> | ||
<div className="container hello"> | ||
<div className="level header"> | ||
<div className="level-left"> | ||
<h2 className="title is-2">Areas</h2> | ||
</div> | ||
<div className="level-right"> | ||
<Link to='/area' className="button is-info is-medium"> | ||
<span className="icon"> | ||
<i className="fa fa-plus"></i> | ||
</span> | ||
<span>New Area</span> | ||
</Link> | ||
</div> | ||
</div> | ||
<table className="table"> | ||
<tbody> | ||
{ areaList } | ||
</tbody> | ||
</table> | ||
</div> | ||
</section> | ||
); | ||
} | ||
} | ||
|
||
export default Area; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
jest.enableAutomock(); | ||
jest.dontMock('components/Area/List/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 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'); | ||
|
||
it('Area should show error message', (done) => { | ||
|
||
let response = {error:"Area Not Found"}; | ||
let Area; | ||
let component; | ||
let mockAdapter = new MockAdapter(axios); | ||
|
||
mockAdapter.onGet(HOST + '/api/v1/area').reply(404, response); | ||
|
||
Area = require('components/Area/List/Area').default; | ||
|
||
component = shallow( | ||
<Area /> | ||
); | ||
|
||
setTimeout(() => { | ||
try { | ||
component.update(); | ||
expect(component.render().text()).toEqual('Area Not Found'); | ||
done(); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
|
||
}, 0); | ||
}); | ||
|
||
it('Area should show default error message', (done) => { | ||
|
||
let response = {}; | ||
let Area; | ||
let component; | ||
let mockAdapter = new MockAdapter(axios); | ||
|
||
mockAdapter.onGet(HOST + '/api/v1/area').reply(503, response); | ||
|
||
Area = require('components/Area/List/Area').default; | ||
|
||
component = shallow( | ||
<Area /> | ||
); | ||
|
||
setTimeout(() => { | ||
try { | ||
component.update(); | ||
expect(component.render().text()).toEqual('Error Found: Trying get area'); | ||
done(); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}, 0); | ||
}); | ||
|
||
it('Area should show mocked data', (done) => { | ||
|
||
let response = { | ||
areas: [ | ||
{_id: 'South', parent: 'Center', ancestors: 'Center'}, | ||
{_id: 'North', parent: 'Center', ancestors: 'Center'}, | ||
] | ||
}; | ||
let Area; | ||
let component; | ||
let mockAdapter = new MockAdapter(axios); | ||
|
||
mockAdapter.onGet(HOST + '/api/v1/area').reply(200, response); | ||
|
||
Area = require('components/Area/List/Area').default; | ||
|
||
component = shallow( | ||
<Area /> | ||
); | ||
|
||
setTimeout(() => { | ||
try { | ||
component.update(); | ||
expect(component.find('tbody td').at(0).text()).toEqual('South'); | ||
done(); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}, 0); | ||
}); | ||
|
||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters