Skip to content
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 login nav link to append to redirect #3799

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/services/navigation/useNavLinks.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { renderHook } from '@testing-library/react'
import qs from 'qs'
import { PropsWithChildren } from 'react'
import { MemoryRouter, Route } from 'react-router-dom'

import config from 'config'

import { useNavLinks } from './useNavLinks'

vi.mock('config')

// ensuring that we reset the config after each test
afterEach(() => {
config.IS_SELF_HOSTED = false
})

const wrapper =
(location: string): React.FC<PropsWithChildren> =>
({ children }) => (
<MemoryRouter initialEntries={[location]} initialIndex={0}>
<Route path="/login">{children}</Route>
<Route path="/:provider">{children}</Route>
<Route path="/:provider/:owner">{children}</Route>
<Route path="/:provider/:owner/:repo">{children}</Route>
Expand Down Expand Up @@ -91,6 +102,74 @@ describe('useNavLinks', () => {
})
})

describe('login', () => {
describe('config is not set to self-hosted', () => {
beforeEach(() => {
config.IS_SELF_HOSTED = false
})

it('returns the correct link with nothing passed', () => {
const { result } = renderHook(() => useNavLinks(), {
wrapper: wrapper('/gl/doggo/squirrel-locator'),
})

const path = result.current.login.path()
expect(path).toBe('/login')
})

describe('to parameter is passed', () => {
it('appends the param to the login url', () => {
const { result } = renderHook(() => useNavLinks(), {
wrapper: wrapper('/gl/doggo/squirrel-locator'),
})

const path = result.current.login.path({
to: '/gh/codecov/gazebo',
})

const query = qs.stringify(
{ to: '/gh/codecov/gazebo' },
{ addQueryPrefix: true }
)
expect(path).toBe(`/login${query}`)
})
})
})

describe('config is set to self-hosted', () => {
beforeEach(() => {
config.IS_SELF_HOSTED = true
})

it('returns the correct link with nothing passed', () => {
const { result } = renderHook(() => useNavLinks(), {
wrapper: wrapper('/gl/doggo/squirrel-locator'),
})

const path = result.current.login.path()
expect(path).toBe('/')
})

describe('to parameter is passed', () => {
it('appends the param to the login url', () => {
const { result } = renderHook(() => useNavLinks(), {
wrapper: wrapper('/gl/doggo/squirrel-locator'),
})

const path = result.current.login.path({
to: '/gh/codecov/gazebo',
})

const query = qs.stringify(
{ to: '/gh/codecov/gazebo' },
{ addQueryPrefix: true }
)
expect(path).toBe(`/${query}`)
})
})
})
})

describe('owner link', () => {
it('returns the correct link with nothing passed', () => {
const { result } = renderHook(() => useNavLinks(), {
Expand Down
14 changes: 14 additions & 0 deletions src/services/navigation/useNavLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ export function useNavLinks() {
},
isExternalLink: true,
},
login: {
text: 'Login',
path: ({ to }: { to?: string } = {}) => {
const query = qs.stringify({ to }, { addQueryPrefix: true })

// Enterprise login page is at different url than Cloud; see App.tsx
if (config.IS_SELF_HOSTED) {
return `/${query}`
}

return `/login${query}`
},
isExternalLink: false,
},
signUp: {
text: 'Sign Up',
path: () => `${config.MARKETING_BASE_URL}/sign-up/`,
Expand Down
1 change: 0 additions & 1 deletion src/services/navigation/useStaticNavLinks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ describe('useStaticNavLinks', () => {
${links.bundleConfigFeedback} | ${'https://github.com/codecov/feedback/issues/270'}
${links.quickStart} | ${'https://docs.codecov.com/docs/quick-start'}
${links.installSelfHosted} | ${'https://docs.codecov.com/docs/installing-codecov-self-hosted'}
${links.login} | ${`/login`}
${links.testsAnalytics} | ${'https://docs.codecov.com/docs/test-analytics#failed-test-reporting'}
${links.testsAnalyticsDataRetention} | ${'https://docs.codecov.com/docs/test-analytics#data-retention'}
${links.expiredReports} | ${'https://docs.codecov.com/docs/codecov-yaml#section-expired-reports'}
Expand Down
12 changes: 0 additions & 12 deletions src/services/navigation/useStaticNavLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,18 +447,6 @@ export function useStaticNavLinks() {
isExternalLink: true,
openNewTab: true,
},
login: {
text: 'Login',
path: () => {
// Enterprise login page is at different url than Cloud; see App.tsx
if (config.IS_SELF_HOSTED) {
return '/'
}

return `/login`
},
isExternalLink: false,
},
testsAnalytics: {
text: 'Tests Analytics',
path: () =>
Expand Down