Skip to content

Commit

Permalink
added few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mav-Ivan committed Oct 10, 2024
1 parent be7e7b9 commit 66bfc0f
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/containers/course-section/CourseSectionContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const CourseSectionContainer: FC<SectionProps> = ({
component: (
<ChangeResourceConfirmModal
onConfirm={handleConfirm}
resourceId={resource.isDuplicate ? resource._id : resource._id}
resourceId={resource.isDuplicate ? '' : resource._id}
title={(resource as Attachment).fileName}
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,74 @@ export const mockedSectionData = {
]
}

export const mockedDuplicatedSectionData = {
id: 1,
title: 'Title',
description: 'Description',
resources: [
{
availability: {
status: 'open',
date: null
},
resource: {
_id: '64cd12f1fad091e0sfe12134',
id: 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
title: 'Lesson1',
author: 'some author',
content: 'Content',
description: 'Description',
attachments: [],
category: null,
isDuplicate: true,
resourceType: ResourceType.Lesson
},
resourceType: ResourceType.Lesson
},
{
availability: {
status: 'open',
date: null
},
resource: {
_id: '64fb2c33eba89699411d22bb',
id: '9b2e3d7e-1c4b-4f3b-8f8e-2d3b2c3d4e5f',
title: 'Quiz',
description: '',
items: [],
author: '648afee884936e09a37deaaa',
category: { id: '64fb2c33eba89699411d22bb', name: 'Music' },
isDuplicate: true,
createdAt: '2023-09-08T14:14:11.373Z',
updatedAt: '2023-09-08T14:14:11.373Z',
resourceType: ResourceType.Quiz
},
resourceType: ResourceType.Quiz
},
{
availability: {
status: 'open',
date: null
},
resource: {
_id: '64cd12f1fad091e0ee719830',
id: 'd2c3b4e5-6f7a-8b9c-0d1e-2f3b4c5d6e7f',
author: '6494128829631adbaf5cf615',
fileName: 'spanish.pdf',
link: 'link',
category: { id: '64fb2c33eba89699411d22bb', name: 'History' },
description: 'Mock description for attachments',
size: 100,
isDuplicate: true,
createdAt: '2023-07-25T13:12:12.998Z',
updatedAt: '2023-07-25T13:12:12.998Z',
resourceType: ResourceType.Attachment
},
resourceType: ResourceType.Attachment
}
]
}

export const mockedUpdatedResources = [
{ _id: 'resource1', resourceType: ResourceType.Lesson },
{ _id: 'resource2', resourceType: ResourceType.Quiz }
Expand Down
100 changes: 100 additions & 0 deletions tests/unit/containers/course-section/CourseSectionContainer.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { authRoutes } from '~/router/constants/authRoutes'
import { resourceNavigationMap } from '~/containers/course-section/CourseSectionContainer.constants'

import CourseSectionContainer from '~/containers/course-section/CourseSectionContainer'
import { mockedDuplicatedSectionData } from './CourseSectionContainer.spec.constants'

const mockedHandleSectionInputChange = vi.fn()
const mockedResourceEventHandler = vi.fn()
Expand Down Expand Up @@ -426,3 +427,102 @@ describe('Testing CourseSectionContainer Event Handlers', () => {
})
})
})

describe('should remove duplicates from list', () => {
const mockSectionId = mockedSectionData.id

beforeEach(() => {
renderWithProviders(
<CourseSectionContainer
handleSectionInputChange={mockedHandleSectionInputChange}
isCooperation
resourceEventHandler={mockedResourceEventHandler}
sectionData={mockedDuplicatedSectionData}
sectionEventHandler={mockedSectionEventHandler}
/>
)
})

afterEach(() => {
cleanup()
vi.resetAllMocks()
})

it('should handle resource remove event on Quiz', async () => {
fireEvent.change(screen.getByTestId('mock-ResourcesList'), {
target: {
value: JSON.stringify({
event: 'deleteResource',
payload: {
type: CourseResourceEventType.ResourceRemoved,
sectionId: mockSectionId,
resourceId: mockedSectionData.resources[0].id
}
})
}
})

await waitFor(() => {
expect(mockedResourceEventHandler).toHaveBeenCalledTimes(1)
expect(mockedResourceEventHandler).toHaveBeenCalledWith(
expect.objectContaining({
type: CourseResourceEventType.ResourceRemoved,
sectionId: mockSectionId,
resourceId: mockedSectionData.resources[0].id
})
)
})
})

it('should handle resource remove event on Quiz', async () => {
fireEvent.change(screen.getByTestId('mock-ResourcesList'), {
target: {
value: JSON.stringify({
event: 'deleteResource',
payload: {
type: CourseResourceEventType.ResourceRemoved,
sectionId: mockSectionId,
resourceId: mockedSectionData.resources[1].id
}
})
}
})

await waitFor(() => {
expect(mockedResourceEventHandler).toHaveBeenCalledTimes(1)
expect(mockedResourceEventHandler).toHaveBeenCalledWith(
expect.objectContaining({
type: CourseResourceEventType.ResourceRemoved,
sectionId: mockSectionId,
resourceId: mockedSectionData.resources[1].id
})
)
})
})

it('should handle resource remove event on Attachement', async () => {
fireEvent.change(screen.getByTestId('mock-ResourcesList'), {
target: {
value: JSON.stringify({
event: 'deleteResource',
payload: {
type: CourseResourceEventType.ResourceRemoved,
sectionId: mockSectionId,
resourceId: mockedSectionData.resources[2].id
}
})
}
})

await waitFor(() => {
expect(mockedResourceEventHandler).toHaveBeenCalledTimes(1)
expect(mockedResourceEventHandler).toHaveBeenCalledWith(
expect.objectContaining({
type: CourseResourceEventType.ResourceRemoved,
sectionId: mockSectionId,
resourceId: mockedSectionData.resources[2].id
})
)
})
})
})

0 comments on commit 66bfc0f

Please sign in to comment.