Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inventory #4

Closed
wants to merge 10 commits into from
2 changes: 2 additions & 0 deletions src/HospitalRun.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Route, Switch } from 'react-router-dom'
import Dashboard from './dashboard/Dashboard'
import Imagings from './imagings/Imagings'
import Incidents from './incidents/Incidents'
import Inventory from './inventory/Inventory'
import Labs from './labs/Labs'
import Medications from './medications/Medications'
import Breadcrumbs from './page-header/breadcrumbs/Breadcrumbs'
Expand Down Expand Up @@ -55,6 +56,7 @@ const HospitalRun = () => {
<Route path="/incidents" component={Incidents} />
<Route path="/settings" component={Settings} />
<Route path="/imaging" component={Imagings} />
<Route path="/inventory" component={Inventory} />
</Switch>
</div>
<Toaster autoClose={5000} hideProgressBar draggable />
Expand Down
161 changes: 140 additions & 21 deletions src/__tests__/shared/components/Sidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ describe('Sidebar', () => {
Permissions.AddVisit,
Permissions.RequestImaging,
Permissions.ViewImagings,
Permissions.AddItem,
Permissions.ViewItem,
Permissions.ViewInventory,
]
const store = mockStore({
components: { sidebarCollapsed: false },
Expand Down Expand Up @@ -461,27 +464,6 @@ describe('Sidebar', () => {
expect(incidentsIndex).not.toBe(-1)
})

it('should be the last one in the sidebar', () => {
const wrapper = setup('/incidents')

const listItems = wrapper.find(ListItem)
const reportsLabel = listItems.length - 2

expect(listItems.at(reportsLabel).text().trim()).toBe('incidents.reports.label')
expect(
listItems
.at(reportsLabel - 1)
.text()
.trim(),
).toBe('incidents.reports.new')
expect(
listItems
.at(reportsLabel - 2)
.text()
.trim(),
).toBe('incidents.label')
})

it('should render the new incident report link', () => {
const wrapper = setup('/incidents')

Expand Down Expand Up @@ -849,4 +831,141 @@ describe('Sidebar', () => {
expect(history.location.pathname).toEqual('/medications')
})
})

describe('inventory links', () => {
it('should be the last one in the sidebar', () => {
const wrapper = setup('/inventory')

const listItems = wrapper.find(ListItem)
const inventoryLabel = listItems.length - 1

expect(listItems.at(inventoryLabel).text().trim()).toBe('inventory.items.label')
expect(
listItems
.at(inventoryLabel - 1)
.text()
.trim(),
).toBe('inventory.items.new')
expect(
listItems
.at(inventoryLabel - 2)
.text()
.trim(),
).toBe('inventory.label')
})

it('should render the main inventory link', () => {
const wrapper = setup('/inventory')

const listItems = wrapper.find(ListItem)
const inventoryIndex = getIndex(listItems, 'inventory.label')

expect(inventoryIndex).not.toBe(-1)
})

it('should render the add inventory item link', () => {
const wrapper = setup('/inventory')

const listItems = wrapper.find(ListItem)
const inventoryIndex = getIndex(listItems, 'inventory.items.new')

expect(inventoryIndex).not.toBe(-1)
})

it('should not render the add inventory item link when user does not have add item privileges', () => {
const wrapper = setupNoPermissions('/inventory')

const listItems = wrapper.find(ListItem)
const labsIndex = getIndex(listItems, 'inventory.items.new')

expect(labsIndex).toBe(-1)
})

it('should render the inventory list link', () => {
const wrapper = setup('/inventory')

const listItems = wrapper.find(ListItem)
const inventoryIndex = getIndex(listItems, 'inventory.items.label')

expect(inventoryIndex).not.toBe(-1)
})

it('should not render the inventory list link when user does not have view inventory privileges', () => {
const wrapper = setupNoPermissions('/inventory')

const listItems = wrapper.find(ListItem)
const inventoryIndex = getIndex(listItems, 'inventory.items.label')

expect(inventoryIndex).toBe(-1)
})

it('main inventory link should be active when the current path is /inventory', () => {
const wrapper = setup('/inventory')

const listItems = wrapper.find(ListItem)
const inventoryIndex = getIndex(listItems, 'inventory.label')

expect(listItems.at(inventoryIndex).prop('active')).toBeTruthy()
})

it('should navigate to /inventory when the main lab link is clicked', () => {
const wrapper = setup('/')

const listItems = wrapper.find(ListItem)
const inventoryIndex = getIndex(listItems, 'inventory.label')

act(() => {
const onClick = listItems.at(inventoryIndex).prop('onClick') as any
onClick()
})

expect(history.location.pathname).toEqual('/inventory')
})

it('add inventory item link should be active when the current path is /inventory/new', () => {
const wrapper = setup('/inventory/new')

const listItems = wrapper.find(ListItem)
const inventoryIndex = getIndex(listItems, 'inventory.items.new')

expect(listItems.at(inventoryIndex).prop('active')).toBeTruthy()
})

it('should navigate to /inventory/new when the add inventory item link is clicked', () => {
const wrapper = setup('/inventory')

const listItems = wrapper.find(ListItem)
const inventoryIndex = getIndex(listItems, 'inventory.items.new')

act(() => {
const onClick = listItems.at(inventoryIndex).prop('onClick') as any
onClick()
})

expect(history.location.pathname).toEqual('/inventory/new')
})

it('inventory list link should be active when the current path is /inventory', () => {
const wrapper = setup('/inventory')

const listItems = wrapper.find(ListItem)
const inventoryIndex = getIndex(listItems, 'inventory.items.label')

expect(listItems.at(inventoryIndex).prop('active')).toBeTruthy()
})

it('should navigate to /inventory when the inventory list link is clicked', () => {
const wrapper = setup('/inventory/new')

const listItems = wrapper.find(ListItem)
const inventoryIndex = getIndex(listItems, 'inventory.items.label')

act(() => {
const onClick = listItems.at(inventoryIndex).prop('onClick') as any
onClick()
})

expect(history.location.pathname).toEqual('/inventory')
})
})
})
56 changes: 56 additions & 0 deletions src/inventory/Inventory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react'
import { useSelector } from 'react-redux'
import { Switch } from 'react-router-dom'

import useAddBreadcrumbs from '../page-header/breadcrumbs/useAddBreadcrumbs'
import PrivateRoute from '../shared/components/PrivateRoute'
import Permissions from '../shared/model/Permissions'
import { RootState } from '../shared/store'
import AddInventoryItem from './add/AddInventoryItem'
import EditItem from './edit/EditItem'
import ViewInventory from './view/ViewInventory'
import ViewItem from './view/ViewItem'

const Inventory = () => {
const { permissions } = useSelector((state: RootState) => state.user)
const breadcrumbs = [
{
i18nKey: 'inventory.label',
location: `/inventory`,
},
]
useAddBreadcrumbs(breadcrumbs, true)

return (
<Switch>
<PrivateRoute
isAuthenticated={permissions.includes(Permissions.ViewInventory)}
exact
path="/inventory"
component={ViewInventory}
/>
<PrivateRoute
isAuthenticated={permissions.includes(Permissions.AddItem)}
exact
path="/inventory/new"
component={AddInventoryItem}
/>
<PrivateRoute
isAuthenticated={
permissions.includes(Permissions.AddItem) && permissions.includes(Permissions.ViewItem)
}
exact
path="/inventory/edit/:id"
component={EditItem}
/>
<PrivateRoute
isAuthenticated={permissions.includes(Permissions.ViewItem)}
exact
path="/inventory/:id"
component={ViewItem}
/>
</Switch>
)
}

export default Inventory