Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/components/Common/PaginationControl/PaginationControl.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { render, screen } from '@testing-library/react'
import { describe, test, expect, vi } from 'vitest'
import { PaginationControl } from './PaginationControl'
import type { PaginationControlProps } from './PaginationControlTypes'
import { ThemeProvider } from '@/contexts/ThemeProvider'
import { ComponentsProvider } from '@/contexts/ComponentAdapter/ComponentsProvider'
import { defaultComponents } from '@/contexts/ComponentAdapter/adapters/defaultComponentAdapter'

const basePaginationProps: PaginationControlProps = {
currentPage: 1,
totalPages: 3,
itemsPerPage: 5,
handleFirstPage: vi.fn(),
handlePreviousPage: vi.fn(),
handleNextPage: vi.fn(),
handleLastPage: vi.fn(),
handleItemsPerPageChange: vi.fn(),
}

const renderPaginationControl = (props: Partial<PaginationControlProps> = {}) => {
return render(
<ThemeProvider>
<ComponentsProvider value={defaultComponents}>
<PaginationControl {...basePaginationProps} {...props} />
</ComponentsProvider>
</ThemeProvider>,
)
Comment on lines +21 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend to update this to renderWithProviders instead, that'll give you the needed providers out of the box

}

describe('PaginationControl Visibility', () => {
describe('based on totalCount', () => {
test('hides when totalCount is 0 (empty state)', () => {
renderPaginationControl({ totalCount: 0 })
expect(screen.queryByTestId('pagination-control')).not.toBeInTheDocument()
})

test('hides when totalCount <= MINIMUM_PAGE_SIZE (5)', () => {
renderPaginationControl({ totalCount: 5 })
expect(screen.queryByTestId('pagination-control')).not.toBeInTheDocument()
})

test('hides when totalCount is 3 (less than min page size)', () => {
renderPaginationControl({ totalCount: 3 })
expect(screen.queryByTestId('pagination-control')).not.toBeInTheDocument()
})

test('shows when totalCount > MINIMUM_PAGE_SIZE', () => {
renderPaginationControl({ totalCount: 6 })
expect(screen.getByTestId('pagination-control')).toBeInTheDocument()
})

test('shows when totalCount is undefined (server info unavailable)', () => {
renderPaginationControl({ totalCount: undefined })
expect(screen.getByTestId('pagination-control')).toBeInTheDocument()
})

test('shows when totalCount is large', () => {
renderPaginationControl({ totalCount: 100 })
expect(screen.getByTestId('pagination-control')).toBeInTheDocument()
})
})

describe('edge cases', () => {
test('shows pagination when totalCount > 5 even with totalPages = 1', () => {
renderPaginationControl({ totalCount: 10, totalPages: 1, itemsPerPage: 50 })
expect(screen.getByTestId('pagination-control')).toBeInTheDocument()
})

test('hides when totalCount is exactly 5', () => {
renderPaginationControl({ totalCount: 5, totalPages: 1 })
expect(screen.queryByTestId('pagination-control')).not.toBeInTheDocument()
})
})
})
11 changes: 10 additions & 1 deletion src/components/Common/PaginationControl/PaginationControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ import PaginationPrevIcon from '@/assets/icons/pagination_previous.svg?react'
import PaginationNextIcon from '@/assets/icons/pagination_next.svg?react'
import PaginationLastIcon from '@/assets/icons/pagination_last.svg?react'

const MINIMUM_PAGE_SIZE = 5

const shouldShowPagination = (totalCount: number | undefined): boolean => {
if (totalCount === undefined) return true
if (totalCount === 0) return false
return totalCount > MINIMUM_PAGE_SIZE
}

