Skip to content

Commit

Permalink
update with more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RulaKhaled committed Dec 5, 2024
1 parent 4ac0b0c commit 16a52e2
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event'
import { graphql, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import qs from 'qs'
import { mockIsIntersecting } from 'react-intersection-observer/test-utils'
import { MemoryRouter, Route } from 'react-router-dom'

import CodeTreeTable from './CodeTreeTable'
Expand Down Expand Up @@ -131,7 +132,40 @@ const mockTreeData = {
},
}

const mockDataMultipleRows = {
const node1 = {
__typename: 'PathContentDir',
hits: 9,
misses: 0,
partials: 0,
lines: 10,
name: 'dir1',
path: 'dir1',
percentCovered: 100.0,
}

const node2 = {
__typename: 'PathContentDir',
hits: 9,
misses: 2,
partials: 1,
lines: 999,
name: 'dir2',
path: 'dir2',
percentCovered: 100.0,
}

const node3 = {
__typename: 'PathContentDir',
hits: 9,
misses: 2,
partials: 1,
lines: 999,
name: 'dir3',
path: 'dir3',
percentCovered: 100.0,
}

const mockDataMultipleRows = (after: string) => ({
owner: {
username: 'codecov-tree',
repository: {
Expand All @@ -146,42 +180,19 @@ const mockDataMultipleRows = {
head: {
deprecatedPathContents: {
__typename: 'PathContentConnection',
edges: [
{
node: {
__typename: 'PathContentDir',
hits: 9,
misses: 0,
partials: 0,
lines: 10,
name: 'src',
path: 'src',
percentCovered: 100.0,
},
},
{
node: {
__typename: 'PathContentDir',
hits: 9,
misses: 2,
partials: 1,
lines: 999,
name: 'tests',
path: 'tests',
percentCovered: 100.0,
},
},
],
edges: after
? [{ node: node3 }]
: [{ node: node1 }, { node: node2 }],
pageInfo: {
hasNextPage: false,
endCursor: null,
hasNextPage: after ? true : false,
endCursor: after ? 'cursor3' : 'cursor2',
},
},
},
},
},
},
}
})

