-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Update NotFoundError to have login button #3804
Merged
nicholas-codecov
merged 4 commits into
main
from
gh-eng-3453-update-not-found-error-to-have-login-button
Mar 11, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
200e4c2
update NetworkErrorBoundary to conditionally render login/home button…
nicholas-codecov b318de3
update tests
nicholas-codecov 337080c
remove home button as it's not in scope
nicholas-codecov 008a93c
Merge branch 'main' into gh-eng-3453-update-not-found-error-to-have-l…
nicholas-codecov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { render, screen, waitFor } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import { Component, useState } from 'react' | ||
import { MemoryRouter, useHistory } from 'react-router-dom' | ||
import { graphql, HttpResponse } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
import qs from 'qs' | ||
import { Component, Suspense, useState } from 'react' | ||
import { MemoryRouter, Route, useHistory } from 'react-router-dom' | ||
import { vi } from 'vitest' | ||
|
||
import config from 'config' | ||
|
@@ -13,15 +16,67 @@ import NetworkErrorBoundary from './NetworkErrorBoundary' | |
vi.spyOn(console, 'error').mockImplementation(() => undefined) | ||
vi.mock('config') | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { queries: { retry: false } }, | ||
const mockUser = { | ||
me: { | ||
owner: { | ||
defaultOrgUsername: 'codecov', | ||
}, | ||
email: '[email protected]', | ||
privateAccess: true, | ||
onboardingCompleted: true, | ||
businessEmail: '[email protected]', | ||
termsAgreement: true, | ||
user: { | ||
name: 'Jane Doe', | ||
username: 'janedoe', | ||
avatarUrl: 'http://127.0.0.1/avatar-url', | ||
avatar: 'http://127.0.0.1/avatar-url', | ||
student: false, | ||
studentCreatedAt: null, | ||
studentUpdatedAt: null, | ||
}, | ||
trackingMetadata: { | ||
service: 'github', | ||
ownerid: 123, | ||
serviceId: '123', | ||
plan: 'users-basic', | ||
staff: false, | ||
hasYaml: false, | ||
bot: null, | ||
delinquent: null, | ||
didTrial: null, | ||
planProvider: null, | ||
planUserCount: 1, | ||
createdAt: 'timestamp', | ||
updatedAt: 'timestamp', | ||
profile: { | ||
createdAt: 'timestamp', | ||
otherGoal: null, | ||
typeProjects: [], | ||
goals: [], | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const nullUser = { | ||
me: null, | ||
} | ||
|
||
const server = setupServer() | ||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
queryClient.clear() | ||
server.resetHandlers() | ||
vi.clearAllMocks() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
class TestErrorBoundary extends Component { | ||
constructor(props) { | ||
super(props) | ||
|
@@ -48,9 +103,7 @@ function ErrorComponent({ status, detail, typename, dev, error }) { | |
// eslint-disable-next-line no-throw-literal | ||
throw { | ||
status, | ||
data: { | ||
detail, | ||
}, | ||
data: { detail }, | ||
dev, | ||
error, | ||
__typename: typename, | ||
|
@@ -105,16 +158,37 @@ function App({ status, detail, typename, dev, error }) { | |
|
||
const wrapper = | ||
(initialEntries = ['/gh/codecov', '/gh']) => | ||
({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
<MemoryRouter initialEntries={initialEntries}>{children}</MemoryRouter> | ||
</QueryClientProvider> | ||
) | ||
({ children }) => { | ||
const queryClient = new QueryClient({ | ||
defaultOptions: { queries: { retry: false, suspense: true } }, | ||
}) | ||
|
||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<MemoryRouter initialEntries={initialEntries}> | ||
<Route path="/:provider"> | ||
<Suspense fallback={<div>Loading</div>}>{children}</Suspense> | ||
</Route> | ||
</MemoryRouter> | ||
</QueryClientProvider> | ||
) | ||
} | ||
|
||
describe('NetworkErrorBoundary', () => { | ||
function setup({ isSelfHosted = false } = { isSelfHosted: false }) { | ||
function setup( | ||
{ isSelfHosted = false, user = nullUser } = { | ||
isSelfHosted: false, | ||
user: nullUser, | ||
} | ||
) { | ||
config.IS_SELF_HOSTED = isSelfHosted | ||
|
||
server.use( | ||
graphql.query('CurrentUser', () => { | ||
return HttpResponse.json({ data: user }) | ||
}) | ||
) | ||
|
||
return { user: userEvent.setup() } | ||
} | ||
|
||
|
@@ -310,6 +384,41 @@ describe('NetworkErrorBoundary', () => { | |
const button = await screen.findByText('Return to previous page') | ||
expect(button).toBeInTheDocument() | ||
}) | ||
|
||
describe('user is not logged in', () => { | ||
it('renders a login button', async () => { | ||
const { user } = setup({ user: nullUser }) | ||
render(<App status={404} />, { | ||
wrapper: wrapper(), | ||
}) | ||
|
||
const textBox = await screen.findByRole('textbox') | ||
await user.type(textBox, 'fail') | ||
|
||
const queryString = qs.stringify({ to: '/gh/codecov' }) | ||
const loginButton = await screen.findByText(/Login/) | ||
expect(loginButton).toBeInTheDocument() | ||
expect(loginButton).toHaveAttribute('href', `/login?${queryString}`) | ||
}) | ||
}) | ||
|
||
describe('user is logged in', () => { | ||
it('does not render a login button', async () => { | ||
const { user } = setup({ user: mockUser }) | ||
render(<App status={404} />, { | ||
wrapper: wrapper(), | ||
}) | ||
|
||
const textBox = await screen.findByRole('textbox') | ||
await user.type(textBox, 'fail') | ||
|
||
const notFound = await screen.findByText(/Not found/) | ||
expect(notFound).toBeInTheDocument() | ||
|
||
const loginButton = screen.queryByText(/Login/) | ||
expect(loginButton).not.toBeInTheDocument() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when running in self hosted mode', () => { | ||
|
@@ -322,7 +431,7 @@ describe('NetworkErrorBoundary', () => { | |
const textBox = await screen.findByRole('textbox') | ||
await user.type(textBox, 'fail') | ||
|
||
const pleaseSee = await screen.findByText(/Please see/) | ||
const pleaseSee = await screen.findByText(/please see/) | ||
expect(pleaseSee).toBeInTheDocument() | ||
}) | ||
|
||
|
@@ -338,6 +447,41 @@ describe('NetworkErrorBoundary', () => { | |
const button = await screen.findByText('Return to previous page') | ||
expect(button).toBeInTheDocument() | ||
}) | ||
|
||
describe('user is not logged in', () => { | ||
it('renders a login button', async () => { | ||
const { user } = setup({ user: nullUser, isSelfHosted: true }) | ||
render(<App status={404} />, { | ||
wrapper: wrapper(), | ||
}) | ||
|
||
const textBox = await screen.findByRole('textbox') | ||
await user.type(textBox, 'fail') | ||
|
||
const queryString = qs.stringify({ to: '/gh/codecov' }) | ||
const loginButton = await screen.findByText(/Login/) | ||
expect(loginButton).toBeInTheDocument() | ||
expect(loginButton).toHaveAttribute('href', `/?${queryString}`) | ||
}) | ||
}) | ||
|
||
describe('user is logged in', () => { | ||
it('does not render a login button', async () => { | ||
const { user } = setup({ user: mockUser, isSelfHosted: true }) | ||
render(<App status={404} />, { | ||
wrapper: wrapper(), | ||
}) | ||
|
||
const textBox = await screen.findByRole('textbox') | ||
await user.type(textBox, 'fail') | ||
|
||
const notFound = await screen.findByText(/Not found/) | ||
expect(notFound).toBeInTheDocument() | ||
|
||
const loginButton = screen.queryByText(/Login/) | ||
expect(loginButton).not.toBeInTheDocument() | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
|
@@ -366,7 +510,7 @@ describe('NetworkErrorBoundary', () => { | |
global.fetch = vi.fn(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve({}), | ||
json: () => Promise.resolve({ data: nullUser }), | ||
}) | ||
) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤢