Skip to content

Commit 7c85c05

Browse files
feat: Update login nav link to append to redirect (#3799)
1 parent fa6e9b5 commit 7c85c05

File tree

4 files changed

+93
-13
lines changed

4 files changed

+93
-13
lines changed

src/services/navigation/useNavLinks.test.tsx

+79
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { renderHook } from '@testing-library/react'
2+
import qs from 'qs'
23
import { PropsWithChildren } from 'react'
34
import { MemoryRouter, Route } from 'react-router-dom'
45

6+
import config from 'config'
7+
58
import { useNavLinks } from './useNavLinks'
69

10+
vi.mock('config')
11+
12+
// ensuring that we reset the config after each test
13+
afterEach(() => {
14+
config.IS_SELF_HOSTED = false
15+
})
16+
717
const wrapper =
818
(location: string): React.FC<PropsWithChildren> =>
919
({ children }) => (
1020
<MemoryRouter initialEntries={[location]} initialIndex={0}>
21+
<Route path="/login">{children}</Route>
1122
<Route path="/:provider">{children}</Route>
1223
<Route path="/:provider/:owner">{children}</Route>
1324
<Route path="/:provider/:owner/:repo">{children}</Route>
@@ -91,6 +102,74 @@ describe('useNavLinks', () => {
91102
})
92103
})
93104

105+
describe('login', () => {
106+
describe('config is not set to self-hosted', () => {
107+
beforeEach(() => {
108+
config.IS_SELF_HOSTED = false
109+
})
110+
111+
it('returns the correct link with nothing passed', () => {
112+
const { result } = renderHook(() => useNavLinks(), {
113+
wrapper: wrapper('/gl/doggo/squirrel-locator'),
114+
})
115+
116+
const path = result.current.login.path()
117+
expect(path).toBe('/login')
118+
})
119+
120+
describe('to parameter is passed', () => {
121+
it('appends the param to the login url', () => {
122+
const { result } = renderHook(() => useNavLinks(), {
123+
wrapper: wrapper('/gl/doggo/squirrel-locator'),
124+
})
125+
126+
const path = result.current.login.path({
127+
to: '/gh/codecov/gazebo',
128+
})
129+
130+
const query = qs.stringify(
131+
{ to: '/gh/codecov/gazebo' },
132+
{ addQueryPrefix: true }
133+
)
134+
expect(path).toBe(`/login${query}`)
135+
})
136+
})
137+
})
138+
139+
describe('config is set to self-hosted', () => {
140+
beforeEach(() => {
141+
config.IS_SELF_HOSTED = true
142+
})
143+
144+
it('returns the correct link with nothing passed', () => {
145+
const { result } = renderHook(() => useNavLinks(), {
146+
wrapper: wrapper('/gl/doggo/squirrel-locator'),
147+
})
148+
149+
const path = result.current.login.path()
150+
expect(path).toBe('/')
151+
})
152+
153+
describe('to parameter is passed', () => {
154+
it('appends the param to the login url', () => {
155+
const { result } = renderHook(() => useNavLinks(), {
156+
wrapper: wrapper('/gl/doggo/squirrel-locator'),
157+
})
158+
159+
const path = result.current.login.path({
160+
to: '/gh/codecov/gazebo',
161+
})
162+
163+
const query = qs.stringify(
164+
{ to: '/gh/codecov/gazebo' },
165+
{ addQueryPrefix: true }
166+
)
167+
expect(path).toBe(`/${query}`)
168+
})
169+
})
170+
})
171+
})
172+
94173
describe('owner link', () => {
95174
it('returns the correct link with nothing passed', () => {
96175
const { result } = renderHook(() => useNavLinks(), {

src/services/navigation/useNavLinks.ts

+14
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ export function useNavLinks() {
5353
},
5454
isExternalLink: true,
5555
},
56+
login: {
57+
text: 'Login',
58+
path: ({ to }: { to?: string } = {}) => {
59+
const query = qs.stringify({ to }, { addQueryPrefix: true })
60+
61+
// Enterprise login page is at different url than Cloud; see App.tsx
62+
if (config.IS_SELF_HOSTED) {
63+
return `/${query}`
64+
}
65+
66+
return `/login${query}`
67+
},
68+
isExternalLink: false,
69+
},
5670
signUp: {
5771
text: 'Sign Up',
5872
path: () => `${config.MARKETING_BASE_URL}/sign-up/`,

src/services/navigation/useStaticNavLinks.test.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ describe('useStaticNavLinks', () => {
8888
${links.bundleConfigFeedback} | ${'https://github.com/codecov/feedback/issues/270'}
8989
${links.quickStart} | ${'https://docs.codecov.com/docs/quick-start'}
9090
${links.installSelfHosted} | ${'https://docs.codecov.com/docs/installing-codecov-self-hosted'}
91-
${links.login} | ${`/login`}
9291
${links.testsAnalytics} | ${'https://docs.codecov.com/docs/test-analytics#failed-test-reporting'}
9392
${links.testsAnalyticsDataRetention} | ${'https://docs.codecov.com/docs/test-analytics#data-retention'}
9493
${links.expiredReports} | ${'https://docs.codecov.com/docs/codecov-yaml#section-expired-reports'}

src/services/navigation/useStaticNavLinks.ts

-12
Original file line numberDiff line numberDiff line change
@@ -447,18 +447,6 @@ export function useStaticNavLinks() {
447447
isExternalLink: true,
448448
openNewTab: true,
449449
},
450-
login: {
451-
text: 'Login',
452-
path: () => {
453-
// Enterprise login page is at different url than Cloud; see App.tsx
454-
if (config.IS_SELF_HOSTED) {
455-
return '/'
456-
}
457-
458-
return `/login`
459-
},
460-
isExternalLink: false,
461-
},
462450
testsAnalytics: {
463451
text: 'Tests Analytics',
464452
path: () =>

0 commit comments

Comments
 (0)