Skip to content

Commit

Permalink
Increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ipasic-softserve committed Nov 11, 2024
1 parent 33e46d0 commit 3da892b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/containers/course-section/resource-item/ResourceItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ const ResourceItem: FC<ResourceItemProps> = ({
}

return (
<Box onClick={onResourceItemClick} sx={styles.container(isView)}>
<Box
data-testid='resourceItem'
onClick={onResourceItemClick}
sx={styles.container(isView)}
>
<Box
sx={{
...styles.titleWithDescriptionWrapper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { renderWithProviders } from '~tests/test-utils'
import { fireEvent, screen, waitFor } from '@testing-library/react'

import {
ResourceAvailabilityStatusEnum,
ResourcesTypesEnum as ResourceType
} from '~/types'
import {
mockedLessonDataOriginal,
mockedQuizDataDuplicate,
Expand All @@ -9,7 +13,7 @@ import {
mockAvailabilityForQuizDataDuplicate,
mockAvailabilityOpen
} from '~tests/unit/containers/course-section/resource-item/ResourceItem.spec.constants'

import { ResourceService } from '~/services/resource-service'
import ResourceItem from '~/containers/course-section/resource-item/ResourceItem'

const mockDeleteResource = vi.fn()
Expand Down Expand Up @@ -42,6 +46,19 @@ vi.mock('@mui/x-date-pickers/DatePicker', () => ({
)
}))

const navigateMock = vi.fn()

vi.mock('react-router-dom', async () => ({
...(await vi.importActual('react-router-dom')),
useNavigate: () => navigateMock
}))

vi.mock('~/services/resource-service', () => ({
ResourceService: {
downloadAttachment: vi.fn()
}
}))

describe('ResourceItem tests', () => {
beforeEach(() => {
renderWithProviders(
Expand Down Expand Up @@ -226,3 +243,91 @@ describe('ResourceItem tests when resourceType attachment', () => {
expect(attachmentItem).toBeInTheDocument()
})
})

describe('ResourceItem component', () => {
const mockNavigate = vi.fn()
const mockResource = {
_id: '123',
resourceType: ResourceType.Lesson
}

it('renders the button', () => {
renderWithProviders(<ResourceItem resource={mockResource} />)
expect(screen.getByTestId('resourceItem')).toBeInTheDocument()
})

it('does not navigate or download if isView is false', () => {
renderWithProviders(<ResourceItem resource={mockResource} isView={false} />)

fireEvent.click(screen.getByTestId('resourceItem'))
expect(mockNavigate).not.toHaveBeenCalled()
expect(ResourceService.downloadAttachment).not.toHaveBeenCalled()
})

it('does not navigate or download if resource is not open', () => {
renderWithProviders(
<ResourceItem
resource={mockResource}
isView={true}
availability={{ status: ResourceAvailabilityStatusEnum.Closed }}
/>
)

fireEvent.click(screen.getByTestId('resourceItem'))
expect(mockNavigate).not.toHaveBeenCalled()
expect(ResourceService.downloadAttachment).not.toHaveBeenCalled()
})

it('navigates to lesson-details if resource type is Lesson and isView is true', () => {
renderWithProviders(
<ResourceItem
resource={mockResource}
resourceType={ResourceType.Lesson}
isView={true}
availability={{ status: ResourceAvailabilityStatusEnum.Open }}
/>
)

fireEvent.click(screen.getByTestId('resourceItem'))
waitFor(()=> {
expect(mockNavigate).toHaveBeenCalledWith('lesson-details/123')
})
})

it('navigates to quiz if resource type is Quiz and isView is true', () => {
renderWithProviders(
<ResourceItem
resource={{ ...mockResource, resourceType: ResourceType.Quiz }}
resourceType={ResourceType.Quiz}
isView={true}
availability={{ status: ResourceAvailabilityStatusEnum.Open }}
/>
)

fireEvent.click(screen.getByTestId('resourceItem'))
waitFor(()=> {
expect(mockNavigate).toHaveBeenCalledWith('quiz/123')
})
})

it('calls downloadAttachment if resource type is Attachment', () => {
renderWithProviders(
<ResourceItem
resource={{
...mockResource,
resourceType: ResourceType.Attachment,
fileName: 'example.pdf'
}}
resourceType={ResourceType.Attachment}
isView={true}
availability={{ status: ResourceAvailabilityStatusEnum.Open }}
/>
)

fireEvent.click(screen.getByTestId('resourceItem'))
expect(ResourceService.downloadAttachment).toHaveBeenCalledWith(
'123',
'example.pdf'
)
})
})

0 comments on commit 3da892b

Please sign in to comment.