Skip to content

Commit

Permalink
Bound confirm on resource delete (#2568)
Browse files Browse the repository at this point in the history
* bound confirm on resource delete

* increased tests coverage

* fexed tests
  • Loading branch information
YaroslavChuiko authored Oct 3, 2024
1 parent c291100 commit a067c74
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PaginationProps } from '@mui/material'
import { useAppDispatch } from '~/hooks/use-redux'
import useAxios from '~/hooks/use-axios'
import useConfirm from '~/hooks/use-confirm'
import { useModalContext } from '~/context/modal-context'
import AppPagination from '~/components/app-pagination/AppPagination'
import EnhancedTable, {
EnhancedTableProps
Expand All @@ -19,6 +20,7 @@ import {
ResourcesTabsEnum
} from '~/types'
import { roundedBorderTable } from '~/containers/my-cooperations/cooperations-container/CooperationContainer.styles'
import ChangeResourceConfirmModal from '~/containers/change-resource-confirm-modal/ChangeResourceConfirmModal'
import { openAlert } from '~/redux/features/snackbarSlice'
import { getErrorKey } from '~/utils/get-error-key'

Expand Down Expand Up @@ -47,6 +49,7 @@ const MyResourcesTable = <T extends TableItem>({
const { t } = useTranslation()
const dispatch = useAppDispatch()
const { openDialog } = useConfirm()
const { openModal } = useModalContext()

const { page, onChange } = pagination
const { response, getData } = data
Expand Down Expand Up @@ -86,10 +89,25 @@ const MyResourcesTable = <T extends TableItem>({
}

const onDelete = (id: string) => {
openDialog({
message: 'myResourcesPage.confirmDeletionMessage',
sendConfirm: (isConfirmed: boolean) => void handleDelete(id, isConfirmed),
title: `myResourcesPage.${resource}.confirmDeletionTitle`
const currentResource = response.items.find((item) => item._id === id)

const handleConfirm = () => {
openDialog({
message: 'myResourcesPage.confirmDeletionMessage',
sendConfirm: (isConfirmed: boolean) =>
void handleDelete(id, isConfirmed),
title: `myResourcesPage.${resource}.confirmDeletionTitle`
})
}

openModal({
component: (
<ChangeResourceConfirmModal
onConfirm={handleConfirm}
resourceId={id}
title={currentResource?.title ?? currentResource?.fileName}
/>
)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface TableRowAction {

export interface TableItem {
_id: string
title?: string
fileName?: string
}

export interface TableSelect<I> {
Expand Down
102 changes: 102 additions & 0 deletions tests/unit/containers/my-resources/MyResourcesTable.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { fireEvent, screen } from '@testing-library/react'

import MyResourcesTable from '~/containers/my-resources/my-resources-table/MyResourcesTable'

import { renderWithProviders } from '~tests/test-utils'

vi.mock('~/components/enhanced-table/EnhancedTable', () => ({
default: ({ rowActions }) => (
<div data-testid='table'>
{rowActions.map(({ label, func }) => (
<button data-testid={label} key={label} onClick={func}>
{label}
</button>
))}
</div>
)
}))

vi.mock('~/components/app-pagination/AppPagination', () => ({
default: () => <div data-testid='pagination' />
}))

vi.mock(
'~/containers/change-resource-confirm-modal/ChangeResourceConfirmModal',
() => ({
default: () => <div data-testid='confirmModal' />
})
)

const lessonMock = {
_id: '64e49ce305b3353b2ae6309e',
author: '648afee884936e09a37deaaa',
title: 'eew',
description: 'dsdfd',
attachments: [],
createdAt: '2023-08-22T11:32:51.995Z',
updatedAt: '2023-08-22T11:32:51.995Z'
}

const responseItemsMock = Array(10)
.fill()
.map((_, index) => ({
...lessonMock,
_id: `${index}`,
title: index + lessonMock.title
}))

const props = {
resource: 'lessons',
pagination: {
page: 1,
onChange: vi.fn()
},
data: {
response: {
items: responseItemsMock,
count: 10
},
getData: vi.fn()
},
actions: {
onEdit: vi.fn(),
onDuplicate: vi.fn()
},
services: {
deleteService: vi.fn()
}
}

describe('MyResourcesTable test', () => {
beforeEach(async () => {
renderWithProviders(<MyResourcesTable {...props} />)
})

it('should render table and pagination', () => {
const table = screen.getByTestId('table')
const pagination = screen.getByTestId('pagination')

expect(table).toBeInTheDocument()
expect(pagination).toBeInTheDocument()
})

it('should render table action buttons', async () => {
const editButton = await screen.findByTestId('common.edit')
const deleteButton = await screen.findByText('common.delete')
const duplicateButton = await screen.findByText('common.duplicate')

expect(editButton).toBeInTheDocument()
expect(deleteButton).toBeInTheDocument()
expect(duplicateButton).toBeInTheDocument()
})

it('should run onDelete action', async () => {
const deleteButton = await screen.findByText('common.delete')

fireEvent.click(deleteButton)

const modal = await screen.findByTestId('confirmModal')

expect(modal).toBeInTheDocument()
})
})

0 comments on commit a067c74

Please sign in to comment.