-
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.
Merge pull request #1147 from rainlanguage/01/13/25-fix-hash-links-to…
…-vaults-and-orders 01/13/25 fix hash links to vaults and orders
- Loading branch information
Showing
7 changed files
with
141 additions
and
63 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
packages/ui-components/src/__tests__/OrderOrVaultHash.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,71 @@ | ||
import { render, fireEvent } from '@testing-library/svelte'; | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import OrderOrVaultHash from '../lib/components/OrderOrVaultHash.svelte'; | ||
import { goto } from '$app/navigation'; | ||
|
||
vi.mock('$app/navigation', () => ({ | ||
goto: vi.fn() | ||
})); | ||
|
||
describe('OrderOrVaultHash', () => { | ||
const mockOrder = { | ||
id: '123', | ||
orderHash: '0x123abc', | ||
active: true | ||
}; | ||
|
||
const mockInactiveOrder = { | ||
...mockOrder, | ||
active: false | ||
}; | ||
|
||
const mockSubgraphName = 'test-subgraph'; | ||
const mockUpdateFn = vi.fn(); | ||
|
||
it('renders with active order', () => { | ||
const { getByTestId } = render(OrderOrVaultHash, { | ||
props: { | ||
type: 'orders', | ||
orderOrVault: mockOrder, | ||
network: mockSubgraphName, | ||
updateActiveNetworkAndOrderbook: mockUpdateFn | ||
} | ||
}); | ||
|
||
const button = getByTestId('vault-order-input'); | ||
expect(button).toBeTruthy(); | ||
expect(button.classList.toString()).toContain('text-white bg-green'); | ||
expect(button.getAttribute('data-id')).toBe('123'); | ||
}); | ||
|
||
it('renders with inactive order', () => { | ||
const { getByTestId } = render(OrderOrVaultHash, { | ||
props: { | ||
type: 'orders', | ||
orderOrVault: mockInactiveOrder, | ||
network: mockSubgraphName, | ||
updateActiveNetworkAndOrderbook: mockUpdateFn | ||
} | ||
}); | ||
|
||
const button = getByTestId('vault-order-input'); | ||
expect(button.classList.toString()).toContain('text-white bg-yellow'); | ||
}); | ||
|
||
it('handles click event correctly', async () => { | ||
const { getByTestId } = render(OrderOrVaultHash, { | ||
props: { | ||
type: 'orders', | ||
orderOrVault: mockOrder, | ||
network: mockSubgraphName, | ||
updateActiveNetworkAndOrderbook: mockUpdateFn | ||
} | ||
}); | ||
|
||
const button = getByTestId('vault-order-input'); | ||
await fireEvent.click(button); | ||
|
||
expect(mockUpdateFn).toHaveBeenCalledWith(mockSubgraphName); | ||
expect(goto).toHaveBeenCalledWith(`/orders/${mockSubgraphName}-${mockOrder.id}`); | ||
}); | ||
}); |
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
31 changes: 31 additions & 0 deletions
31
packages/ui-components/src/lib/components/OrderOrVaultHash.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,31 @@ | ||
<script lang="ts"> | ||
import { Button } from 'flowbite-svelte'; | ||
import Hash, { HashType } from './Hash.svelte'; | ||
import { goto } from '$app/navigation'; | ||
import type { OrderAsIO, OrderSubgraph, Vault } from '@rainlanguage/orderbook/js_api'; | ||
type OrderOrVault = OrderSubgraph | OrderAsIO | Vault; | ||
export let orderOrVault: OrderOrVault; | ||
export let type: 'orders' | 'vaults'; | ||
export let network: string; | ||
export let updateActiveNetworkAndOrderbook: (subgraphName: string) => void; | ||
let hash; | ||
$: isOrder = 'orderHash' in (orderOrVault || {}); | ||
$: slug = isOrder ? (orderOrVault as OrderSubgraph).id : (orderOrVault as Vault)?.id; | ||
$: hash = isOrder ? (orderOrVault as OrderSubgraph).id : (orderOrVault as Vault)?.id || ''; | ||
$: isActive = isOrder ? (orderOrVault as OrderAsIO).active : false; | ||
</script> | ||
|
||
<Button | ||
class="mr-1 mt-1 px-2 py-1 text-sm" | ||
color={isActive ? 'green' : 'yellow'} | ||
data-testid="vault-order-input" | ||
data-id={slug} | ||
on:click={() => { | ||
updateActiveNetworkAndOrderbook(network); | ||
goto(`/${type}/${network}-${slug}`); | ||
}}><Hash type={HashType.Identifier} value={hash} copyOnClick={false} /></Button | ||
> |
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
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