-
Notifications
You must be signed in to change notification settings - Fork 7
Add registry provider to UI components #1705
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
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
bf7b48e
Add registryProvider to ui-components
hardingjam 48de062
webapp changes
hardingjam 5e653b0
update tests
hardingjam 4b16b3b
Modals causing mocking error
hardingjam bfcafbd
remove inline mock of orderbook
hardingjam af075cc
Move tests back to main
hardingjam 6ba629c
format and lint
hardingjam 636e102
remove log
hardingjam 652fa9d
revert modal
hardingjam 4efba7a
Merge branch 'main' into Add-RegistryProvider-to-ui-components
hardingjam bc525e2
ai comments
hardingjam b7425fa
Merge branch 'main' into Add-RegistryProvider-to-ui-components
hardingjam 7d5a610
move tests
hardingjam 4b1c7fa
formatted
hardingjam ca6d5da
remove unused
hardingjam c1f65ff
add return type
hardingjam 42dac7d
fix test
hardingjam 80858cb
add warning
hardingjam b66a092
reload on deploy click
hardingjam cda4102
format
hardingjam 112cc5c
ai comments
hardingjam 40665c8
moced and tested
hardingjam dc2ef5a
rm comment
hardingjam e733de5
Remove store, use RegistryManager directly
hardingjam fdc4c4c
remove unused
hardingjam f5027c9
Merge branch 'main' into Add-RegistryProvider-to-ui-components
hardingjam b852aa2
address AI comments
hardingjam 96018a4
remove consts
hardingjam a9aa4f4
add ai checks
hardingjam 4b82043
Merge branch 'main' into Add-RegistryProvider-to-ui-components
hardyjosh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
packages/ui-components/src/__fixtures__/RegistryManager.ts
This file contains hidden or 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,17 @@ | ||
import { vi } from 'vitest'; | ||
import type { RegistryManager } from '$lib/providers/registry/RegistryManager'; | ||
|
||
const mockDefaultRegistry = 'https://example.com/default-registry.json'; | ||
let mockCurrentRegistry: string | null = mockDefaultRegistry; | ||
|
||
export const initialRegistry: Partial<RegistryManager> = { | ||
getCurrentRegistry: vi.fn(() => mockCurrentRegistry ?? mockDefaultRegistry), | ||
setRegistry: vi.fn((newRegistry: string) => { | ||
mockCurrentRegistry = newRegistry; | ||
}), | ||
resetToDefault: vi.fn(() => { | ||
mockCurrentRegistry = mockDefaultRegistry; | ||
}), | ||
updateUrlWithRegistry: vi.fn(), | ||
isCustomRegistry: vi.fn(() => mockCurrentRegistry !== mockDefaultRegistry) | ||
}; |
This file contains hidden or 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
178 changes: 178 additions & 0 deletions
178
packages/ui-components/src/__tests__/RegistryManager.test.ts
This file contains hidden or 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,178 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
import { RegistryManager } from '../lib/providers/registry/RegistryManager'; | ||
|
||
const DEFAULT_REGISTRY_URL = 'https://default.registry.url/registry.json'; | ||
const CUSTOM_REGISTRY_URL = 'https://custom.registry.url/registry.json'; | ||
const STORAGE_KEY = 'registry'; | ||
|
||
const mockLocalStorage = (() => { | ||
let store: Record<string, string> = {}; | ||
return { | ||
getItem: vi.fn((key: string) => store[key] || null), | ||
setItem: vi.fn((key: string, value: string) => { | ||
store[key] = value; | ||
}), | ||
removeItem: vi.fn((key: string) => { | ||
delete store[key]; | ||
}), | ||
clear: () => { | ||
store = {}; | ||
}, | ||
getStore: () => store | ||
}; | ||
})(); | ||
|
||
const mockLocation = (searchParams: URLSearchParams) => ({ | ||
href: `http://localhost/?${searchParams.toString()}`, | ||
searchParams | ||
}); | ||
|
||
const mockHistory = { | ||
pushState: vi.fn() | ||
}; | ||
|
||
vi.stubGlobal('localStorage', mockLocalStorage); | ||
vi.stubGlobal('history', mockHistory); | ||
|
||
const setMockLocation = (params: Record<string, string>) => { | ||
const searchParams = new URLSearchParams(params); | ||
Object.defineProperty(window, 'location', { | ||
value: mockLocation(searchParams), | ||
writable: true | ||
}); | ||
}; | ||
|
||
describe('RegistryManager', () => { | ||
beforeEach(() => { | ||
mockLocalStorage.clear(); | ||
vi.clearAllMocks(); | ||
setMockLocation({}); | ||
}); | ||
|
||
it('should initialize with default registry if no URL param or localStorage', () => { | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
expect(manager.getCurrentRegistry()).toBe(DEFAULT_REGISTRY_URL); | ||
expect(mockLocalStorage.getItem).toHaveBeenCalledWith(STORAGE_KEY); | ||
expect(mockLocalStorage.setItem).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should initialize with URL parameter if present', () => { | ||
setMockLocation({ [STORAGE_KEY]: CUSTOM_REGISTRY_URL }); | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
expect(manager.getCurrentRegistry()).toBe(CUSTOM_REGISTRY_URL); | ||
expect(mockLocalStorage.setItem).toHaveBeenCalledWith(STORAGE_KEY, CUSTOM_REGISTRY_URL); | ||
}); | ||
|
||
it('should initialize with localStorage value if present (and no URL param)', () => { | ||
mockLocalStorage.setItem(STORAGE_KEY, CUSTOM_REGISTRY_URL); | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
expect(manager.getCurrentRegistry()).toBe(CUSTOM_REGISTRY_URL); | ||
expect(mockLocalStorage.getItem).toHaveBeenCalledWith(STORAGE_KEY); | ||
expect(mockLocalStorage.setItem).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should prioritize URL parameter over localStorage on initialization', () => { | ||
const urlRegistry = 'https://from.url/registry.json'; | ||
setMockLocation({ [STORAGE_KEY]: urlRegistry }); | ||
mockLocalStorage.setItem(STORAGE_KEY, CUSTOM_REGISTRY_URL); | ||
|
||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
expect(manager.getCurrentRegistry()).toBe(urlRegistry); | ||
expect(mockLocalStorage.setItem).toHaveBeenCalledWith(STORAGE_KEY, urlRegistry); | ||
}); | ||
|
||
it('getCurrentRegistry() should return the current registry', () => { | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
expect(manager.getCurrentRegistry()).toBe(DEFAULT_REGISTRY_URL); | ||
|
||
setMockLocation({ [STORAGE_KEY]: CUSTOM_REGISTRY_URL }); | ||
const manager2 = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
expect(manager2.getCurrentRegistry()).toBe(CUSTOM_REGISTRY_URL); | ||
}); | ||
|
||
it('setRegistry() should update current registry, localStorage, and URL', () => { | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
manager.setRegistry(CUSTOM_REGISTRY_URL); | ||
|
||
expect(manager.getCurrentRegistry()).toBe(CUSTOM_REGISTRY_URL); | ||
expect(mockLocalStorage.setItem).toHaveBeenCalledWith(STORAGE_KEY, CUSTOM_REGISTRY_URL); | ||
expect(mockHistory.pushState).toHaveBeenCalledTimes(1); | ||
const expectedUrl = new URL(window.location.href); | ||
expectedUrl.searchParams.set(STORAGE_KEY, CUSTOM_REGISTRY_URL); | ||
expect(mockHistory.pushState).toHaveBeenCalledWith({}, '', expectedUrl.toString()); | ||
}); | ||
|
||
it('resetToDefault() should reset registry, clear localStorage, and update URL', () => { | ||
mockLocalStorage.setItem(STORAGE_KEY, CUSTOM_REGISTRY_URL); | ||
setMockLocation({ [STORAGE_KEY]: CUSTOM_REGISTRY_URL }); | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
expect(manager.getCurrentRegistry()).toBe(CUSTOM_REGISTRY_URL); | ||
|
||
manager.resetToDefault(); | ||
|
||
expect(manager.getCurrentRegistry()).toBe(DEFAULT_REGISTRY_URL); | ||
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith(STORAGE_KEY); | ||
expect(mockHistory.pushState).toHaveBeenCalledTimes(1); | ||
const expectedUrl = new URL(window.location.href); | ||
expectedUrl.searchParams.delete(STORAGE_KEY); | ||
expect(mockHistory.pushState).toHaveBeenCalledWith({}, '', expectedUrl.toString()); | ||
}); | ||
|
||
it('updateUrlWithRegistry() should update URL search parameter', () => { | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
manager.updateUrlWithRegistry(CUSTOM_REGISTRY_URL); | ||
|
||
expect(mockHistory.pushState).toHaveBeenCalledTimes(1); | ||
const expectedUrl = new URL(window.location.href); | ||
expectedUrl.searchParams.set(STORAGE_KEY, CUSTOM_REGISTRY_URL); | ||
expect(mockHistory.pushState).toHaveBeenCalledWith({}, '', expectedUrl.toString()); | ||
}); | ||
|
||
it('updateUrlWithRegistry() should remove URL search parameter when value is null', () => { | ||
setMockLocation({ [STORAGE_KEY]: CUSTOM_REGISTRY_URL }); | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
manager.updateUrlWithRegistry(null); | ||
|
||
expect(mockHistory.pushState).toHaveBeenCalledTimes(1); | ||
const expectedUrl = new URL(window.location.href); | ||
expectedUrl.searchParams.delete(STORAGE_KEY); | ||
expect(mockHistory.pushState).toHaveBeenCalledWith({}, '', expectedUrl.toString()); | ||
}); | ||
|
||
it('isCustomRegistry() should return false when using default registry', () => { | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
expect(manager.isCustomRegistry()).toBe(false); | ||
}); | ||
|
||
it('isCustomRegistry() should return true when using a custom registry', () => { | ||
mockLocalStorage.setItem(STORAGE_KEY, CUSTOM_REGISTRY_URL); | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
expect(manager.isCustomRegistry()).toBe(true); | ||
}); | ||
|
||
it('isCustomRegistry() should return false after resetting to default', () => { | ||
mockLocalStorage.setItem(STORAGE_KEY, CUSTOM_REGISTRY_URL); | ||
const manager = new RegistryManager(DEFAULT_REGISTRY_URL); | ||
expect(manager.isCustomRegistry()).toBe(true); | ||
manager.resetToDefault(); | ||
expect(manager.isCustomRegistry()).toBe(false); | ||
}); | ||
|
||
it('should handle localStorage errors gracefully (at least not crash)', () => { | ||
vi.spyOn(mockLocalStorage, 'getItem').mockImplementation(() => { | ||
throw new Error('localStorage unavailable'); | ||
}); | ||
vi.spyOn(mockLocalStorage, 'setItem').mockImplementation(() => { | ||
throw new Error('localStorage unavailable'); | ||
}); | ||
vi.spyOn(mockLocalStorage, 'removeItem').mockImplementation(() => { | ||
throw new Error('localStorage unavailable'); | ||
}); | ||
|
||
expect(() => new RegistryManager(DEFAULT_REGISTRY_URL)).toThrow( | ||
/Failed to access localStorage|Failed to save to localStorage/ | ||
); | ||
|
||
vi.restoreAllMocks(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.