-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
9a8778e
commit 6b91953
Showing
4 changed files
with
120 additions
and
23 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
packages/ui-components/src/__tests__/InputRegistryUrl.test.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,91 @@ | ||
import { render, screen, fireEvent } from '@testing-library/svelte'; | ||
import { vi } from 'vitest'; | ||
import InputRegistryUrl from '../lib/components/input/InputRegistryUrl.svelte'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
describe('InputRegistryUrl', () => { | ||
const mockPushState = vi.fn(); | ||
const mockReload = vi.fn(); | ||
const mockLocalStorageSetItem = vi.fn(); | ||
|
||
beforeEach(() => { | ||
vi.stubGlobal('localStorage', { | ||
setItem: mockLocalStorageSetItem | ||
}); | ||
|
||
Object.defineProperty(window, 'location', { | ||
value: { | ||
pathname: '/test-path', | ||
reload: mockReload | ||
}, | ||
writable: true | ||
}); | ||
|
||
window.history.pushState = mockPushState; | ||
|
||
mockPushState.mockClear(); | ||
mockReload.mockClear(); | ||
mockLocalStorageSetItem.mockClear(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.unstubAllGlobals(); | ||
}); | ||
|
||
it('should render input and button', () => { | ||
render(InputRegistryUrl, { props: { newRegistryUrl: '' } }); | ||
|
||
const input = screen.getByPlaceholderText('Enter URL to raw strategy registry file'); | ||
const button = screen.getByText('Load Registry URL'); | ||
|
||
expect(input).toBeInTheDocument(); | ||
expect(button).toBeInTheDocument(); | ||
}); | ||
|
||
it('should bind input value to newRegistryUrl prop', async () => { | ||
const { component } = render(InputRegistryUrl, { props: { newRegistryUrl: '' } }); | ||
|
||
const input = screen.getByPlaceholderText('Enter URL to raw strategy registry file'); | ||
const testUrl = 'https://example.com/registry.json'; | ||
|
||
await userEvent.type(input, testUrl); | ||
|
||
expect(input).toHaveValue(testUrl); | ||
}); | ||
|
||
it('should handle registry URL loading when button is clicked', async () => { | ||
const testUrl = 'https://example.com/registry.json'; | ||
render(InputRegistryUrl, { props: { newRegistryUrl: testUrl } }); | ||
|
||
const button = screen.getByText('Load Registry URL'); | ||
await fireEvent.click(button); | ||
|
||
// Verify URL update | ||
expect(mockPushState).toHaveBeenCalledWith( | ||
{}, | ||
'', | ||
'/test-path?registry=' + testUrl | ||
); | ||
|
||
// Verify page reload | ||
expect(mockReload).toHaveBeenCalled(); | ||
|
||
// Verify localStorage update | ||
expect(mockLocalStorageSetItem).toHaveBeenCalledWith('registry', testUrl); | ||
}); | ||
|
||
it('should handle empty URL', async () => { | ||
render(InputRegistryUrl, { props: { newRegistryUrl: '' } }); | ||
|
||
const button = screen.getByText('Load Registry URL'); | ||
await fireEvent.click(button); | ||
|
||
expect(mockPushState).toHaveBeenCalledWith( | ||
{}, | ||
'', | ||
'/test-path?registry=' | ||
); | ||
expect(mockReload).toHaveBeenCalled(); | ||
expect(mockLocalStorageSetItem).toHaveBeenCalledWith('registry', ''); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/ui-components/src/lib/components/input/InputRegistryUrl.svelte
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,21 @@ | ||
<script lang="ts"> | ||
import { Button, Input } from 'flowbite-svelte'; | ||
export let newRegistryUrl: string; | ||
const loadRegistryUrl = () => { | ||
window.history.pushState({}, '', window.location.pathname + '?registry=' + newRegistryUrl); | ||
window.location.reload(); | ||
localStorage.setItem('registry', newRegistryUrl); | ||
}; | ||
</script> | ||
|
||
<div class="flex w-full items-start gap-4"> | ||
<Input | ||
id="strategy-url" | ||
type="url" | ||
placeholder="Enter URL to raw strategy registry file" | ||
bind:value={newRegistryUrl} | ||
/> | ||
<Button class="text-nowrap" on:click={loadRegistryUrl}>Load Registry URL</Button> | ||
</div> |
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
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