Skip to content

Commit d21c57f

Browse files
mattwr18joaopapereira
authored andcommitted
Display login/signup navbar links only when a user is not signed in
1 parent e062928 commit d21c57f

File tree

3 files changed

+72
-19
lines changed

3 files changed

+72
-19
lines changed

src/components/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class App extends Component {
2424
render () {
2525
return (
2626
<Fragment>
27-
<Navbar />
27+
<Navbar cookies={this.props.cookies} />
2828
<Switch>
2929
<Route path='/' exact component={Homepage} />
3030
<Route exact path='/about' component={About} />

src/components/navbar/Navbar.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react'
1+
import React, { Component, Fragment } from 'react'
22
import { Menu, Container, Image } from 'semantic-ui-react'
33
import { Link } from 'react-router-dom'
44
import { withRouter } from 'react-router'
@@ -16,7 +16,7 @@ export class Navbar extends Component {
1616

1717
render () {
1818
const activeItem = this.currentPath()
19-
19+
const { cookies } = this.props
2020
return (
2121
<Menu stackable borderless inverted as='div' className='navbar'>
2222
<Container>
@@ -65,19 +65,23 @@ export class Navbar extends Component {
6565
Getting Started
6666
</Link>
6767
</Menu.Item>
68-
<Menu.Item
69-
name='login'
70-
active={activeItem === 'login'}
71-
>
72-
<Link to='/login'>Login</Link>
73-
</Menu.Item>
68+
{!cookies.get('_WebsiteOne_session')
69+
? <Fragment>
70+
<Menu.Item
71+
name='login'
72+
active={activeItem === 'login'}
73+
>
74+
<Link to='/login'>Login</Link>
75+
</Menu.Item>
7476

75-
<Menu.Item
76-
name='signup'
77-
active={activeItem === 'signup'}
78-
>
79-
<Link to='/signup'>Sign up</Link>
80-
</Menu.Item>
77+
<Menu.Item
78+
name='signup'
79+
active={activeItem === 'signup'}
80+
>
81+
<Link to='/signup'>Sign up</Link>
82+
</Menu.Item>
83+
</Fragment>
84+
: null}
8185
</Menu.Menu>
8286
</Container>
8387
</Menu>

src/tests/components/Navbar.test.js

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,67 @@ import { BrowserRouter as Router } from 'react-router-dom'
44
import { Navbar } from '../../components/navbar/Navbar'
55

66
describe('Navbar', () => {
7-
const props = { location: { pathname: '/users' } }
8-
const homepage = (props) => mount(<Router><Navbar {...props} /></Router>)
7+
let wrapper
8+
const props = {
9+
location: { pathname: '/users' },
10+
cookies: { get: jest.fn() }
11+
}
12+
13+
beforeEach(() => {
14+
wrapper = mount(
15+
<Router>
16+
<Navbar {...props} />
17+
</Router>
18+
)
19+
})
920

1021
describe('renders the Navbar component', () => {
1122
it('renders without errors', () => {
12-
const wrapper = homepage(props)
1323
expect(wrapper.find('Navbar').length).toEqual(1)
1424
})
1525

1626
it('renders 1 active Link element', () => {
17-
const wrapper = homepage(props)
1827
expect(wrapper.find('.active').find('Link').length).toEqual(1)
1928
})
2029
})
30+
31+
describe('renders links based on if a user is signed in or not', () => {
32+
it('renders a login link if a user in not signed in', () => {
33+
const wrapper = mount(
34+
<Router>
35+
<Navbar {...props} />
36+
</Router>
37+
)
38+
const loginLink = wrapper.find('a').filterWhere(item => {
39+
return item.prop('href') === '/login'
40+
})
41+
expect(loginLink.text()).toEqual('Login')
42+
})
43+
44+
it('renders a sign up link if a user in not signed in', () => {
45+
const wrapper = mount(
46+
<Router>
47+
<Navbar {...props} />
48+
</Router>
49+
)
50+
const loginLink = wrapper.find('a').filterWhere(item => {
51+
return item.prop('href') === '/signup'
52+
})
53+
expect(loginLink.text()).toEqual('Sign up')
54+
})
55+
56+
it('renders 9 menu items if a user is not signed in', () => {
57+
expect(wrapper.find('MenuItem').length).toEqual(9)
58+
})
59+
60+
it('renders 7 menu items if a user is signed in', () => {
61+
props.cookies.get.mockReturnValue(true)
62+
const wrapper = mount(
63+
<Router>
64+
<Navbar {...props} />
65+
</Router>
66+
)
67+
expect(wrapper.find('MenuItem').length).toEqual(7)
68+
})
69+
})
2170
})

0 commit comments

Comments
 (0)