Skip to content

Commit

Permalink
Update Repo queries to use GQL (#2943)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitvinnakota-codecov committed Jul 5, 2024
1 parent 56cb7d3 commit 4a04934
Show file tree
Hide file tree
Showing 19 changed files with 587 additions and 360 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import PropTypes from 'prop-types'
import { useState } from 'react'

import { useEraseRepoContent } from 'services/repo'
import Button from 'ui/Button'

import EraseRepoContentModal from './EraseRepoContentModal'
import useEraseContent from './useEraseContent'

function EraseRepoButton({ isLoading, setShowModal }) {
if (isLoading) {
Expand Down Expand Up @@ -33,7 +33,7 @@ EraseRepoButton.propTypes = {

function EraseRepoContent() {
const [showModal, setShowModal] = useState(false)
const { eraseRepoContent, isLoading } = useEraseContent()
const { mutate: eraseRepoContent, isLoading } = useEraseRepoContent()

return (
<div className="flex flex-col sm:flex-row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, screen, waitFor } from 'custom-testing-library'

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import userEvent from '@testing-library/user-event'
import { rest } from 'msw'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { MemoryRouter, Route } from 'react-router-dom'

Expand Down Expand Up @@ -37,9 +37,34 @@ const wrapper = ({ children }) => (
</MemoryRouter>
</QueryClientProvider>
)

const mockErrorResponse = {
eraseRepository: {
error: {
__typename: 'ValidationError',
message: 'error',
},
},
}

const mockUnauthorized = {
eraseRepository: {
error: {
__typename: 'UnauthorizedError',
message: 'UnauthorizedError error',
},
},
}

const mockResponse = {
eraseRepository: {
data: null,
},
}

describe('EraseRepoContent', () => {
function setup(
{ failedMutation = false, isLoading = false } = {
{ failedMutation = false, isLoading = false, unauthorized = false } = {
failedMutation: false,
isLoading: false,
}
Expand All @@ -50,22 +75,20 @@ describe('EraseRepoContent', () => {
useAddNotification.mockReturnValue(addNotification)

server.use(
rest.patch(
'/internal/github/codecov/repos/codecov-client/erase/',
(req, res, ctx) => {
mutate()

if (isLoading) {
// https://cathalmacdonnacha.com/mocking-error-empty-and-loading-states-with-msw
return res(ctx.status(200), ctx.json({}), ctx.delay(100))
}

if (failedMutation) {
return res(ctx.status(500))
}
return res(ctx.status(200))
graphql.mutation('EraseRepository', (req, res, ctx) => {
mutate()
if (isLoading) {
// https://cathalmacdonnacha.com/mocking-error-empty-and-loading-states-with-msw
return res(ctx.status(200), ctx.data(mockResponse), ctx.delay(100))
}
)
if (failedMutation) {
return res(ctx.status(200), ctx.data(mockErrorResponse))
}
if (unauthorized) {
return res(ctx.status(200), ctx.data(mockUnauthorized))
}
return res(ctx.status(200), ctx.data(mockResponse))
})
)

return { user, mutate, addNotification }
Expand Down Expand Up @@ -268,4 +291,29 @@ describe('EraseRepoContent', () => {
)
})
})

describe('when user is unauthorized', () => {
it('adds an error notification', async () => {
const { user, mutate, addNotification } = setup({ unauthorized: true })
render(<EraseRepoContent />, { wrapper })

const eraseButton = await screen.findByRole('button', {
name: /Erase Content/,
})
await user.click(eraseButton)

const modalEraseButton = await screen.findByRole('button', {
name: /Erase Content/,
})
await user.click(modalEraseButton)

await waitFor(() => expect(mutate).toHaveBeenCalled())
await waitFor(() =>
expect(addNotification).toHaveBeenCalledWith({
type: 'error',
text: "We were unable to erase this repo's content",
})
)
})
})
})

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import PropTypes from 'prop-types'
import { useState } from 'react'

import { useRegenerateRepoUploadToken } from 'services/repoUploadToken'
import { useAddNotification } from 'services/toastNotification'
import A from 'ui/A'
import Button from 'ui/Button'
import TokenWrapper from 'ui/TokenWrapper'
Expand All @@ -15,25 +14,13 @@ const TokenFormatEnum = Object.freeze({
})

function useRegenerateToken() {
const addToast = useAddNotification()
const { mutate, ...rest } = useRegenerateRepoUploadToken()

async function regenerateToken() {
mutate(null, {
onError: () =>
addToast({
type: 'error',
text: 'Something went wrong',
}),
})
}

return { regenerateToken, ...rest }
const { mutate, isLoading } = useRegenerateRepoUploadToken()
return { mutate, isLoading }
}

function RepoUploadToken({ uploadToken }) {
const [showModal, setShowModal] = useState(false)
const { regenerateToken, isLoading } = useRegenerateToken()
const { mutate, isLoading } = useRegenerateToken()

if (!uploadToken) {
return null
Expand Down Expand Up @@ -69,7 +56,7 @@ function RepoUploadToken({ uploadToken }) {
<RegenerateTokenModal
showModal={showModal}
closeModal={() => setShowModal(false)}
regenerateToken={regenerateToken}
regenerateToken={mutate}
isLoading={isLoading}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, screen, waitFor } from 'custom-testing-library'

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import userEvent from '@testing-library/user-event'
import { rest } from 'msw'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { MemoryRouter, Route } from 'react-router-dom'

Expand Down Expand Up @@ -54,22 +54,30 @@ describe('RepoUploadToken', () => {
useAddNotification.mockReturnValue(addNotification)

server.use(
rest.patch(
`/internal/github/codecov/repos/codecov-client/regenerate-upload-token/`,
(req, res, ctx) => {
mutate(req)
if (triggerError) {
return res(ctx.status(500))
} else {
return res(
ctx.status(200),
ctx.json({
data: { uploadToken },
})
)
}
graphql.mutation('RegenerateRepositoryUploadToken', (req, res, ctx) => {
mutate(req.variables)
if (triggerError) {
return res(
ctx.status(200),
ctx.data({
regenerateRepositoryUploadToken: {
error: {
__typename: 'ValidationError',
},
},
})
)
}
)

return res(
ctx.status(200),
ctx.data({
regenerateRepositoryUploadToken: {
value: 'test',
},
})
)
})
)

return { mutate, addNotification, user }
Expand Down Expand Up @@ -226,6 +234,7 @@ describe('RepoUploadToken', () => {
expect(addNotification).toHaveBeenCalledWith({
type: 'error',
text: 'Something went wrong',
disappearAfter: 10000,
})
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import GenerateSecretStringModal from './GenerateSecretStringModal'
import useGenerateSecretString from './useGenerateSecretSring'

function SecretString() {
const { generateSecretString, data, isLoading } = useGenerateSecretString()
const {
generateSecretString,
data: generatedSecretString,
isLoading,
} = useGenerateSecretString()

const [showGenerateModal, setShowGenerateModal] = useState(false)
const [showCopyModal, setShowCopyModal] = useState(false)

const value = data?.value
const value = generatedSecretString?.data?.encodeSecretString?.value

return (
<SettingsDescriptor
Expand Down
Loading

0 comments on commit 4a04934

Please sign in to comment.