Skip to content

Commit

Permalink
Merge pull request #51 from Kalkuli/develop
Browse files Browse the repository at this point in the history
Release Final Sprint
  • Loading branch information
youssef-md authored Nov 27, 2018
2 parents 9114acf + 5a6d40d commit 30994db
Show file tree
Hide file tree
Showing 143 changed files with 3,483 additions and 12,987 deletions.
11,895 changes: 0 additions & 11,895 deletions package-lock.json

This file was deleted.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"apexcharts": "2.2.2",
"autoprefixer": "7.1.6",
"axios": "^0.18.0",
"babel-core": "6.26.0",
Expand Down Expand Up @@ -39,10 +40,12 @@
"raf": "3.4.0",
"react": "^16.6.0",
"react-addons-shallow-compare": "^15.6.2",
"react-apexcharts": "1.0.10",
"react-dates": "^18.1.1",
"react-dev-utils": "^5.0.2",
"react-dom": "^16.6.0",
"react-dropzone": "^5.1.1",
"react-particles-js": "^2.4.2",
"react-redux": "^5.1.0",
"react-router-dom": "^4.3.1",
"react-scrollchor": "^6.0.0",
Expand Down
44 changes: 25 additions & 19 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,53 @@ import Reports from './components/Reports/Reports'
import ReceiptView from './components/Receipt/ReceiptView/ReceiptView'
import HomePage from './components/HomePage/HomePage'
import { connect } from 'react-redux'
import { BrowserRouter,Route, Switch, Redirect } from 'react-router-dom'
import { BrowserRouter,Route, Switch } from 'react-router-dom'
import * as actionTypes from './store/actions/actions'

class App extends Component {
export class App extends Component {

componentWillMount() {
let token = localStorage.getItem('auth_token')
this.props.onAddAuthToken(token)
}

render() {
let routes = null
if(this.props.auth_token) {
routes = (
<Switch>
<Route path='/' exact component={HomePage}/>
<Route path='/dashboard' component={Dashboard} />
<Route path='/list-all-receipts'component={ReceiptList} />
<Route path='/reports' component={Reports} />
<Route path='/receipt' component={ReceiptView} />
<Route render={this.notFoundRoute} />
</Switch>
)
}
return (
<BrowserRouter>
<Switch>
<Route path='/' exact component={HomePage}/>
<PrivateRoute token={this.props.auth_token} path='/list-all-receipts'component={ReceiptList} />
<PrivateRoute token={this.props.auth_token} path='/dashboard' component={Dashboard} />
<PrivateRoute token={this.props.auth_token} path='/reports' component={Reports} />
<PrivateRoute token={this.props.auth_token} path='/receipt' component={ReceiptView} />
<Route render={() => <h1>Not found</h1>} />
<Switch>
<Route path='/' exact component={HomePage}/>
{routes}
<Route render={this.notFoundRoute} />
</Switch>
</BrowserRouter>
)
}
}

const PrivateRoute = ({ component: Component, token: token, ...rest}) => (
<Route {...rest} render={(props) => (
token ? <Component {...props}/> : <Redirect to='/'/>)} />
)

const mapStateToProps = state => {
notFoundRoute = () => {
return <h1>Not found</h1>
}
}
export const mapStateToProps = state => {
return {
auth_token: state.auth_token
}
}

const mapDispatchToProps = dispatch => {
export const mapDispatchToProps = dispatch => {
return {
onAddAuthToken: (token) => dispatch({ type: actionTypes.ADD_AUTH_TOKEN, auth_token: token})
}
}

export default connect(mapStateToProps, mapDispatchToProps)(App);
50 changes: 50 additions & 0 deletions src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import './services/__mocks__/matchMedia'
import { App, mapStateToProps, mapDispatchToProps } from './App'
import { Route } from 'react-router-dom'

describe('Testing <App/>', () => {

const spyOnAddAuthToken = jest.fn()

let wrapper = null
const props = {
onAddAuthToken: spyOnAddAuthToken,
auth_token: 'token'
}

beforeEach(() => {
wrapper = shallow(<App {...props}/>)
})

it('should dispatch onAddAuthToken', () => {
expect(spyOnAddAuthToken).toHaveBeenCalled()
})

it('should guard restrict routes to unloogged users', () => {
wrapper.setProps({ auth_token: null })
const restrictedRoute = wrapper.find('dashboard')
expect(restrictedRoute.exists()).toBe(false)
})

it('should render restrict routes to logged users', () => {
expect(wrapper.find(Route)).toHaveLength(8)
})

it('should get a not found when accessing an unknown route', () => {
const notFoundReturn = wrapper.instance().notFoundRoute()
expect(notFoundReturn).toEqual(<h1>Not found</h1>)
})

it('should test mapStateToProps for retrieving auth_token', () => {
const initialState = {
auth_token: 'token'
}
expect(mapStateToProps(initialState).auth_token).toMatch('token')
})

it('should test mapDispatchToProps for dispatching onAddAuthToken', () => {
const dispatch = jest.fn()
mapDispatchToProps(dispatch).onAddAuthToken()
expect(dispatch.mock.calls[0][0]).toEqual({type: 'ADD_AUTH_TOKEN'})
})
})
Loading

0 comments on commit 30994db

Please sign in to comment.