-
Notifications
You must be signed in to change notification settings - Fork 21
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
6346dac
commit 9ee5cc4
Showing
3 changed files
with
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { screen, waitFor } from '@testing-library/react'; | ||
import { h } from 'react-hyperscript-helpers'; | ||
import { ImportDataPage } from 'src/import-data/ImportDataPage'; | ||
import { useImportDrsRequest } from 'src/import-data/useImportDrsRequest'; | ||
import * as Nav from 'src/libs/nav'; | ||
import { renderWithAppContexts } from 'src/testing/test-utils'; | ||
|
||
jest.mock('src/libs/nav', () => ({ | ||
...jest.requireActual('src/libs/nav'), | ||
getCurrentUrl: jest.fn().mockReturnValue(new URL('https://app.terra.bio')), | ||
getLink: jest.fn().mockImplementation((_) => _), | ||
goToPath: jest.fn(), | ||
useRoute: jest.fn().mockReturnValue({ query: {} }), | ||
})); | ||
|
||
jest.mock('src/import-data/useImportDrsRequest', () => ({ | ||
useImportDrsRequest: jest.fn(), | ||
})); | ||
|
||
describe('ImportDataPage', () => { | ||
it('renders the ImportDataPage component with the correct title and content', async () => { | ||
// Arrange | ||
(Nav.useRoute as jest.Mock).mockReturnValue({ | ||
query: { format: 'dataset', referrer: 'data-catalog', drs: 'drs://example.com' }, | ||
}); | ||
|
||
// Act | ||
renderWithAppContexts(h(ImportDataPage)); | ||
|
||
// Assert | ||
await waitFor(() => { | ||
expect(screen.getByText('Catalog')).toBeInTheDocument(); | ||
expect(screen.getByRole('main')).toBeInTheDocument(); | ||
expect(screen.getByAltText('')).toBeInTheDocument(); | ||
expect(useImportDrsRequest).toHaveBeenCalledWith('drs://example.com'); | ||
}); | ||
}); | ||
|
||
it('renders the ImportDataPage component with the default title when referrer is not data-catalog', async () => { | ||
// Arrange | ||
(Nav.useRoute as jest.Mock).mockReturnValue({ | ||
query: { format: 'snapshot', referrer: 'other', drs: undefined }, | ||
}); | ||
|
||
// Act | ||
renderWithAppContexts(h(ImportDataPage)); | ||
|
||
// Assert | ||
await waitFor(() => { | ||
expect(screen.getByText('Import Snapshot')).toBeInTheDocument(); | ||
expect(screen.getByRole('main')).toBeInTheDocument(); | ||
expect(screen.getByAltText('')).toBeInTheDocument(); | ||
expect(useImportDrsRequest).toHaveBeenCalledWith(undefined); | ||
}); | ||
}); | ||
}); |
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,29 @@ | ||
import { useEffect } from 'react'; | ||
import { DrsUriResolver } from 'src/libs/ajax/drs/DrsUriResolver'; | ||
import * as Nav from 'src/libs/nav'; | ||
import { useCancellation } from 'src/libs/react-utils'; | ||
|
||
export const useImportDrsRequest = (drsUri: string | undefined) => { | ||
const signal = useCancellation(); | ||
|
||
useEffect(() => { | ||
if (!drsUri) return; | ||
|
||
const fetchData = async () => { | ||
try { | ||
const { contentType, accessUrl } = await DrsUriResolver(signal).getDataObjectMetadata( | ||
decodeURIComponent(drsUri), | ||
['contentType', 'accessUrl'] | ||
); | ||
|
||
if (contentType === 'application/pfb' && accessUrl) { | ||
Nav.updateSearch({ url: accessUrl }); | ||
} | ||
} catch (error) { | ||
console.error('Error fetching DRS URI metadata:', error); | ||
} | ||
}; | ||
|
||
fetchData().then(); | ||
}, [drsUri, signal]); | ||
}; |