|
1 | 1 | import { renderHook } from '@testing-library/react'
|
| 2 | +import qs from 'qs' |
2 | 3 | import { PropsWithChildren } from 'react'
|
3 | 4 | import { MemoryRouter, Route } from 'react-router-dom'
|
4 | 5 |
|
| 6 | +import config from 'config' |
| 7 | + |
5 | 8 | import { useNavLinks } from './useNavLinks'
|
6 | 9 |
|
| 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 | + |
7 | 17 | const wrapper =
|
8 | 18 | (location: string): React.FC<PropsWithChildren> =>
|
9 | 19 | ({ children }) => (
|
10 | 20 | <MemoryRouter initialEntries={[location]} initialIndex={0}>
|
| 21 | + <Route path="/login">{children}</Route> |
11 | 22 | <Route path="/:provider">{children}</Route>
|
12 | 23 | <Route path="/:provider/:owner">{children}</Route>
|
13 | 24 | <Route path="/:provider/:owner/:repo">{children}</Route>
|
@@ -91,6 +102,74 @@ describe('useNavLinks', () => {
|
91 | 102 | })
|
92 | 103 | })
|
93 | 104 |
|
| 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 | + |
94 | 173 | describe('owner link', () => {
|
95 | 174 | it('returns the correct link with nothing passed', () => {
|
96 | 175 | const { result } = renderHook(() => useNavLinks(), {
|
|
0 commit comments