Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat(navbar): display currently logged in user name
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTan331 authored Jul 18, 2020
1 parent 73a7b7c commit 5a4bfa5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
26 changes: 22 additions & 4 deletions src/__tests__/shared/components/navbar/Navbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import thunk from 'redux-thunk'

import Navbar from '../../../../shared/components/navbar/Navbar'
import Permissions from '../../../../shared/model/Permissions'
import User from '../../../../shared/model/User'
import { RootState } from '../../../../shared/store'

const mockStore = createMockStore<RootState, any>([thunk])

describe('Navbar', () => {
const history = createMemoryHistory()

const setup = (permissions: Permissions[]) => {
const setup = (permissions: Permissions[], user?: User) => {
const store = mockStore({
title: '',
user: { permissions },
user: { permissions, user },
} as any)

const wrapper = mount(
Expand All @@ -34,6 +35,11 @@ describe('Navbar', () => {
return wrapper
}

const userName = {
givenName: 'givenName',
familyName: 'familyName',
} as User

const allPermissions = [
Permissions.ReadPatients,
Permissions.WritePatients,
Expand Down Expand Up @@ -150,13 +156,24 @@ describe('Navbar', () => {
})

describe('account', () => {
it('should render an account link list', () => {
it("should render a link with the user's identification", () => {
const expectedUserName = `user.login.currentlySignedInAs ${userName.givenName} ${userName.familyName}`

const wrapper = setup(allPermissions, userName)
const hospitalRunNavbar = wrapper.find(HospitalRunNavbar)
const accountLinkList = hospitalRunNavbar.find('.nav-account')
const { children } = accountLinkList.first().props() as any

expect(children[0].props.children).toEqual([undefined, expectedUserName])
})

it('should render a setting link list', () => {
const wrapper = setup(allPermissions)
const hospitalRunNavbar = wrapper.find(HospitalRunNavbar)
const accountLinkList = hospitalRunNavbar.find('.nav-account')
const { children } = accountLinkList.first().props() as any

expect(children[0].props.children).toEqual([undefined, 'settings.label'])
expect(children[1].props.children).toEqual([undefined, 'settings.label'])
})

it('should navigate to /settings when the list option is selected', () => {
Expand All @@ -167,6 +184,7 @@ describe('Navbar', () => {

act(() => {
children[0].props.onClick()
children[1].props.onClick()
})

expect(history.location.pathname).toEqual('/settings')
Expand Down
11 changes: 10 additions & 1 deletion src/shared/components/navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Navbar = () => {
const dispatch = useDispatch()
const history = useHistory()
const { t } = useTranslator()
const { permissions } = useSelector((state: RootState) => state.user)
const { permissions, user } = useSelector((state: RootState) => state.user)

const navigateTo = (location: string) => {
history.push(location)
Expand Down Expand Up @@ -89,6 +89,15 @@ const Navbar = () => {
type: 'link-list-icon',
alignRight: true,
children: [
{
type: 'link',
label: `${t('user.login.currentlySignedInAs')} ${user?.givenName} ${
user?.familyName
}`,
onClick: () => {
navigateTo('/settings')
},
},
{
type: 'link',
label: t('settings.label'),
Expand Down
1 change: 1 addition & 0 deletions src/shared/locales/enUs/translations/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default {
required: 'Password is required.',
},
},
currentlySignedInAs: 'Currently signed in as',
},
},
}

1 comment on commit 5a4bfa5

@vercel
Copy link

@vercel vercel bot commented on 5a4bfa5 Jul 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.