-
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.
refactor(app): improve top level query chaining
- Loading branch information
Showing
8 changed files
with
237 additions
and
69 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
141 changes: 141 additions & 0 deletions
141
app/src/App/TopLevelRedirects/hooks/__tests__/useCurrentRunRoute.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,141 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import { describe, it, expect, vi } from 'vitest' | ||
import { useCurrentRunRoute } from '../useCurrentRunRoute' | ||
import { useNotifyRunQuery } from '../../../../resources/runs' | ||
import { | ||
RUN_ACTION_TYPE_PLAY, | ||
RUN_STATUS_BLOCKED_BY_OPEN_DOOR, | ||
RUN_STATUS_FAILED, | ||
RUN_STATUS_IDLE, | ||
RUN_STATUS_STOPPED, | ||
RUN_STATUS_SUCCEEDED, | ||
} from '@opentrons/api-client' | ||
|
||
vi.mock('../../../../resources/runs') | ||
|
||
const MOCK_RUN_ID = 'MOCK_RUN_ID' | ||
|
||
describe('useCurrentRunRoute', () => { | ||
it('returns null when the run record is null', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: null, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBeNull() | ||
}) | ||
|
||
it('returns null when isFetching is true', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { data: {} }, | ||
isFetching: true, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBeNull() | ||
}) | ||
|
||
it('returns the summary route for a run with succeeded status', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { id: MOCK_RUN_ID, status: RUN_STATUS_SUCCEEDED, actions: [] }, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/summary`) | ||
}) | ||
|
||
it('returns the summary route for a started run that has a stopped status', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { | ||
id: MOCK_RUN_ID, | ||
status: RUN_STATUS_STOPPED, | ||
actions: [{ actionType: RUN_ACTION_TYPE_PLAY }], | ||
}, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/summary`) | ||
}) | ||
|
||
it('returns summary route for a run with failed status', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { id: MOCK_RUN_ID, status: RUN_STATUS_FAILED, actions: [] }, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/summary`) | ||
}) | ||
|
||
it('returns the setup route for a run with an idle status', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { data: { id: MOCK_RUN_ID, status: RUN_STATUS_IDLE, actions: [] } }, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/setup`) | ||
}) | ||
|
||
it('returns the setup route for a "blocked by open door" run that has not been started yet', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { | ||
id: MOCK_RUN_ID, | ||
status: RUN_STATUS_BLOCKED_BY_OPEN_DOOR, | ||
actions: [], | ||
}, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/setup`) | ||
}) | ||
|
||
it('returns run route for a started run', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { | ||
id: MOCK_RUN_ID, | ||
status: RUN_STATUS_BLOCKED_BY_OPEN_DOOR, | ||
actions: [{ actionType: RUN_ACTION_TYPE_PLAY }], | ||
}, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/run`) | ||
}) | ||
|
||
it('returns null for cancelled run before starting', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { id: MOCK_RUN_ID, status: RUN_STATUS_STOPPED, actions: [] }, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBeNull() | ||
}) | ||
}) |
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 @@ | ||
export { useCurrentRunRoute } from './useCurrentRunRoute' |
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,50 @@ | ||
import { | ||
RUN_ACTION_TYPE_PLAY, | ||
RUN_STATUS_BLOCKED_BY_OPEN_DOOR, | ||
RUN_STATUS_FAILED, | ||
RUN_STATUS_IDLE, | ||
RUN_STATUS_STOPPED, | ||
RUN_STATUS_SUCCEEDED, | ||
} from '@opentrons/api-client' | ||
|
||
import { useNotifyRunQuery } from '../../../resources/runs' | ||
import { CURRENT_RUN_POLL } from '../../constants' | ||
|
||
export function useCurrentRunRoute(currentRunId: string): string | null { | ||
const { data: runRecord, isFetching } = useNotifyRunQuery(currentRunId, { | ||
refetchInterval: CURRENT_RUN_POLL, | ||
}) | ||
|
||
const runStatus = runRecord?.data.status | ||
const runActions = runRecord?.data.actions | ||
if ( | ||
runRecord == null || | ||
runStatus == null || | ||
runActions == null || | ||
isFetching | ||
) { | ||
return null | ||
} | ||
// grabbing run id off of the run query to have all routing info come from one source of truth | ||
const runId = runRecord.data.id | ||
const hasRunStarted = runActions?.some( | ||
action => action.actionType === RUN_ACTION_TYPE_PLAY | ||
) | ||
if ( | ||
runStatus === RUN_STATUS_SUCCEEDED || | ||
(runStatus === RUN_STATUS_STOPPED && hasRunStarted) || | ||
runStatus === RUN_STATUS_FAILED | ||
) { | ||
return `/runs/${runId}/summary` | ||
} else if ( | ||
runStatus === RUN_STATUS_IDLE || | ||
(!hasRunStarted && runStatus === RUN_STATUS_BLOCKED_BY_OPEN_DOOR) | ||
) { | ||
return `/runs/${runId}/setup` | ||
} else if (hasRunStarted) { | ||
return `/runs/${runId}/run` | ||
} else { | ||
// includes runs cancelled before starting and runs not yet started | ||
return null | ||
} | ||
} |
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,28 @@ | ||
import * as React from 'react' | ||
import { Navigate, Route, Routes } from 'react-router-dom' | ||
|
||
import { useCurrentRunId } from '../../resources/runs' | ||
import { CURRENT_RUN_POLL } from '../constants' | ||
import { useCurrentRunRoute } from './hooks' | ||
|
||
export function TopLevelRedirects(): JSX.Element | null { | ||
const currentRunId = useCurrentRunId({ refetchInterval: CURRENT_RUN_POLL }) | ||
|
||
return currentRunId != null ? ( | ||
<CurrentRunRoute currentRunId={currentRunId} /> | ||
) : null | ||
} | ||
|
||
function CurrentRunRoute({ | ||
currentRunId, | ||
}: { | ||
currentRunId: string | ||
}): JSX.Element | null { | ||
const currentRunRoute = useCurrentRunRoute(currentRunId) | ||
|
||
return currentRunRoute != null ? ( | ||
<Routes> | ||
<Route path="*" element={<Navigate to={currentRunRoute} />} /> | ||
</Routes> | ||
) : null | ||
} |
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