-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72e9e3f
commit 1b94e00
Showing
7 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
File renamed without changes.
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
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
47 changes: 47 additions & 0 deletions
47
src/test/__tests__/common/hooks/useComplexLookupApi.test.ts
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,47 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { useComplexLookupApi } from '@common/hooks/useComplexLookupApi'; | ||
import { useSearchFiltersData } from '@common/hooks/useSearchFiltersData'; | ||
|
||
jest.mock('@common/hooks/useSearchFiltersData'); | ||
|
||
describe('useComplexLookupApi', () => { | ||
const api = { | ||
endpoints: { | ||
facets: '/facets', | ||
source: '/source', | ||
}, | ||
sourceKey: 'testSourceKey', | ||
} as ComplexLookupApiEntryConfig; | ||
const filters = [ | ||
{ facet: 'facet_1', isOpen: false }, | ||
{ facet: 'facet_2', isOpen: true }, | ||
] as SearchFilters; | ||
|
||
let getSearchSourceDataMock: jest.Mock; | ||
let getSearchFacetsDataMock: jest.Mock; | ||
|
||
beforeEach(() => { | ||
getSearchSourceDataMock = jest.fn(); | ||
getSearchFacetsDataMock = jest.fn(); | ||
|
||
(useSearchFiltersData as jest.Mock).mockReturnValue({ | ||
getSearchSourceData: getSearchSourceDataMock, | ||
getSearchFacetsData: getSearchFacetsDataMock, | ||
}); | ||
}); | ||
|
||
it('calls getSearchFacetsData with correct arguments in getFacetsData', async () => { | ||
const { result } = renderHook(() => useComplexLookupApi(api, filters)); | ||
await result.current.getFacetsData('facet_1', true); | ||
|
||
expect(getSearchFacetsDataMock).toHaveBeenCalledWith(api.endpoints.facets, 'facet_1', true); | ||
}); | ||
|
||
it('calls getSearchSourceData and getFacetsData with correct arguments in getSourceData', async () => { | ||
const { result } = renderHook(() => useComplexLookupApi(api, filters)); | ||
await result.current.getSourceData(); | ||
|
||
expect(getSearchSourceDataMock).toHaveBeenCalledWith(api.endpoints.source, api.sourceKey); | ||
expect(getSearchFacetsDataMock).toHaveBeenCalledWith(api.endpoints.facets, 'facet_2', undefined); | ||
}); | ||
}); |
81 changes: 81 additions & 0 deletions
81
src/test/__tests__/components/MarcPreviewComplexLookup.test.tsx
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,81 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { RecoilRoot } from 'recoil'; | ||
import { useSearchContext } from '@common/hooks/useSearchContext'; | ||
import { MarcPreviewComplexLookup } from '@components/ComplexLookupField/MarcPreviewComplexLookup'; | ||
import state from '@state'; | ||
|
||
jest.mock('@common/hooks/useSearchContext'); | ||
jest.mock('@common/constants/build.constants', () => ({ IS_EMBEDDED_MODE: false })); | ||
jest.mock('@components/MarcContent', () => ({ MarcContent: () => <div>Marc Content</div> })); | ||
|
||
const mockUseSearchContext = useSearchContext as jest.Mock; | ||
const marcPreviewData = { | ||
metadata: { | ||
updatedDate: '2024-01-01', | ||
}, | ||
} as MarcDTO; | ||
const marcPreviewMetadata = { | ||
title: 'Test Title', | ||
headingType: 'Test Heading', | ||
baseId: 'testBaseId_1', | ||
} as MarcPreviewMetadata; | ||
|
||
describe('MarcPreviewComplexLookup', () => { | ||
const onClose = jest.fn(); | ||
const onAssignRecord = jest.fn(); | ||
|
||
beforeEach(() => { | ||
mockUseSearchContext.mockReturnValue({ onAssignRecord }); | ||
}); | ||
|
||
const renderComponent = ( | ||
isMarcPreviewOpen: boolean, | ||
marcPreviewData: MarcDTO, | ||
marcPreviewMetadata: MarcPreviewMetadata, | ||
) => { | ||
return render( | ||
<RecoilRoot | ||
initializeState={({ set }) => { | ||
set(state.ui.isMarcPreviewOpen, isMarcPreviewOpen); | ||
set(state.data.marcPreviewData, marcPreviewData); | ||
set(state.data.marcPreviewMetadata, marcPreviewMetadata); | ||
}} | ||
> | ||
<MarcPreviewComplexLookup onClose={onClose} /> | ||
</RecoilRoot>, | ||
); | ||
}; | ||
|
||
it('renders the component when isMarcPreviewOpen is true and marcPreviewData is available', () => { | ||
renderComponent(true, marcPreviewData, marcPreviewMetadata); | ||
|
||
expect(screen.getByText('Test Title')).toBeInTheDocument(); | ||
expect(screen.getByText('Marc Content')).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not render the component when isMarcPreviewOpen is false', () => { | ||
renderComponent(false, marcPreviewData, marcPreviewMetadata); | ||
|
||
expect(screen.queryByText('Test Title')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('calls onClose when close button is clicked', () => { | ||
renderComponent(true, marcPreviewData, marcPreviewMetadata); | ||
|
||
fireEvent.click(screen.getByTestId('nav-close-button')); | ||
|
||
expect(onClose).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls onAssignRecord when assign button is clicked', () => { | ||
renderComponent(true, marcPreviewData, marcPreviewMetadata); | ||
|
||
fireEvent.click(screen.getByText('ld.assign')); | ||
|
||
expect(onAssignRecord).toHaveBeenCalledWith({ | ||
id: 'testBaseId_1', | ||
title: 'Test Title', | ||
linkedFieldValue: 'Test Heading', | ||
}); | ||
}); | ||
}); |
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,43 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { TableFlex } from '@components/Table/TableFlex'; | ||
|
||
describe('TableFlex Component', () => { | ||
const header = { | ||
name: { label: 'Name', position: 1 }, | ||
age: { label: 'Age', position: 2 }, | ||
}; | ||
|
||
const data = [ | ||
{ __meta: { id: 1, key: 'row_1' }, name: { label: 'John Doe' }, age: { label: '30' } }, | ||
{ __meta: { id: 2, key: 'row_2' }, name: { label: 'Jane Doe' }, age: { label: '25' } }, | ||
]; | ||
|
||
const onRowClick = jest.fn(); | ||
const onHeaderCellClick = jest.fn(); | ||
|
||
test('renders TableFlex component', () => { | ||
render(<TableFlex header={header} data={data} onRowClick={onRowClick} onHeaderCellClick={onHeaderCellClick} />); | ||
|
||
expect(screen.getByTestId('table')).toBeInTheDocument(); | ||
expect(screen.getByTestId('th-name')).toHaveTextContent('Name'); | ||
expect(screen.getByTestId('th-age')).toHaveTextContent('Age'); | ||
expect(screen.getAllByTestId('table-row')).toHaveLength(2); | ||
}); | ||
|
||
test('calls onHeaderCellClick when header cell is clicked', () => { | ||
render(<TableFlex header={header} data={data} onRowClick={onRowClick} onHeaderCellClick={onHeaderCellClick} />); | ||
|
||
fireEvent.click(screen.getByTestId('th-name')); | ||
|
||
expect(onHeaderCellClick).toHaveBeenCalledWith({ name: header.name }); | ||
}); | ||
|
||
test('calls onRowClick when row is clicked', () => { | ||
render(<TableFlex header={header} data={data} onRowClick={onRowClick} onHeaderCellClick={onHeaderCellClick} />); | ||
|
||
fireEvent.click(screen.getAllByTestId('table-row')[0]); | ||
|
||
expect(onRowClick).toHaveBeenCalledWith(data[0]); | ||
}); | ||
}); |