This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
DEVPROD-4193: Redirect to project identifier on Project Health page
Showing
7 changed files
with
211 additions
and
9 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
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,6 @@ | ||
query Project($idOrIdentifier: String!) { | ||
project(projectIdentifier: $idOrIdentifier) { | ||
id | ||
identifier | ||
} | ||
} |
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,36 @@ | ||
import { useQuery } from "@apollo/client"; | ||
import { useParams, useLocation, useNavigate } from "react-router-dom"; | ||
import { ProjectQuery, ProjectQueryVariables } from "gql/generated/types"; | ||
import { PROJECT } from "gql/queries"; | ||
import { validators } from "utils"; | ||
|
||
const { validateObjectId } = validators; | ||
|
||
/** | ||
* useProjectRedirect will replace the project id with the project identifier in the URL. | ||
* @returns isRedirecting - boolean to indicate if a redirect is in progress | ||
*/ | ||
export const useProjectRedirect = () => { | ||
const { projectIdentifier: project } = useParams<{ | ||
projectIdentifier: string; | ||
}>(); | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
|
||
const needsRedirect = validateObjectId(project); | ||
|
||
const { loading } = useQuery<ProjectQuery, ProjectQueryVariables>(PROJECT, { | ||
skip: !needsRedirect, | ||
variables: { | ||
idOrIdentifier: project, | ||
}, | ||
onCompleted: (projectData) => { | ||
const { identifier } = projectData.project; | ||
const currentUrl = location.pathname.concat(location.search); | ||
const redirectPathname = currentUrl.replace(project, identifier); | ||
navigate(redirectPathname); | ||
}, | ||
}); | ||
|
||
return { isRedirecting: needsRedirect && loading }; | ||
}; |
137 changes: 137 additions & 0 deletions
137
src/hooks/useProjectRedirect/useProjectRedirect.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,137 @@ | ||
import { MockedProvider } from "@apollo/client/testing"; | ||
import { GraphQLError } from "graphql"; | ||
import { MemoryRouter, Routes, Route, useLocation } from "react-router-dom"; | ||
import { ProjectQuery, ProjectQueryVariables } from "gql/generated/types"; | ||
import { PROJECT } from "gql/queries"; | ||
import { renderHook, waitFor } from "test_utils"; | ||
import { ApolloMock } from "types/gql"; | ||
import { useProjectRedirect } from "."; | ||
|
||
const useJointHook = () => { | ||
const { isRedirecting } = useProjectRedirect(); | ||
const { pathname, search } = useLocation(); | ||
return { isRedirecting, pathname, search }; | ||
}; | ||
|
||
describe("useProjectRedirect", () => { | ||
const ProviderWrapper: React.FC<{ | ||
children: React.ReactNode; | ||
location: string; | ||
}> = ({ children, location }) => ( | ||
<MockedProvider mocks={[repoMock, projectMock]}> | ||
<MemoryRouter initialEntries={[location]}> | ||
<Routes> | ||
<Route element={children} path="/commits/:projectIdentifier" /> | ||
</Routes> | ||
</MemoryRouter> | ||
</MockedProvider> | ||
); | ||
|
||
it("should not redirect if URL has project identifier", async () => { | ||
const { result } = renderHook(() => useJointHook(), { | ||
wrapper: ({ children }) => | ||
ProviderWrapper({ children, location: "/commits/my-project" }), | ||
}); | ||
expect(result.current).toMatchObject({ | ||
isRedirecting: false, | ||
pathname: "/commits/my-project", | ||
search: "", | ||
}); | ||
}); | ||
|
||
it("should redirect if URL has project ID", async () => { | ||
const { result } = renderHook(() => useJointHook(), { | ||
wrapper: ({ children }) => | ||
ProviderWrapper({ children, location: `/commits/${projectId}` }), | ||
}); | ||
expect(result.current).toMatchObject({ | ||
isRedirecting: true, | ||
pathname: "/commits/5f74d99ab2373627c047c5e5", | ||
search: "", | ||
}); | ||
await waitFor(() => { | ||
expect(result.current).toMatchObject({ | ||
isRedirecting: false, | ||
pathname: "/commits/my-project", | ||
search: "", | ||
}); | ||
}); | ||
}); | ||
|
||
it("should preserve query params when redirecting", async () => { | ||
const { result } = renderHook(() => useJointHook(), { | ||
wrapper: ({ children }) => | ||
ProviderWrapper({ | ||
children, | ||
location: `/commits/${projectId}?taskName=thirdparty`, | ||
}), | ||
}); | ||
expect(result.current).toMatchObject({ | ||
isRedirecting: true, | ||
pathname: "/commits/5f74d99ab2373627c047c5e5", | ||
search: "?taskName=thirdparty", | ||
}); | ||
await waitFor(() => { | ||
expect(result.current).toMatchObject({ | ||
isRedirecting: false, | ||
pathname: "/commits/my-project", | ||
search: "?taskName=thirdparty", | ||
}); | ||
}); | ||
}); | ||
|
||
it("should attempt redirect if URL has repo ID but stop attempting after query", async () => { | ||
const { result } = renderHook(() => useJointHook(), { | ||
wrapper: ({ children }) => | ||
ProviderWrapper({ children, location: `/commits/${repoId}` }), | ||
}); | ||
expect(result.current).toMatchObject({ | ||
isRedirecting: true, | ||
pathname: "/commits/5e6bb9e23066155a993e0f1a", | ||
search: "", | ||
}); | ||
await waitFor(() => { | ||
expect(result.current).toMatchObject({ | ||
isRedirecting: false, | ||
pathname: "/commits/5e6bb9e23066155a993e0f1a", | ||
search: "", | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
const projectId = "5f74d99ab2373627c047c5e5"; | ||
const projectMock: ApolloMock<ProjectQuery, ProjectQueryVariables> = { | ||
request: { | ||
query: PROJECT, | ||
variables: { | ||
idOrIdentifier: projectId, | ||
}, | ||
}, | ||
result: { | ||
data: { | ||
project: { | ||
__typename: "Project", | ||
id: projectId, | ||
identifier: "my-project", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const repoId = "5e6bb9e23066155a993e0f1a"; | ||
const repoMock: ApolloMock<ProjectQuery, ProjectQueryVariables> = { | ||
request: { | ||
query: PROJECT, | ||
variables: { | ||
idOrIdentifier: repoId, | ||
}, | ||
}, | ||
result: { | ||
errors: [ | ||
new GraphQLError( | ||
`Error finding project by id ${repoId}: 404 (Not Found): project '${repoId}' not found`, | ||
), | ||
], | ||
}, | ||
}; |
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