|
| 1 | +import { getNavigationStateFromDeeplinkPath } from 'app/utils/deeplink/getNavigationStateFromDeeplinkPath' |
| 2 | + |
| 3 | +const stubGetStateFromPath = (path: string) => |
| 4 | + path.startsWith('/track/') |
| 5 | + ? { routes: [{ name: 'Track' }] } |
| 6 | + : path.startsWith('/profile') |
| 7 | + ? { routes: [{ name: 'UserProfile' }] } |
| 8 | + : path.includes('/collection/') |
| 9 | + ? { routes: [{ name: 'Collection' }] } |
| 10 | + : { routes: [{ name: path }] } |
| 11 | + |
| 12 | +const getLeafRouteName = (state: any): string | undefined => { |
| 13 | + let current: any = state |
| 14 | + while (current?.routes?.length) { |
| 15 | + current = current.routes[current.index ?? 0] |
| 16 | + if (current?.state) current = current.state |
| 17 | + } |
| 18 | + return current?.name |
| 19 | +} |
| 20 | + |
| 21 | +describe('getNavigationStateFromDeeplinkPath', () => { |
| 22 | + test('routes /users/:id to Profile', () => { |
| 23 | + const state = getNavigationStateFromDeeplinkPath({ |
| 24 | + path: '/users/Nz9yBb4', |
| 25 | + options: undefined, |
| 26 | + hasAccount: true, |
| 27 | + accountHandle: 'someone', |
| 28 | + routeName: '/trending', |
| 29 | + getStateFromPath: stubGetStateFromPath as any |
| 30 | + }) |
| 31 | + |
| 32 | + expect(getLeafRouteName(state)).toBe('Profile') |
| 33 | + }) |
| 34 | + |
| 35 | + test('routes /playlists/:id to Collection', () => { |
| 36 | + const state = getNavigationStateFromDeeplinkPath({ |
| 37 | + path: '/playlists/Nz9yBb4', |
| 38 | + options: undefined, |
| 39 | + hasAccount: true, |
| 40 | + accountHandle: 'someone', |
| 41 | + routeName: '/trending', |
| 42 | + getStateFromPath: stubGetStateFromPath as any |
| 43 | + }) |
| 44 | + |
| 45 | + expect(getLeafRouteName(state)).toBe('Collection') |
| 46 | + }) |
| 47 | + |
| 48 | + test('does not rewrite current user playlist permalink to /profile', () => { |
| 49 | + const state = getNavigationStateFromDeeplinkPath({ |
| 50 | + path: '/Audius/playlist/140', |
| 51 | + options: undefined, |
| 52 | + hasAccount: true, |
| 53 | + accountHandle: 'Audius', |
| 54 | + routeName: '/trending', |
| 55 | + getStateFromPath: stubGetStateFromPath as any |
| 56 | + }) |
| 57 | + |
| 58 | + expect(getLeafRouteName(state)).toBe('Collection') |
| 59 | + }) |
| 60 | +}) |
0 commit comments