-
Notifications
You must be signed in to change notification settings - Fork 177
fix: handle widgetCustomTokens parameter #7999
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
base: develop
Are you sure you want to change the base?
Changes from 5 commits
ae14147
18a0011
90d17e5
63ccfef
a9ceb66
162210d
ed327ca
0499519
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { useListsEnabledState } from '@cowprotocol/tokens' | ||
|
|
||
| import { renderHook } from '@testing-library/react' | ||
|
|
||
| import { useEnabledTokensListsUrls } from './useEnabledTokensListsUrls' | ||
|
|
||
| jest.mock('@cowprotocol/tokens', () => ({ | ||
| useListsEnabledState: jest.fn(), | ||
| })) | ||
|
|
||
| const useListsEnabledStateMock = jest.requireMock<{ useListsEnabledState: jest.Mock }>( | ||
| '@cowprotocol/tokens', | ||
| ).useListsEnabledState | ||
|
|
||
| function mockEnabledState(state: Record<string, boolean>): void { | ||
| useListsEnabledStateMock.mockReturnValue(state as unknown as ReturnType<typeof useListsEnabledState>) | ||
| } | ||
|
|
||
| describe('useEnabledTokensListsUrls', () => { | ||
| beforeEach(() => { | ||
| useListsEnabledStateMock.mockReset() | ||
| }) | ||
|
|
||
| it('returns an empty array when no lists are enabled', () => { | ||
| mockEnabledState({}) | ||
|
|
||
| const { result } = renderHook(() => useEnabledTokensListsUrls()) | ||
|
|
||
| expect(result.current).toEqual([]) | ||
| }) | ||
|
|
||
| it('excludes disabled list urls', () => { | ||
| mockEnabledState({ | ||
| 'https://example.com/list-a.json': true, | ||
| 'https://example.com/list-b.json': false, | ||
| }) | ||
|
|
||
| const { result } = renderHook(() => useEnabledTokensListsUrls()) | ||
|
|
||
| expect(result.current).toEqual(['https://example.com/list-a.json']) | ||
| }) | ||
|
|
||
| it('excludes non-http(s) sources such as virtual widget list ids', () => { | ||
| mockEnabledState({ | ||
| 'https://example.com/list-a.json': true, | ||
| widgetCustomTokens: true, | ||
| }) | ||
|
|
||
| const { result } = renderHook(() => useEnabledTokensListsUrls()) | ||
|
|
||
| expect(result.current).toEqual(['https://example.com/list-a.json']) | ||
| }) | ||
|
|
||
| it('returns enabled list urls sorted alphabetically', () => { | ||
| mockEnabledState({ | ||
| 'https://example.com/z-list.json': true, | ||
| 'https://example.com/a-list.json': true, | ||
| }) | ||
|
|
||
| const { result } = renderHook(() => useEnabledTokensListsUrls()) | ||
|
|
||
| expect(result.current).toEqual(['https://example.com/a-list.json', 'https://example.com/z-list.json']) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { useMemo } from 'react' | ||
|
|
||
| import { isHttpUrl } from '@cowprotocol/common-utils' | ||
| import { useListsEnabledState } from '@cowprotocol/tokens' | ||
|
|
||
| export function useEnabledTokensListsUrls(): string[] { | ||
|
|
@@ -8,7 +9,7 @@ export function useEnabledTokensListsUrls(): string[] { | |
| return useMemo( | ||
| () => | ||
| Object.entries(enabledState) | ||
| .filter(([, enabled]) => enabled === true) | ||
| .filter(([source, enabled]) => enabled === true && isHttpUrl(source)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure about that? It looks like lists with non-http source become always disabled
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you, replace by filtering all virtualList sources |
||
| .map(([source]) => source) | ||
| .sort(), | ||
| [enabledState], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,12 @@ | |
| "import": "./src/json-utils.ts", | ||
| "require": "./src/json-utils.ts", | ||
| "default": "./src/json-utils.ts" | ||
| }, | ||
| "./safeLink": { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great solution! |
||
| "types": "./src/safeLink.ts", | ||
| "import": "./src/safeLink.ts", | ||
| "require": "./src/safeLink.ts", | ||
| "default": "./src/safeLink.ts" | ||
| } | ||
| }, | ||
| "publishConfig": { | ||
|
|
@@ -31,6 +37,11 @@ | |
| "types": "./json-utils.d.ts", | ||
| "import": "./json-utils.mjs", | ||
| "require": "./json-utils.js" | ||
| }, | ||
| "./safeLink": { | ||
| "types": "./safeLink.d.ts", | ||
| "import": "./safeLink.mjs", | ||
| "require": "./safeLink.js" | ||
| } | ||
| } | ||
| }, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.