-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release-0.2.0' into feat/prashanth-email-trigger-1226
- Loading branch information
Showing
3 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
frontend/src/views/AllocationAgreements/__tests__/AddEditAllocationAgreements.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// src/views/AllocationAgreements/__tests__/AddEditAllocationAgreements.test.jsx | ||
|
||
import React from 'react' | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react' | ||
import { describe, it, expect, beforeEach, vi } from 'vitest' | ||
import { AddEditAllocationAgreements } from '../AddEditAllocationAgreements' | ||
import * as useAllocationAgreementHook from '@/hooks/useAllocationAgreement' | ||
import { wrapper } from '@/tests/utils/wrapper' | ||
|
||
// Mock react-router-dom hooks | ||
const mockUseLocation = vi.fn() | ||
const mockUseNavigate = vi.fn() | ||
const mockUseParams = vi.fn() | ||
|
||
vi.mock('react-router-dom', () => ({ | ||
...vi.importActual('react-router-dom'), | ||
useLocation: () => mockUseLocation(), | ||
useNavigate: () => mockUseNavigate(), | ||
useParams: () => mockUseParams() | ||
})) | ||
|
||
// Mock react-i18next | ||
vi.mock('react-i18next', () => ({ | ||
useTranslation: () => ({ | ||
t: (key) => key | ||
}) | ||
})) | ||
|
||
// Mock hooks related to allocation agreements | ||
vi.mock('@/hooks/useAllocationAgreement') | ||
|
||
// Mock BCGridEditor component | ||
vi.mock('@/components/BCDataGrid/BCGridEditor', () => ({ | ||
BCGridEditor: ({ | ||
gridRef, | ||
alertRef, | ||
onGridReady, | ||
rowData, | ||
onCellValueChanged, | ||
onCellEditingStopped | ||
}) => ( | ||
<div data-test="bc-grid-editor"> | ||
<div data-test="row-data"> | ||
{rowData.map((row, index) => ( | ||
<div key={index} data-test="grid-row"> | ||
{row.id} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
) | ||
})) | ||
|
||
describe('AddEditAllocationAgreements', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks() | ||
|
||
// Mock react-router-dom hooks with complete location object | ||
mockUseLocation.mockReturnValue({ | ||
pathname: '/test-path', // Include pathname to prevent undefined errors | ||
state: {} | ||
}) | ||
mockUseNavigate.mockReturnValue(vi.fn()) | ||
mockUseParams.mockReturnValue({ | ||
complianceReportId: 'testReportId', | ||
compliancePeriod: '2024' | ||
}) | ||
|
||
// Mock useGetAllocationAgreements hook to return empty data initially | ||
vi.mocked( | ||
useAllocationAgreementHook.useGetAllocationAgreements | ||
).mockReturnValue({ | ||
data: { allocationAgreements: [] }, | ||
isLoading: false | ||
}) | ||
|
||
// Mock useAllocationAgreementOptions hook | ||
vi.mocked( | ||
useAllocationAgreementHook.useAllocationAgreementOptions | ||
).mockReturnValue({ | ||
data: { fuelTypes: [] }, | ||
isLoading: false, | ||
isFetched: true | ||
}) | ||
|
||
// Mock useSaveAllocationAgreement hook | ||
vi.mocked( | ||
useAllocationAgreementHook.useSaveAllocationAgreement | ||
).mockReturnValue({ | ||
mutateAsync: vi.fn() | ||
}) | ||
}) | ||
|
||
it('renders the component', () => { | ||
render(<AddEditAllocationAgreements />, { wrapper }) | ||
expect( | ||
screen.getByText('allocationAgreement:addAllocationAgreementRowsTitle') | ||
).toBeInTheDocument() | ||
}) | ||
|
||
it('initializes with at least one row in the empty state', () => { | ||
render(<AddEditAllocationAgreements />, { wrapper }) | ||
const rows = screen.getAllByTestId('grid-row') | ||
expect(rows.length).toBe(1) // Ensure at least one row exists | ||
}) | ||
|
||
it('loads data when allocationAgreements are available', async () => { | ||
// Update the mock to return allocation agreements | ||
vi.mocked( | ||
useAllocationAgreementHook.useGetAllocationAgreements | ||
).mockReturnValue({ | ||
data: { | ||
allocationAgreements: [ | ||
{ allocationAgreementId: 'testId1' }, | ||
{ allocationAgreementId: 'testId2' } | ||
] | ||
}, | ||
isLoading: false | ||
}) | ||
|
||
render(<AddEditAllocationAgreements />, { wrapper }) | ||
|
||
// Use findAllByTestId for asynchronous elements | ||
const rows = await screen.findAllByTestId('grid-row') | ||
expect(rows.length).toBe(2) | ||
// Check that each row's textContent matches UUID format | ||
const uuidRegex = | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i | ||
rows.forEach((row) => { | ||
expect(uuidRegex.test(row.textContent)).toBe(true) | ||
}) | ||
}) | ||
}) |