-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app, api-client, react-api-client): add api-client function for …
…`getCsvFile` (#15926) Add new api-client function for `getCsvFile`, which takes a fileId and returns `CsvFileData`: ``` { id: string createdAt: string name: string } ``` Wrap in react query hook and implement in `HistoricalProtocolRunDrawer`.
- Loading branch information
Showing
10 changed files
with
185 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { GET, request } from '../request' | ||
|
||
import type { CsvFileDataResponse } from './types' | ||
import type { ResponsePromise } from '../request' | ||
import type { HostConfig } from '../types' | ||
|
||
export function getCsvFile( | ||
config: HostConfig, | ||
fileId: string | ||
): ResponsePromise<CsvFileDataResponse> { | ||
return request<CsvFileDataResponse>(GET, `/dataFiles/${fileId}`, null, config) | ||
} |
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
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
78 changes: 78 additions & 0 deletions
78
react-api-client/src/dataFiles/__tests__/useCsvFileQuery.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,78 @@ | ||
import * as React from 'react' | ||
import { describe, it, expect, beforeEach, vi } from 'vitest' | ||
import { QueryClient, QueryClientProvider } from 'react-query' | ||
import { renderHook, waitFor } from '@testing-library/react' | ||
import { getCsvFile } from '@opentrons/api-client' | ||
import { useHost } from '../../api' | ||
import { useCsvFileQuery } from '..' | ||
|
||
import type { | ||
CsvFileData, | ||
CsvFileDataResponse, | ||
HostConfig, | ||
Response, | ||
} from '@opentrons/api-client' | ||
|
||
vi.mock('@opentrons/api-client') | ||
vi.mock('../../api/useHost') | ||
|
||
const HOST_CONFIG: HostConfig = { hostname: 'localhost' } | ||
const FILE_ID = 'file123' | ||
const FILE_NAME = 'my_file.csv' | ||
const FILE_CONTENT_RESPONSE = { | ||
data: { | ||
name: FILE_NAME, | ||
id: FILE_ID, | ||
createdAt: '2024-06-07T19:19:56.268029+00:00', | ||
} as CsvFileData, | ||
} as CsvFileDataResponse | ||
|
||
describe('useCsvFileQuery hook', () => { | ||
let wrapper: React.FunctionComponent<{ children: React.ReactNode }> | ||
|
||
beforeEach(() => { | ||
const queryClient = new QueryClient() | ||
const clientProvider: React.FunctionComponent<{ | ||
children: React.ReactNode | ||
}> = ({ children }) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
) | ||
|
||
wrapper = clientProvider | ||
}) | ||
|
||
it('should return no data if no host', () => { | ||
vi.mocked(useHost).mockReturnValue(null) | ||
|
||
const { result } = renderHook(() => useCsvFileQuery(FILE_ID), { | ||
wrapper, | ||
}) | ||
|
||
expect(result.current.data).toBeUndefined() | ||
}) | ||
|
||
it('should return no data if the get file request fails', () => { | ||
vi.mocked(useHost).mockReturnValue(HOST_CONFIG) | ||
vi.mocked(getCsvFile).mockRejectedValue('oh no') | ||
|
||
const { result } = renderHook(() => useCsvFileQuery(FILE_ID), { | ||
wrapper, | ||
}) | ||
expect(result.current.data).toBeUndefined() | ||
}) | ||
|
||
it('should return file data if successful request', async () => { | ||
vi.mocked(useHost).mockReturnValue(HOST_CONFIG) | ||
vi.mocked(getCsvFile).mockResolvedValue({ | ||
data: FILE_CONTENT_RESPONSE, | ||
} as Response<CsvFileDataResponse>) | ||
|
||
const { result } = renderHook(() => useCsvFileQuery(FILE_ID), { | ||
wrapper, | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(result.current.data).toEqual(FILE_CONTENT_RESPONSE) | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { useCsvFileQuery } from './useCsvFileQuery' | ||
export { useCsvFileRawQuery } from './useCsvFileRawQuery' | ||
export { useUploadCsvFileMutation } from './useUploadCsvFileMutation' |
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,25 @@ | ||
import { useQuery } from 'react-query' | ||
import { getCsvFile } from '@opentrons/api-client' | ||
import { useHost } from '../api' | ||
|
||
import type { UseQueryOptions, UseQueryResult } from 'react-query' | ||
import type { HostConfig, CsvFileDataResponse } from '@opentrons/api-client' | ||
|
||
export function useCsvFileQuery( | ||
fileId: string, | ||
options?: UseQueryOptions<CsvFileDataResponse> | ||
): UseQueryResult<CsvFileDataResponse> { | ||
const host = useHost() | ||
const allOptions: UseQueryOptions<CsvFileDataResponse> = { | ||
...options, | ||
enabled: host !== null && fileId !== null, | ||
} | ||
|
||
const query = useQuery<CsvFileDataResponse>( | ||
[host, 'dataFiles', fileId], | ||
() => | ||
getCsvFile(host as HostConfig, fileId).then(response => response.data), | ||
allOptions | ||
) | ||
return query | ||
} |
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