Skip to content

Commit

Permalink
Merge branch 'release-0.2.0' into feat/prashanth-email-trigger-1226
Browse files Browse the repository at this point in the history
  • Loading branch information
prv-proton authored Dec 10, 2024
2 parents 3d40414 + 7c77662 commit 017ff27
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 2 deletions.
2 changes: 1 addition & 1 deletion frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { AddEditOtherUses } from './views/OtherUses/AddEditOtherUses'
import { AddEditFinalSupplyEquipments } from './views/FinalSupplyEquipments/AddEditFinalSupplyEquipments'
import { AddEditFuelSupplies } from './views/FuelSupplies/AddEditFuelSupplies'
import { AddEditFuelExports } from './views/FuelExports/AddEditFuelExports'
import { AddEditAllocationAgreements } from './views/AllocationAgreements/AddAllocationAgreements'
import { AddEditAllocationAgreements } from './views/AllocationAgreements/AddEditAllocationAgreements'
import { logout } from '@/utils/keycloak.js'
import { CompareReports } from '@/views/CompareReports/CompareReports'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,21 @@ export const AddEditAllocationAgreements = () => {
const onGridReady = useCallback(
async (params) => {
setGridApi(params.api)
setRowData([...(data.allocationAgreements || { id: uuid() })])

if (
Array.isArray(data.allocationAgreements) &&
data.allocationAgreements.length > 0
) {
const updatedRowData = data.allocationAgreements.map((item) => ({
...item,
id: item.id || uuid() // Ensure every item has a unique ID
}))
setRowData(updatedRowData)
} else {
// If allocationAgreements is not available or empty, initialize with a single row
setRowData([{ id: uuid() }])
}

params.api.sizeColumnsToFit()
},
[data]
Expand Down
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)
})
})
})

0 comments on commit 017ff27

Please sign in to comment.