const DefaultPaginationControl = ({
currentPage,
totalPages,
totalCount,
isFetching,
handleFirstPage,
handlePreviousPage,
Expand All @@ -23,7 +32,7 @@ const DefaultPaginationControl = ({
const { t } = useTranslation('common')
const Components = useComponentContext()

if (totalPages < 2) {
if (!shouldShowPagination(totalCount)) {
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type PaginationControlProps = {
handleItemsPerPageChange: (n: PaginationItemsPerPage) => void
currentPage: number
totalPages: number
totalCount?: number
itemsPerPage?: PaginationItemsPerPage
isFetching?: boolean
}
2 changes: 2 additions & 0 deletions src/components/Company/Locations/LocationsList/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const List = () => {
handleEditLocation,
currentPage,
totalPages,
totalCount,
handleFirstPage,
handleItemsPerPageChange,
handleLastPage,
Expand Down Expand Up @@ -89,6 +90,7 @@ export const List = () => {
handleItemsPerPageChange,
currentPage,
totalPages,
totalCount,
itemsPerPage,
},
emptyState: () => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function Root({ companyId, className, children }: LocationsListProps) {
} = useLocationsGetSuspense({ companyId, page: currentPage, per: itemsPerPage })

const totalPages = Number(httpMeta.response.headers.get('x-total-pages') ?? 1)
const totalCount = Number(httpMeta.response.headers.get('x-total-count') ?? 0)

const handleItemsPerPageChange = (newCount: PaginationItemsPerPage) => {
setItemsPerPage(newCount)
Expand Down Expand Up @@ -69,6 +70,7 @@ function Root({ companyId, className, children }: LocationsListProps) {
locationList: locationList ?? [],
currentPage,
totalPages,
totalCount,
handleFirstPage,
handlePreviousPage,
handleNextPage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { PaginationItemsPerPage } from '@/components/Common/PaginationContr
type LocationsListContextType = {
locationList: Location[]
totalPages: number
totalCount: number
currentPage: number
itemsPerPage: PaginationItemsPerPage
handleItemsPerPageChange: (n: PaginationItemsPerPage) => void
Expand Down
1 change: 1 addition & 0 deletions src/components/Contractor/ContractorList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ function Root({ companyId, className, dictionary, successMessage }: ContractorLi
handleItemsPerPageChange,
currentPage,
totalPages,
totalCount,
itemsPerPage,
},
})
Expand Down
2 changes: 2 additions & 0 deletions src/components/Employee/EmployeeList/EmployeeList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function Root({ companyId, className, children, dictionary }: EmployeeListProps)
const employees = employeeList!

const totalPages = Number(httpMeta.response.headers.get('x-total-pages') ?? 1)
const totalCount = Number(httpMeta.response.headers.get('x-total-count') ?? 0)

const handleItemsPerPageChange = (newCount: PaginationItemsPerPage) => {
setItemsPerPage(newCount)
Expand Down Expand Up @@ -139,6 +140,7 @@ function Root({ companyId, className, children, dictionary }: EmployeeListProps)
employees,
currentPage,
totalPages,
totalCount,
handleFirstPage,
handlePreviousPage,
handleNextPage,
Expand Down
2 changes: 2 additions & 0 deletions src/components/Employee/EmployeeList/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const List = () => {
handleItemsPerPageChange,
currentPage,
totalPages,
totalCount,
itemsPerPage,
handleSkip,
isFetching,
Expand Down Expand Up @@ -139,6 +140,7 @@ export const List = () => {
handleItemsPerPageChange,
currentPage,
totalPages,
totalCount,
itemsPerPage,
},
emptyState: () => (
Expand Down
1 change: 1 addition & 0 deletions src/components/Employee/EmployeeList/useEmployeeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type EmployeeListContextType = {
handleItemsPerPageChange: (newCount: PaginationItemsPerPage) => void
currentPage: number
totalPages: number
totalCount: number
employees: Employee[]
itemsPerPage: PaginationItemsPerPage
isFetching: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const Root = ({
}, [employeeData.showEmployees])

const totalPages = Number(employeeData.httpMeta.response.headers.get('x-total-pages') ?? 1)
const totalCount = Number(employeeData.httpMeta.response.headers.get('x-total-count') ?? 0)

const handleItemsPerPageChange = (newCount: PaginationItemsPerPage) => {
setItemsPerPage(newCount)
Expand All @@ -99,6 +100,7 @@ export const Root = ({
handleLastPage,
handleItemsPerPageChange,
totalPages,
totalCount,
isFetching: isFetchingEmployeeData,
itemsPerPage,
}
Expand Down
1 change: 0 additions & 1 deletion src/hooks/usePagination/index.ts

This file was deleted.

177 changes: 0 additions & 177 deletions src/hooks/usePagination/usePagination.test.ts

This file was deleted.

Loading