-
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 #1255 from rainlanguage/2025-01-10-remove-order-bu…
…tton Adding remove order button to order details page on webapp
- Loading branch information
Showing
8 changed files
with
213 additions
and
11 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
import { render, screen, fireEvent } from '@testing-library/svelte'; | ||
import OrderRemoveModal from '$lib/components/OrderRemoveModal.svelte'; | ||
import { transactionStore } from '@rainlanguage/ui-components'; | ||
import type { ComponentProps } from 'svelte'; | ||
|
||
export type ModalProps = ComponentProps<OrderRemoveModal>; | ||
|
||
vi.mock('@rainlanguage/orderbook/js_api', () => ({ | ||
getRemoveOrderCalldata: vi.fn().mockResolvedValue('0x123') | ||
})); | ||
|
||
vi.useFakeTimers(); | ||
|
||
describe('OrderRemoveModal', () => { | ||
const mockOrder = { | ||
id: '1', | ||
orderHash: '0x123', | ||
owner: '0x456' | ||
}; | ||
|
||
const defaultProps = { | ||
open: true, | ||
order: mockOrder, | ||
onRemove: vi.fn(), | ||
wagmiConfig: {}, | ||
chainId: 1, | ||
orderbookAddress: '0x789' | ||
} as unknown as ModalProps; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
transactionStore.reset(); | ||
}); | ||
|
||
it('handles transaction correctly', async () => { | ||
const handleTransactionSpy = vi.spyOn(transactionStore, 'handleRemoveOrderTransaction'); | ||
render(OrderRemoveModal, defaultProps); | ||
|
||
await vi.runAllTimersAsync(); | ||
|
||
expect(handleTransactionSpy).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
chainId: 1, | ||
orderbookAddress: '0x789', | ||
config: {} | ||
}) | ||
); | ||
}); | ||
|
||
it('closes modal and resets transaction store', async () => { | ||
render(OrderRemoveModal, defaultProps); | ||
const resetSpy = vi.spyOn(transactionStore, 'reset'); | ||
|
||
const closeButton = screen.getByLabelText('Close modal'); | ||
await fireEvent.click(closeButton); | ||
|
||
expect(resetSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls onRemove callback after successful transaction', async () => { | ||
render(OrderRemoveModal, defaultProps); | ||
|
||
transactionStore.transactionSuccess('0x123'); | ||
await vi.runAllTimersAsync(); | ||
|
||
expect(defaultProps.onRemove).toHaveBeenCalled(); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/webapp/src/lib/components/OrderRemoveModal.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,45 @@ | ||
<script lang="ts"> | ||
import { transactionStore } from '@rainlanguage/ui-components'; | ||
import TransactionModal from './TransactionModal.svelte'; | ||
import type { OrderSubgraph } from '@rainlanguage/orderbook/js_api'; | ||
import { getRemoveOrderCalldata } from '@rainlanguage/orderbook/js_api'; | ||
import type { Config } from 'wagmi'; | ||
export let open: boolean; | ||
export let order: OrderSubgraph; | ||
export let onRemove: () => void; | ||
export let wagmiConfig: Config; | ||
export let chainId: number; | ||
export let orderbookAddress: string; | ||
const messages = { | ||
success: 'Order was successfully removed.', | ||
pending: 'Removing order...', | ||
error: 'Could not remove order.' | ||
}; | ||
function handleSuccess() { | ||
setTimeout(() => { | ||
onRemove(); | ||
}, 5000); | ||
} | ||
function handleClose() { | ||
transactionStore.reset(); | ||
open = false; | ||
} | ||
async function handleTransaction() { | ||
const removeOrderCalldata = await getRemoveOrderCalldata(order); | ||
transactionStore.handleRemoveOrderTransaction({ | ||
config: wagmiConfig, | ||
removeOrderCalldata, | ||
orderbookAddress: orderbookAddress as `0x${string}`, | ||
chainId | ||
}); | ||
} | ||
handleTransaction(); | ||
</script> | ||
|
||
<TransactionModal bind:open {messages} on:close={handleClose} on:success={handleSuccess} /> |
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