Skip to content

Commit

Permalink
Fix active students block with no active cooperations (#2578)
Browse files Browse the repository at this point in the history
* Fix active students block with no active cooperations

* Increase coverage

* Fix icon
  • Loading branch information
ipasic-softserve authored Oct 9, 2024
1 parent b6f9a28 commit 14e0e26
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/components/active-students/ActiveStudentsBlock.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ export const styles = {
mt: '34px',
p: '20px'
},
noStudentsWrapper: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
gap: { md: '70px', xs: '30px' },
flexWrap: 'wrap',
mt: '34px',
p: '20px'
},
showMoreWrapper: {
width: { xs: '180px', md: 'auto' },
cursor: 'pointer',
Expand Down
38 changes: 32 additions & 6 deletions src/components/active-students/ActiveStudentsBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Loader from '../loader/Loader'
import { Cooperation, ItemsWithCount } from '~/types'
import ActiveStudent from './ActiveStudent'
import AppIconButton from '../app-icon-button/AppIconButton'
import { MoreHoriz } from '@mui/icons-material'
import { Add, MoreHoriz } from '@mui/icons-material'
import { styles } from './ActiveStudentsBlock.styles'
import { useTranslation } from 'react-i18next'
import { useNavigate } from 'react-router-dom'
Expand All @@ -29,7 +29,37 @@ const ActiveStudentsBlock = () => {
})

if (loading) return <Loader pageLoad size={50} />
if (error || !response.items.length) return null
if (error) return null

const onShowMoreClick = () => {
navigate('/my-cooperations')
}

const onAddStudentClick = () => {
navigate('/categories/subjects/find-offers')
}

if (!response.items.length)
return (
<>
<Typography sx={styles.title}>{t('activeStudents.title')}</Typography>
<Box sx={styles.noStudentsWrapper}>
<Typography sx={styles.title}>
{t('activeStudents.noStudentsYet')}
</Typography>
<Box
data-testid='addStudent'
onClick={onAddStudentClick}
sx={styles.showMoreWrapper}
>
<AppIconButton size='medium' sx={styles.showMoreButton}>
<Add />
</AppIconButton>
<Typography>{t('activeStudents.addStudent')}</Typography>
</Box>
</Box>
</>
)

const activeStudents = response.items.map((cooperation) => (
<ActiveStudent
Expand All @@ -42,10 +72,6 @@ const ActiveStudentsBlock = () => {
/>
))

const onShowMoreClick = () => {
navigate('/my-cooperations')
}

return (
<>
<Typography sx={styles.title}>{t('activeStudents.title')}</Typography>
Expand Down
4 changes: 3 additions & 1 deletion src/constants/translations/en/active-students.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"title": "Your Students",
"showMore": "show more"
"showMore": "show more",
"addStudent": "add student",
"noStudentsYet": "You don't have any students yet"
}
4 changes: 3 additions & 1 deletion src/constants/translations/uk/active-students.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"title": "Ваші студенти",
"showMore": "показати більше"
"showMore": "показати більше",
"addStudent": "додати студента",
"noStudentsYet": "У вас ще немає студентів"
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ const noCooperationsMock = {
error: null,
fetchData: vi.fn()
}

const errorCooperationsMock = {
loading: false,
response: {
items: [],
count: 0
},
error: {
code: 'not found',
message: 'cooperation not found',
status: '404'
},
fetchData: vi.fn()
}

describe('ActiveStudentsBlock', () => {
useAxios.mockImplementation(() => mockedData)

Expand Down Expand Up @@ -115,9 +130,26 @@ describe('ActiveStudentsBlock', () => {
expect(screen.getByTestId('loader')).toBeInTheDocument()
})

it('should not render when no active cooperations available', () => {
it('should render add student button when no active cooperations available', () => {
useAxios.mockImplementation(() => noCooperationsMock)
renderWithProviders(<ActiveStudentsBlock />)
const addStudent = screen.getByTestId('addStudent')
expect(addStudent).toBeInTheDocument()
})

it('should navigate to /categories/subjects/find-offers on add student button click', () => {
useAxios.mockImplementation(() => noCooperationsMock)
renderWithProviders(<ActiveStudentsBlock />)

const showMoreButton = screen.getByTestId('addStudent')
fireEvent.click(showMoreButton)

waitFor(() => expect(navigateMock).toHaveBeenCalled())
})

it('should not render on error', () => {
useAxios.mockImplementation(() => errorCooperationsMock)
renderWithProviders(<ActiveStudentsBlock />)

expect(screen.queryByText('activeStudents.title')).not.toBeInTheDocument()
})
Expand Down

0 comments on commit 14e0e26

Please sign in to comment.