forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownload-receipt-button.test.tsx
More file actions
64 lines (53 loc) · 1.81 KB
/
Copy pathdownload-receipt-button.test.tsx
File metadata and controls
64 lines (53 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import { DownloadReceiptButton } from '@/components/streams/download-receipt-button'
import type { StreamData } from '@/types/stream'
vi.mock('sonner', () => ({
toast: { success: vi.fn(), error: vi.fn() },
}))
const TOKEN = { address: 'TADDR', symbol: 'USDC', decimals: 7 }
function makeStream(): StreamData {
return {
id: 'stream-123',
sender: 'GSENDER123',
recipient: 'GRECIPIENT456',
token: TOKEN,
depositedAmount: 1_000_000_000n,
withdrawnAmount: 0n,
startTime: 1000n,
endTime: 2000n,
cliffTime: 1000n,
cliffAmount: 0n,
amountPerSecond: 1_000_000n,
linearAmount: 1_000_000_000n,
duration: 1000n,
cancelled: false,
}
}
describe('DownloadReceiptButton openInBrowser', () => {
const blobUrl = 'blob:http://localhost/receipt'
beforeEach(() => {
vi.useFakeTimers()
vi.stubGlobal('URL', {
createObjectURL: vi.fn(() => blobUrl),
revokeObjectURL: vi.fn(),
})
vi.stubGlobal('open', vi.fn())
})
afterEach(() => {
vi.useRealTimers()
vi.unstubAllGlobals()
})
it('revokes the receipt blob URL after the new tab has had time to load', () => {
render(<DownloadReceiptButton stream={makeStream()} />)
fireEvent.click(screen.getByRole('button', { name: /download receipt/i }))
fireEvent.click(screen.getByText(/view & print html/i))
expect(URL.createObjectURL).toHaveBeenCalledTimes(1)
expect(window.open).toHaveBeenCalledWith(blobUrl, '_blank')
expect(URL.revokeObjectURL).not.toHaveBeenCalled()
vi.advanceTimersByTime(9_999)
expect(URL.revokeObjectURL).not.toHaveBeenCalled()
vi.advanceTimersByTime(1)
expect(URL.revokeObjectURL).toHaveBeenCalledWith(blobUrl)
})
})