const mockTreeDataNested = {
owner: {
Expand Down Expand Up @@ -322,7 +333,9 @@ describe('CodeTreeTable', () => {
} else if (noFlagCoverage) {
return HttpResponse.json({ data: mockNoFiles })
} else if (hasMultipleRows) {
return HttpResponse.json({ data: mockDataMultipleRows })
return HttpResponse.json({
data: mockDataMultipleRows(info.variables.after),
})
} else if (isNestedTreeData) {
return HttpResponse.json({ data: mockTreeDataNested })
} else {
Expand Down Expand Up @@ -757,4 +770,29 @@ describe('CodeTreeTable', () => {
})
})
})
describe('testing pagination', () => {
it('displays the first page', async () => {
setup({ hasMultipleRows: true })
render(<CodeTreeTable />, { wrapper: wrapper() })
const loading = await screen.findByText('Loading')
mockIsIntersecting(loading, false)

const page1Dir1 = await screen.findByText('dir1')
expect(page1Dir1).toBeInTheDocument()

const page1Dir2 = await screen.findByText('dir2')
expect(page1Dir2).toBeInTheDocument()
})

it('displays the second page', async () => {
setup({ hasMultipleRows: true })
render(<CodeTreeTable />, { wrapper: wrapper() })

const loading = await screen.findByText('Loading')
mockIsIntersecting(loading, true)

const page2Dir1 = await screen.findByText('dir3')
expect(page2Dir1).toBeInTheDocument()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event'
import { graphql, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import qs from 'qs'
import { mockIsIntersecting } from 'react-intersection-observer/test-utils'
import { MemoryRouter, Route } from 'react-router-dom'

import FileListTable from './FileListTable'
Expand Down Expand Up @@ -90,8 +91,43 @@ const mockMissingCoverage = {
},
},
}
const node1 = {
__typename: 'PathContentFile',
hits: 9,
misses: 0,
partials: 0,
lines: 10,
name: 'file1.js',
path: 'a/b/c/file.js',
percentCovered: 100.0,
isCriticalFile: false,
}

const node2 = {
__typename: 'PathContentFile',
hits: 5,
misses: 2,
partials: 1,
lines: 8,
name: 'file2.js',
path: 'a/b/c/test.js',
percentCovered: 62.5,
isCriticalFile: false,
}

const node3 = {
__typename: 'PathContentFile',
hits: 15,
misses: 5,
partials: 0,
lines: 20,
name: 'file3.js',
path: 'a/b/c/index.js',
percentCovered: 75.0,
isCriticalFile: true,
}

const mockListData = {
const mockListData = (after = false) => ({
owner: {
username: 'cool-codecov',
repository: {
Expand All @@ -106,31 +142,21 @@ const mockListData = {
head: {
deprecatedPathContents: {
__typename: 'PathContentConnection',
edges: [
{
node: {
__typename: 'PathContentFile',
hits: 9,
misses: 0,
partials: 0,
lines: 10,
name: 'file.js',
path: 'a/b/c/file.js',
percentCovered: 100.0,
isCriticalFile: false,
},
},
],
edges: after
? [{ node: node3 }]
: [{ node: node1 }, { node: node2 }],
pageInfo: {
hasNextPage: false,
endCursor: null,
hasNextPage: after ? false : true,
endCursor: after
? 'aa'
: 'MjAyMC0wOC0xMSAxNzozMDowMiswMDowMHwxMDA=',
},
},
},
},
},
},
}
})

const mockNoHeadReport = {
owner: {
Expand Down Expand Up @@ -227,7 +253,7 @@ describe('FileListTable', () => {
return HttpResponse.json({ data: mockUnknownPath })
}

return HttpResponse.json({ data: mockListData })
return HttpResponse.json({ data: mockListData(info.variables.after) })
}),
graphql.query('GetRepoOverview', (info) => {
return HttpResponse.json({ data: mockOverview })
Expand Down Expand Up @@ -615,4 +641,30 @@ describe('FileListTable', () => {
})
})
})

describe('testing pagination', () => {
it('displays the first page', async () => {
setup({})
render(<FileListTable />, { wrapper: wrapper() })
const loading = await screen.findByText('Loading')
mockIsIntersecting(loading, false)

const page1File1 = await screen.findByText('file1.js')
expect(page1File1).toBeInTheDocument()

const page1File2 = await screen.findByText('file2.js')
expect(page1File2).toBeInTheDocument()
})

it('displays the second page', async () => {
setup({})
render(<FileListTable />, { wrapper: wrapper() })

const loading = await screen.findByText('Loading')
mockIsIntersecting(loading, true)

const page2File1 = await screen.findByText('file3.js')
expect(page2File1).toBeInTheDocument()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ export function useRepoBranchContentsTable(sortItem?: {
},
})

const indicationRange = branchData?.pages?.[0]?.indicationRange

const { treePaths } = useTreePaths()

const finalizedTableRows = useMemo(() => {
Expand All @@ -129,6 +127,11 @@ export function useRepoBranchContentsTable(sortItem?: {
return []
}

const indicationRange = branchData?.pages?.[0]?.indicationRange ?? {
lowerRange: 0,
upperRange: 100,
}

const rawTableRows = tableData.map((result) => {
if (!result) return null
let name
Expand Down Expand Up @@ -161,8 +164,8 @@ export function useRepoBranchContentsTable(sortItem?: {
amount={result.percentCovered}
color={determineProgressColor({
coverage: result.percentCovered,
lowerRange: indicationRange?.lowerRange || 0,
upperRange: indicationRange?.upperRange || 100,
lowerRange: indicationRange.lowerRange,
upperRange: indicationRange.upperRange,
})}
/>
)
Expand All @@ -177,34 +180,21 @@ export function useRepoBranchContentsTable(sortItem?: {
}
})

const adjustedTableData = adjustListIfUpDir({
return adjustListIfUpDir({
treePaths,
displayType: selectedDisplayType,
rawTableRows: rawTableRows as Row[],
})

return adjustedTableData
}, [
branchData?.pages,
branch,
indicationRange,
treePaths,
urlPath,
selectedDisplayType,
])
}, [branchData?.pages, branch, treePaths, urlPath, selectedDisplayType])

return {
data: finalizedTableRows ?? [],
indicationRange: branchData?.pages?.[0]?.indicationRange,
// useLocationParams needs to be updated to have full types
// @ts-expect-error
hasFlagsSelected: params?.flags ? params?.flags?.length > 0 : false,
hasFlagsSelected: params?.flags?.length > 0,
// @ts-expect-error
hasComponentsSelected: params?.components
? // useLocationParams needs to be updated to have full types
// @ts-expect-error
params?.components?.length > 0
: false,
hasComponentsSelected: params?.components?.length > 0,
isLoading,
fetchNextPage,
hasNextPage,
Expand Down
23 changes: 12 additions & 11 deletions src/services/pathContents/branch/dir/usePrefetchBranchDirEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,21 @@ export function usePrefetchBranchDirEntry({
} satisfies NetworkErrorObject)
}

let results
const pathContentsType =
let results = null
const pathContent =
data?.owner?.repository?.branch?.head?.deprecatedPathContents
?.__typename
if (pathContentsType === 'PathContentConnection') {

if (
pathContent &&
pathContent?.__typename === 'PathContentConnection'
) {
results = mapEdges({
edges:
data?.owner?.repository?.branch?.head?.deprecatedPathContents
?.edges ?? [],
edges: pathContent?.edges,
})

return {
results: results ?? null,
pathContentsType,
results,
pathContentsType: pathContent.__typename,
indicationRange:
data?.owner?.repository?.repositoryConfig?.indicationRange,
__typename:
Expand All @@ -212,8 +213,8 @@ export function usePrefetchBranchDirEntry({
}

return {
results: results ?? null,
pathContentsType,
results,
pathContentsType: pathContent?.__typename,
indicationRange:
data?.owner?.repository?.repositoryConfig?.indicationRange,
__typename: res?.data?.owner?.repository?.branch?.head?.__typename,
Expand Down
Loading

0 comments on commit 16a52e2

Please sign in to comment.