Skip to content

Commit

Permalink
Merge 590bf70 into 54727ed
Browse files Browse the repository at this point in the history
  • Loading branch information
Lundii committed Sep 16, 2019
2 parents 54727ed + 590bf70 commit 5bdf250
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/actions/__tests__/__snapshots__/authActions.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,11 @@ Array [
},
]
`;

exports[`Auth action tests user should be able to logout adds new articles to the store 1`] = `
Array [
Object {
"type": "LOADING",
},
]
`;
23 changes: 22 additions & 1 deletion src/actions/__tests__/authActions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import nock from 'nock';
import {
createUser,
logIn, requestPasswordLink, setNewPassword,
loginViaSocial,
loginViaSocial, logout,
} from '@Actions/authActions';

const middlewares = [thunk];
Expand Down Expand Up @@ -272,4 +272,25 @@ describe('Auth action tests', () => {
});
});
});

describe('user should be able to logout', () => {
let store;
beforeEach(() => {
store = mockStore({ });
});

afterEach(() => {
nock.cleanAll();
});

it('adds new articles to the store', () => {
nock(url)
.post('/users/logout')
.reply(204, {});
return store.dispatch(logout())
.then(() => {
expect(store.getActions()).toMatchSnapshot();
});
});
});
});
9 changes: 9 additions & 0 deletions src/actions/authActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ export const setNewPassword = (data, history) => async (dispatch) => {
});
}
};

export const logout = () => async (dispatch) => {
dispatch({ type: LOADING });
const response = await axiosInstance.post('users/logout', {});
if (response.status === 204) {
localStorage.removeItem('haven');
}
};

export const loginViaSocial = (brand) => async dispatch => {
dispatch({ type: null });
window.location = `https://a-haven-staging.herokuapp.com/api/v1/users/${brand}`;
Expand Down
13 changes: 11 additions & 2 deletions src/components/DropDown/DropDown.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import './DropDown.style.scss';
import {
markAsRead, getNotifications, markAllRead, updateSubscription,
} from '@Actions/notifications';
import { logout } from '@Actions/authActions';
import { getProfile } from '@Actions/profileAction';
import back from '../../../public/Rectangle.png';
import checkMark from '../../../public/check-mark.png';
Expand Down Expand Up @@ -102,7 +103,9 @@ export class DropDown extends Component {
<div className="toggle-control" />
</label>
</div>
<Link to="/" className="dropLinks">Logout</Link>
<div onClick={() => { this.handleSignout(); }}>
<Link data-test="drop-down" to="/logout" className="dropLinks">Logout</Link>
</div>
</ul>
</div>
);
Expand Down Expand Up @@ -137,6 +140,11 @@ export class DropDown extends Component {
}
}

handleSignout() {
const { logout } = this.props;
logout();
}

render() {
const {
type, notifications: { notifications }, show, profile: { user },
Expand Down Expand Up @@ -197,6 +205,7 @@ DropDown.propTypes = {
notifications: PropTypes.shape({
notifications: PropTypes.array,
}).isRequired,
logout: PropTypes.func.isRequired,
};

DropDown.defaultProps = {
Expand All @@ -205,5 +214,5 @@ DropDown.defaultProps = {

export default connectComponent(withRouter(DropDown),
{
markAsRead, getNotifications, markAllRead, updateSubscription, getProfile,
markAsRead, getNotifications, markAllRead, updateSubscription, getProfile, logout,
});
28 changes: 28 additions & 0 deletions src/e2e/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
'@tag': ['Logout'],
'User should be able to logout': (browser) => {
browser
.url('http://localhost:9090')
.pause(2000)
.waitForElementVisible('[datatest="signin-button"]')
.assert.visible('[datatest="signin-button"]')
.click('[datatest="signin-button"]')
.pause(2000)
.assert.visible('[data-test="signin-form"]')
.pause(1000)
.setValue('#email', 'ngwobiachukwudi@gmail.com')
.pause(1000)
.setValue('#password', 'P@ssword123')
.pause(1000)
.click('[datatest="loginButton"]')
.pause(4000)
.assert.visible('[data-test="userImage"]')
.pause(1000)
.click('[data-test="userImage"]')
.assert.visible('[data-test="drop-down"]')
.pause(1000)
.click('[data-test="drop-down"]')
.pause(3000)
.end();
},
};
5 changes: 4 additions & 1 deletion src/views/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import {
BrowserRouter, Route, Switch, Redirect,
} from 'react-router-dom';
import Login from '@Views/LoginPage';
import Search from '@Views/SearchPage/SearchPage';
import Home from '@Views/HomePage';
Expand Down Expand Up @@ -35,6 +37,7 @@ const App = () => (
<Route exact path="/articles/:slug/edit" component={EditArticle} />
<Route exact path="/articles" component={CategoryPage} />
<Route exact path="/tags" component={ArticleTags} />
<Redirect from="/logout" to="/" />
<Route component={NotFound} />
</Switch>
</div>
Expand Down
13 changes: 12 additions & 1 deletion src/views/ProfilePage/Profile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable no-nested-ternary */
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */
/* eslint-disable react/require-default-props */
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
import React, { Component } from 'react';
Expand All @@ -16,6 +18,7 @@ import { getProfile, editProfile, getArticles } from '@Actions/profileAction';
import { followUser } from '@Actions/followActions';
import { unFollowUser } from '@Actions/unfollowActions';
import { postImage } from '@Actions/imageAction';
import { logout } from '@Actions/authActions';
import Icon from '@Components/Icon';
import RenderButton from '@Components/RenderButton';

Expand Down Expand Up @@ -53,6 +56,7 @@ export class Profile extends Component {
isFollowing: false,
};
this.dialog = React.createRef();
this.handleSignout = this.handleSignout.bind(this);
}

async componentDidMount() {
Expand Down Expand Up @@ -227,6 +231,11 @@ export class Profile extends Component {
}));
};

handleSignout() {
const { logout } = this.props;
logout();
}

render() {
const {
errors,
Expand Down Expand Up @@ -331,7 +340,7 @@ export class Profile extends Component {
<li><Link to="/">Stats</Link></li>
<li><Link to="/">Notification</Link></li>
<li><Link to="/">Help</Link></li>
<li><Link to="/">Log Out</Link></li>
<li onClick={() => { this.handleSignout(); }}><Link to="/logout">Log Out</Link></li>
</ul>
</div>
<div className="main">
Expand Down Expand Up @@ -413,6 +422,7 @@ Profile.propTypes = {
}),
}).isRequired,
history: PropTypes.shape().isRequired,
logout: PropTypes.func.isRequired,
};

export default connectComponent(
Expand All @@ -423,5 +433,6 @@ export default connectComponent(
uploadImage: postImage,
follow: (username) => followUser(username),
unfollow: (username) => unFollowUser(username),
logout,
},
);
6 changes: 4 additions & 2 deletions src/views/ProfilePage/__snapshots__/Profile.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ exports[`Profile component test have props shallow render with snapshot 1`] = `
Help
</Link>
</li>
<li>
<li
onClick={[Function]}
>
<Link
to="/"
to="/logout"
>
Log Out
</Link>
Expand Down

0 comments on commit 5bdf250

Please sign in to comment.