Skip to content

Commit

Permalink
Revert "feat: Default to All branches when default branch has no comm…
Browse files Browse the repository at this point in the history
…its (#2446)" (#2448)

This reverts commit df16311.
  • Loading branch information
nicholas-codecov authored Dec 11, 2023
1 parent df16311 commit 194798b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 597 deletions.
43 changes: 5 additions & 38 deletions src/pages/RepoPage/CommitsTab/CommitsTab.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import {
lazy,
Suspense,
useEffect,
useLayoutEffect,
useRef,
useState,
} from 'react'
import { lazy, Suspense, useLayoutEffect, useState } from 'react'
import { useParams } from 'react-router-dom'

import { useBranchHasCommits } from 'services/branches'
import { useLocationParams } from 'services/navigation'
import { useRepoOverview, useRepoSettingsTeam } from 'services/repo'
import { TierNames, useTier } from 'services/tier'
Expand All @@ -23,7 +15,6 @@ import { useCommitsTabBranchSelector } from './hooks'

import { useSetCrumbs } from '../context'

const ALL_BRANCHES = 'All branches'
const CommitsTable = lazy(() => import('./CommitsTable'))
const CommitsTableTeam = lazy(() => import('./CommitsTableTeam'))

Expand All @@ -34,45 +25,21 @@ const Loader = () => (
)

const useControlParams = ({ defaultBranch }) => {
const initialRenderDone = useRef(false)
const { provider, owner, repo } = useParams()
const defaultParams = {
branch: defaultBranch,
states: [],
search: '',
}

const { params, updateParams } = useLocationParams(defaultParams)
let { branch: selectedBranch, states, search } = params
const { branch: selectedBranch, states, search } = params

const paramStatesNames = states.map((filter) => statusNames[filter])

const [selectedStates, setSelectedStates] = useState(paramStatesNames)

const { data: branchHasCommits } = useBranchHasCommits({
provider,
owner,
repo,
branch: selectedBranch,
opts: {
suspense: true,
enabled: !initialRenderDone.current,
},
})

useEffect(() => {
if (
branchHasCommits === false &&
selectedBranch !== ALL_BRANCHES &&
!initialRenderDone.current
) {
initialRenderDone.current = true
updateParams({ branch: ALL_BRANCHES })
}
}, [branchHasCommits, selectedBranch, updateParams])

let branch = selectedBranch
if (branch === ALL_BRANCHES) {
if (branch === 'All branches') {
branch = ''
}

Expand Down Expand Up @@ -125,7 +92,7 @@ function CommitsTab() {
} = useCommitsTabBranchSelector({
passedBranch: branch,
defaultBranch: overview?.defaultBranch,
isAllCommits: selectedBranch === ALL_BRANCHES,
isAllCommits: selectedBranch === 'All branches',
})

useLayoutEffect(() => {
Expand All @@ -143,7 +110,7 @@ function CommitsTab() {
])
}, [currentBranchSelected, setCrumbs])

const newBranches = [...(isSearching ? [] : [ALL_BRANCHES]), ...branchList]
const newBranches = [...(isSearching ? [] : ['All branches']), ...branchList]

const handleStatusChange = (selectStates) => {
const commitStates = selectStates?.map(
Expand Down
110 changes: 20 additions & 90 deletions src/pages/RepoPage/CommitsTab/CommitsTab.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { Suspense } from 'react'
import useIntersection from 'react-use/lib/useIntersection'

import { TierNames } from 'services/tier'
Expand All @@ -15,25 +14,21 @@ import { repoPageRender, screen, waitFor } from '../repo-jest-setup'
jest.mock('react-use/lib/useIntersection')

const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, suspense: true } },
defaultOptions: { queries: { retry: false } },
})
const server = setupServer()

const Wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
<Suspense fallback={<p>loading</p>}>{children}</Suspense>
</QueryClientProvider>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)

beforeAll(() => {
server.listen()
})

afterEach(() => {
queryClient.clear()
server.resetHandlers()
})

afterAll(() => {
server.close()
})
Expand Down Expand Up @@ -147,40 +142,11 @@ const mockCommitTeamResponse = {
},
}

const mockBranchHasCommits = {
owner: {
repository: {
__typename: 'Repository',
commits: {
edges: [
{
node: {
commitId: 'commit-123',
},
},
],
},
},
},
}

