Skip to content

Commit

Permalink
Auto expand pr table if param passed (#2783)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitvinnakota-codecov authored Apr 18, 2024
1 parent 3d519ac commit 9ecb0dd
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,14 @@ const mockPull = ({
const server = setupServer()

const wrapper =
(queryClient: QueryClient): React.FC<React.PropsWithChildren> =>
(
queryClient: QueryClient,
initialEntries = ['/gh/codecov/test-repo/pull/s2h5a6']
): React.FC<React.PropsWithChildren> =>
({ children }) =>
(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/gh/codecov/test-repo/pull/s2h5a6']}>
<MemoryRouter initialEntries={initialEntries}>
<Route path="/:provider/:owner/:repo/pull/:pull">{children}</Route>
</MemoryRouter>
</QueryClientProvider>
Expand Down Expand Up @@ -374,6 +377,18 @@ describe('FilesChangedTable', () => {
const pullFileDiff = await screen.findByText('FileDiff')
expect(pullFileDiff).toBeInTheDocument()
})

it('auto expands if param is passed in url', async () => {
const { queryClient } = setup()
render(<FilesChangedTable />, {
wrapper: wrapper(queryClient, [
'/gh/codecov/test-repo/pull/s2h5a6?filepath=flag1/mafs.js',
]),
})

const pullFileDiff = await screen.findByText('FileDiff')
expect(pullFileDiff).toBeInTheDocument()
})
})

describe('highlights critical files', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import {
import cs from 'classnames'
import isEmpty from 'lodash/isEmpty'
import isNumber from 'lodash/isNumber'
import { Fragment, lazy, Suspense, useMemo, useState } from 'react'
import { useParams } from 'react-router-dom'
import qs from 'qs'
import { Fragment, lazy, Suspense, useEffect, useMemo, useState } from 'react'
import { useLocation , useParams } from 'react-router-dom'

import {
ImpactedFile,
Expand Down Expand Up @@ -246,6 +247,11 @@ export default function FilesChangedTable() {
{ id: 'missedLines', desc: true },
])
const { provider, owner, repo, pullId } = useParams<URLParams>()
const location = useLocation()
const params = qs.parse(location.search, {
ignoreQueryPrefix: true,
})
const currentlySelectedFile = params.filepath

const { data: pullData, isLoading } = usePull({
provider,
Expand Down Expand Up @@ -274,6 +280,19 @@ export default function FilesChangedTable() {
return []
}, [pullData?.pull?.compareWithBase])

useEffect(() => {
if (data.length > 0 && currentlySelectedFile) {
const fileToExpandIndex = data.findIndex(
(file) => file && file.headName === currentlySelectedFile
)
if (fileToExpandIndex !== -1) {
setExpanded({
[fileToExpandIndex]: true,
})
}
}
}, [data, currentlySelectedFile])

const table = useReactTable({
columns: getColumns({ pullId }),
data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,14 @@ const mockNoChangeFileData = {
const server = setupServer()

const wrapper =
(queryClient: QueryClient): React.FC<React.PropsWithChildren> =>
(
queryClient: QueryClient,
initialEntries = ['/gh/codecov/test-repo/pull/s2h5a6']
): React.FC<React.PropsWithChildren> =>
({ children }) =>
(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/gh/codecov/test-repo/pull/s2h5a6']}>
<MemoryRouter initialEntries={initialEntries}>
<Route path="/:provider/:owner/:repo/pull/:pull">{children}</Route>
</MemoryRouter>
</QueryClientProvider>
Expand Down Expand Up @@ -425,6 +428,18 @@ describe('TableTeam', () => {
const pullFileDiff = await screen.findByText('FileDiff')
expect(pullFileDiff).toBeInTheDocument()
})

it('auto expands if param is passed in url', async () => {
const { queryClient } = setup()
render(<TableTeam />, {
wrapper: wrapper(queryClient, [
'/gh/codecov/test-repo/pull/s2h5a6?filepath=src/App.jsx',
]),
})

const pullFileDiff = await screen.findByText('FileDiff')
expect(pullFileDiff).toBeInTheDocument()
})
})

describe('highlights critical files', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {
} from '@tanstack/react-table'
import cs from 'classnames'
import isEmpty from 'lodash/isEmpty'
import { Fragment, lazy, Suspense, useMemo, useState } from 'react'
import { useParams } from 'react-router-dom'
import qs from 'qs'
import { Fragment, lazy, Suspense, useEffect, useMemo, useState } from 'react'
import { useLocation , useParams } from 'react-router-dom'

import {
ImpactedFile,
Expand Down Expand Up @@ -174,6 +175,12 @@ export default function FilesChangedTableTeam() {
mostRecentCompare = pullData?.pull?.compareWithBase
}

const location = useLocation()
const params = qs.parse(location.search, {
ignoreQueryPrefix: true,
})
const currentlySelectedFile = params.filepath

const data = useMemo(() => {
if (
pullData?.pull?.compareWithBase?.__typename === 'Comparison' &&
Expand All @@ -185,6 +192,19 @@ export default function FilesChangedTableTeam() {
return []
}, [pullData?.pull?.compareWithBase])

useEffect(() => {
if (data.length > 0 && currentlySelectedFile) {
const fileToExpandIndex = data.findIndex(
(file) => file && file.headName === currentlySelectedFile
)
if (fileToExpandIndex !== -1) {
setExpanded({
[fileToExpandIndex]: true,
})
}
}
}, [data, currentlySelectedFile])

const table = useReactTable({
columns: getColumns({ pullId }),
data,
Expand Down

0 comments on commit 9ecb0dd

Please sign in to comment.