-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
138 additions
and
46 deletions.
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
18 changes: 7 additions & 11 deletions
18
src/components/WorldListPage_WorldsForEnsOwnersFeature/NameTabs/NameTabs.tsx
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
5 changes: 0 additions & 5 deletions
5
src/components/WorldListPage_WorldsForEnsOwnersFeature/NameTabs/NameTabs.types.ts
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
59 changes: 59 additions & 0 deletions
59
src/components/WorldListPage_WorldsForEnsOwnersFeature/hooks.spec.ts
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useLocation } from 'react-router' | ||
import { TAB_QUERY_PARAM_KEY, TabType, useCurrentlySelectedTab } from './hooks' | ||
|
||
jest.mock('react-router') | ||
|
||
const mockUseLocation = useLocation as jest.Mock | ||
|
||
describe('when calling the use currently selected tab hook', () => { | ||
let location: Location | ||
|
||
beforeEach(() => { | ||
location = { | ||
pathname: '', | ||
search: '' | ||
} as Location | ||
|
||
mockUseLocation.mockReturnValue(location) | ||
}) | ||
|
||
describe('when the tab query param is not set', () => { | ||
beforeEach(() => { | ||
location.search = '' | ||
}) | ||
|
||
it('should return tab as undefined', () => { | ||
expect(useCurrentlySelectedTab().tab).toBeUndefined() | ||
}) | ||
}) | ||
|
||
describe('when the tab query param is an invalid value', () => { | ||
beforeEach(() => { | ||
location.search = `?${TAB_QUERY_PARAM_KEY}=invalid` | ||
}) | ||
|
||
it('should return tab as undefined', () => { | ||
expect(useCurrentlySelectedTab().tab).toBeUndefined() | ||
}) | ||
}) | ||
|
||
describe('when the tab query param is dcl', () => { | ||
beforeEach(() => { | ||
location.search = `?${TAB_QUERY_PARAM_KEY}=dcl` | ||
}) | ||
|
||
it('should return tab as DCL', () => { | ||
expect(useCurrentlySelectedTab().tab).toBe(TabType.DCL) | ||
}) | ||
}) | ||
|
||
describe('when the tab query param is ens', () => { | ||
beforeEach(() => { | ||
location.search = `?${TAB_QUERY_PARAM_KEY}=ens` | ||
}) | ||
|
||
it('should return tab as ENS', () => { | ||
expect(useCurrentlySelectedTab().tab).toBe(TabType.ENS) | ||
}) | ||
}) | ||
}) |
36 changes: 36 additions & 0 deletions
36
src/components/WorldListPage_WorldsForEnsOwnersFeature/hooks.ts
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useLocation } from 'react-router' | ||
|
||
export const TAB_QUERY_PARAM_KEY = 'tab' | ||
|
||
export enum TabType { | ||
DCL = 'dcl', | ||
ENS = 'ens' | ||
} | ||
|
||
export type UseCurrentlySelectedTabResult = { | ||
tab?: TabType | ||
pathname: string | ||
urlSearchParams: URLSearchParams | ||
} | ||
|
||
export const useCurrentlySelectedTab = (): UseCurrentlySelectedTabResult => { | ||
const location = useLocation() | ||
const urlSearchParams = new URLSearchParams(location.search) | ||
const tab = urlSearchParams.get(TAB_QUERY_PARAM_KEY) | ||
|
||
const result: UseCurrentlySelectedTabResult = { | ||
urlSearchParams, | ||
pathname: location.pathname | ||
} | ||
|
||
switch (tab) { | ||
case 'dcl': | ||
result.tab = TabType.DCL | ||
break | ||
case 'ens': | ||
result.tab = TabType.ENS | ||
break | ||
} | ||
|
||
return result | ||
} |