const mockBranchHasNoCommits = {
owner: {
repository: {
__typename: 'Repository',
commits: {
edges: [],
},
},
},
}

describe('CommitsTab', () => {
function setup({
hasNextPage,
hasBranches,
returnBranch = '',
branchHasCommits = true,
isPrivate = false,
tierValue = TierNames.PRO,
}) {
Expand Down Expand Up @@ -262,13 +228,6 @@ describe('CommitsTab', () => {
commitSearch(req?.variables?.filters?.search)
}
return res(ctx.status(200), ctx.data(mockCommitTeamResponse))
}),
graphql.query('GetBranchCommits', (req, res, ctx) => {
if (branchHasCommits) {
return res(ctx.status(200), ctx.data(mockBranchHasCommits))
} else {
return res(ctx.status(200), ctx.data(mockBranchHasNoCommits))
}
})
)

Expand All @@ -280,50 +239,21 @@ describe('CommitsTab', () => {
})

describe('when rendered', () => {
describe('rendering branch selector', () => {
describe('when branch has commits', () => {
it('uses default branch', async () => {
setup({ hasNextPage: true, returnBranch: 'main' })
repoPageRender({
renderCommits: () => (
<Wrapper>
<CommitsTab />
</Wrapper>
),
initialEntries: ['/gh/codecov/gazebo/commits'],
})

const selector = await screen.findByRole('button', {
name: 'Select branch',
})
expect(selector).toBeInTheDocument()

const selectedBranch = within(selector).getByText(/main/)
expect(selectedBranch).toBeInTheDocument()
})
it('renders branch context selector', async () => {
setup({ hasNextPage: true })
repoPageRender({
renderCommits: () => (
<Wrapper>
<CommitsTab />
</Wrapper>
),
initialEntries: ['/gh/codecov/gazebo/commits'],
})

describe('when branch has no commits', () => {
it('uses all branches', async () => {
setup({ branchHasCommits: false, returnBranch: 'main' })
repoPageRender({
renderCommits: () => (
<Wrapper>
<CommitsTab />
</Wrapper>
),
initialEntries: ['/gh/codecov/gazebo/commits'],
})

const selector = await screen.findByRole('button', {
name: 'Select branch',
})
expect(selector).toBeInTheDocument()

const selectedBranch = within(selector).getByText(/All branches/)
expect(selectedBranch).toBeInTheDocument()
})
const selector = await screen.findByRole('button', {
name: 'Select branch',
})
expect(selector).toBeInTheDocument()
})

it('renders ci status mutliselect', async () => {
Expand All @@ -345,7 +275,7 @@ describe('CommitsTab', () => {
})

describe('rendering CommitsTable', () => {
it('renders with table name heading', async () => {
it('renders with table name heading', () => {
setup({ hasNextPage: true })
repoPageRender({
renderCommits: () => (
Expand All @@ -356,11 +286,11 @@ describe('CommitsTab', () => {
initialEntries: ['/gh/codecov/gazebo/commits'],
})

const head = await screen.findByText(/Name/)
const head = screen.getByText(/Name/)
expect(head).toBeInTheDocument()
})

it('renders with table coverage heading', async () => {
it('renders with table coverage heading', () => {
setup({ hasNextPage: true })
repoPageRender({
renderCommits: () => (
Expand All @@ -371,11 +301,11 @@ describe('CommitsTab', () => {
initialEntries: ['/gh/codecov/gazebo/commits'],
})

const head = await screen.findByText(/Coverage/)
const head = screen.getByText(/Coverage/)
expect(head).toBeInTheDocument()
})

it('renders with table change heading', async () => {
it('renders with table change heading', () => {
setup({ hasNextPage: true })
repoPageRender({
renderCommits: () => (
Expand All @@ -386,7 +316,7 @@ describe('CommitsTab', () => {
initialEntries: ['/gh/codecov/gazebo/commits'],
})

const head = await screen.findByText(/Change/)
const head = screen.getByText(/Change/)
expect(head).toBeInTheDocument()
})

Expand Down
1 change: 0 additions & 1 deletion src/services/branches/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './useBranch'
export * from './useBranches'
export * from './useBranchComponents'
export * from './useBranchHasCommits'
Loading

0 comments on commit 194798b

Please sign in to comment.