Skip to content

Commit

Permalink
chore: Update services/pathContents to Vitest (#3302)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov authored Oct 1, 2024
1 parent 5eeab63 commit 9d3c84b
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'

import { usePrefetchBranchDirEntry } from './usePrefetchBranchDirEntry'
Expand Down Expand Up @@ -151,20 +151,20 @@ describe('usePrefetchBranchDirEntry', () => {
isUnsuccessfulParseError = false,
}: SetupArgs) {
server.use(
graphql.query('BranchContents', (req, res, ctx) => {
graphql.query('BranchContents', (info) => {
if (isMissingCoverage) {
return res(ctx.status(200), ctx.data(mockDataMissingCoverage))
return HttpResponse.json({ data: mockDataMissingCoverage })
} else if (isUnknownPath) {
return res(ctx.status(200), ctx.data(mockDataUnknownPath))
return HttpResponse.json({ data: mockDataUnknownPath })
} else if (isRepositoryNotFoundError) {
return res(ctx.status(200), ctx.data(mockDataRepositoryNotFound))
return HttpResponse.json({ data: mockDataRepositoryNotFound })
} else if (isOwnerNotActivatedError) {
return res(ctx.status(200), ctx.data(mockDataOwnerNotActivated))
return HttpResponse.json({ data: mockDataOwnerNotActivated })
} else if (isUnsuccessfulParseError) {
return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError))
return HttpResponse.json({ data: mockUnsuccessfulParseError })
}

return res(ctx.status(200), ctx.data(mockData))
return HttpResponse.json({ data: mockData })
})
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'

import { useRepoBranchContents } from './useRepoBranchContents'
Expand Down Expand Up @@ -150,20 +150,20 @@ describe('useRepoBranchContents', () => {
isUnsuccessfulParseError = false,
}: SetupArgs) {
server.use(
graphql.query('BranchContents', (req, res, ctx) => {
graphql.query('BranchContents', (info) => {
if (isMissingCoverage) {
return res(ctx.status(200), ctx.data(mockDataMissingCoverage))
return HttpResponse.json({ data: mockDataMissingCoverage })
} else if (isUnknownPath) {
return res(ctx.status(200), ctx.data(mockDataUnknownPath))
return HttpResponse.json({ data: mockDataUnknownPath })
} else if (isRepositoryNotFoundError) {
return res(ctx.status(200), ctx.data(mockDataRepositoryNotFound))
return HttpResponse.json({ data: mockDataRepositoryNotFound })
} else if (isOwnerNotActivatedError) {
return res(ctx.status(200), ctx.data(mockDataOwnerNotActivated))
return HttpResponse.json({ data: mockDataOwnerNotActivated })
} else if (isUnsuccessfulParseError) {
return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError))
return HttpResponse.json({ data: mockUnsuccessfulParseError })
}

return res(ctx.status(200), ctx.data(mockData))
return HttpResponse.json({ data: mockData })
})
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'

import { usePrefetchBranchFileEntry } from './usePrefetchBranchFileEntry'
Expand Down Expand Up @@ -90,9 +90,9 @@ const mockData = {
describe('usePrefetchBranchFileEntry', () => {
function setup() {
server.use(
graphql.query('CoverageForFile', (req, res, ctx) =>
res(ctx.status(200), ctx.data(mockData))
)
graphql.query('CoverageForFile', (info) => {
return HttpResponse.json({ data: mockData })
})
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'

import { usePrefetchCommitDirEntry } from './usePrefetchCommitDirEntry'
Expand Down Expand Up @@ -141,23 +141,19 @@ describe('usePrefetchCommitDirEntry', () => {
isUnsuccessfulParse = false,
}: SetupArgs) {
server.use(
graphql.query('CommitPathContents', (req, res, ctx) => {
graphql.query('CommitPathContents', (info) => {
if (isMissingCoverage) {
return res(ctx.status(200), ctx.data(mockDataMissingCoverage))
return HttpResponse.json({ data: mockDataMissingCoverage })
} else if (isUnknownPath) {
return HttpResponse.json({ data: mockDataUnknownPath })
} else if (isNotFoundError) {
return HttpResponse.json({ data: mockNotFoundError })
} else if (isOwnerNotActivatedError) {
return HttpResponse.json({ data: mockOwnerNotActivatedError })
} else if (isUnsuccessfulParse) {
return HttpResponse.json({ data: mockUnsuccessfulParseError })
}
if (isUnknownPath) {
return res(ctx.status(200), ctx.data(mockDataUnknownPath))
}
if (isNotFoundError) {
return res(ctx.status(200), ctx.data(mockNotFoundError))
}
if (isOwnerNotActivatedError) {
return res(ctx.status(200), ctx.data(mockOwnerNotActivatedError))
}
if (isUnsuccessfulParse) {
return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError))
}
return res(ctx.status(200), ctx.data(mockData))
return HttpResponse.json({ data: mockData })
})
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'

import { useRepoCommitContents } from './useRepoCommitContents'
Expand Down Expand Up @@ -144,19 +144,19 @@ describe('useRepoCommitContents', () => {
isUnsuccessfulParseError = false,
}: SetupArgs) {
server.use(
graphql.query('CommitPathContents', (req, res, ctx) => {
graphql.query('CommitPathContents', (info) => {
if (isMissingCoverage) {
return res(ctx.status(200), ctx.data(mockDataMissingCoverage))
return HttpResponse.json({ data: mockDataMissingCoverage })
} else if (isUnknownPath) {
return res(ctx.status(200), ctx.data(mockDataUnknownPath))
return HttpResponse.json({ data: mockDataUnknownPath })
} else if (isNotFoundError) {
return res(ctx.status(200), ctx.data(mockNotFoundError))
return HttpResponse.json({ data: mockNotFoundError })
} else if (isOwnerNotActivatedError) {
return res(ctx.status(200), ctx.data(mockOwnerNotActivatedError))
return HttpResponse.json({ data: mockOwnerNotActivatedError })
} else if (isUnsuccessfulParseError) {
return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError))
return HttpResponse.json({ data: mockUnsuccessfulParseError })
}
return res(ctx.status(200), ctx.data(mockData))
return HttpResponse.json({ data: mockData })
})
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'
import { type MockInstance } from 'vitest'

import { usePrefetchCommitFileEntry } from './usePrefetchCommitFileEntry'

Expand Down Expand Up @@ -116,19 +117,19 @@ describe('usePrefetchCommitFileEntry', () => {
const mockVars = jest.fn()

server.use(
graphql.query('CoverageForFile', (req, res, ctx) => {
mockVars(req.variables)
graphql.query('CoverageForFile', (info) => {
mockVars(info.variables)

if (isNotFoundError) {
return res(ctx.status(200), ctx.data(mockNotFoundError))
return HttpResponse.json({ data: mockNotFoundError })
} else if (isOwnerNotActivatedError) {
return res(ctx.status(200), ctx.data(mockOwnerNotActivatedError))
return HttpResponse.json({ data: mockOwnerNotActivatedError })
} else if (isUnsuccessfulParseError) {
return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError))
return HttpResponse.json({ data: mockUnsuccessfulParseError })
} else if (isNullOwner) {
return res(ctx.status(200), ctx.data(mockNullOwner))
return HttpResponse.json({ data: mockNullOwner })
} else {
return res(ctx.status(200), ctx.data(mockData))
return HttpResponse.json({ data: mockData })
}
})
)
Expand Down Expand Up @@ -277,12 +278,13 @@ describe('usePrefetchCommitFileEntry', () => {
})

describe('returns NotFoundError __typename', () => {
let consoleSpy: MockInstance
beforeEach(() => {
jest.spyOn(console, 'error')
consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
jest.restoreAllMocks()
consoleSpy.mockRestore()
})

it('throws a 404', async () => {
Expand Down Expand Up @@ -314,12 +316,13 @@ describe('usePrefetchCommitFileEntry', () => {
})

describe('returns OwnerNotActivatedError __typename', () => {
let consoleSpy: MockInstance
beforeEach(() => {
jest.spyOn(console, 'error')
consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
jest.restoreAllMocks()
consoleSpy.mockRestore()
})

it('throws a 403', async () => {
Expand Down Expand Up @@ -351,12 +354,13 @@ describe('usePrefetchCommitFileEntry', () => {
})

describe('unsuccessful parse of zod schema', () => {
let consoleSpy: MockInstance
beforeEach(() => {
jest.spyOn(console, 'error')
consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
jest.restoreAllMocks()
consoleSpy.mockRestore()
})

it('throws a 404', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'

import { usePrefetchPullDirEntry } from './usePrefetchPullDirEntry'
Expand Down Expand Up @@ -95,17 +95,15 @@ describe('usePrefetchPullDirEntry', () => {
ownerNotActivated = false,
}) {
server.use(
graphql.query('PullPathContents', (req, res, ctx) => {
graphql.query('PullPathContents', (info) => {
if (invalidSchema) {
return res(ctx.status(200), ctx.data({}))
return HttpResponse.json({ data: {} })
} else if (repositoryNotFound) {
return HttpResponse.json({ data: mockDataRepositoryNotFound })
} else if (ownerNotActivated) {
return HttpResponse.json({ data: mockDataOwnerNotActivated })
}
if (repositoryNotFound) {
return res(ctx.status(200), ctx.data(mockDataRepositoryNotFound))
}
if (ownerNotActivated) {
return res(ctx.status(200), ctx.data(mockDataOwnerNotActivated))
}
return res(ctx.status(200), ctx.data(mockData))
return HttpResponse.json({ data: mockData })
})
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'
import { type MockInstance } from 'vitest'

import { useRepoPullContents } from './useRepoPullContents'

Expand Down Expand Up @@ -140,23 +141,19 @@ describe('useRepoPullContents', () => {
isUnknownPath = false,
}) {
server.use(
graphql.query('PullPathContents', (req, res, ctx) => {
graphql.query('PullPathContents', (info) => {
if (invalidSchema) {
return res(ctx.status(200), ctx.data({}))
return HttpResponse.json({ data: {} })
} else if (repositoryNotFound) {
return HttpResponse.json({ data: mockDataRepositoryNotFound })
} else if (ownerNotActivated) {
return HttpResponse.json({ data: mockDataOwnerNotActivated })
} else if (isMissingCoverage) {
return HttpResponse.json({ data: mockDataMissingCoverage })
} else if (isUnknownPath) {
return HttpResponse.json({ data: mockDataUnknownPath })
}
if (repositoryNotFound) {
return res(ctx.status(200), ctx.data(mockDataRepositoryNotFound))
}
if (ownerNotActivated) {
return res(ctx.status(200), ctx.data(mockDataOwnerNotActivated))
}
if (isMissingCoverage) {
return res(ctx.status(200), ctx.data(mockDataMissingCoverage))
}
if (isUnknownPath) {
return res(ctx.status(200), ctx.data(mockDataUnknownPath))
}
return res(ctx.status(200), ctx.data(mockData))
return HttpResponse.json({ data: mockData })
})
)
}
Expand Down Expand Up @@ -272,6 +269,15 @@ describe('useRepoPullContents', () => {
})

describe('on invalid schema', () => {
let consoleSpy: MockInstance
beforeEach(() => {
consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
consoleSpy.mockRestore()
})

it('returns 404', async () => {
setup({ invalidSchema: true })
const { result } = renderHook(
Expand Down Expand Up @@ -300,6 +306,15 @@ describe('useRepoPullContents', () => {
})

describe('on repository not found', () => {
let consoleSpy: MockInstance
beforeEach(() => {
consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
consoleSpy.mockRestore()
})

it('returns 404', async () => {
setup({ repositoryNotFound: true })
const { result } = renderHook(
Expand Down Expand Up @@ -328,6 +343,15 @@ describe('useRepoPullContents', () => {
})

describe('on owner not activated', () => {
let consoleSpy: MockInstance
beforeEach(() => {
consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
consoleSpy.mockRestore()
})

it('returns 403', async () => {
setup({ ownerNotActivated: true })
const { result } = renderHook(
Expand Down
Loading

0 comments on commit 9d3c84b

Please sign in to comment.