@openocean.finance/widget
Version:
Openocean Widget for cross-chain bridging and swapping. It will drive your multi-chain strategy and attract new users from everywhere.
58 lines (51 loc) • 1.95 kB
text/typescript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
describe('OpenOceanService', () => {
beforeEach(() => {
vi.resetModules()
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ data: { openapi_v1: ['https://rpc.example'] } }),
} satisfies Partial<Response>)
)
})
afterEach(() => {
vi.restoreAllMocks()
})
it('throws a fallback error when swap returns a non-200 business code with an empty error', async () => {
const { OpenOceanService } = await import('./OpenOceanService.js')
vi.mocked(globalThis.fetch).mockResolvedValue({
ok: true,
json: async () => ({ code: 500, error: '' }),
} satisfies Partial<Response> as Response)
await expect(
OpenOceanService.getSwapQuote({
chain: '8453',
inTokenAddress: '0x0000000000000000000000000000000000000001',
inTokenSymbol: 'ETH',
outTokenAddress: '0x0000000000000000000000000000000000000002',
outTokenSymbol: 'USDC',
amount: '1000000000000000000',
account: '0x0000000000000000000000000000000000000003',
})
).rejects.toThrow('Failed to fetch swap quote')
})
it('throws the API error message when quote returns a non-200 business code', async () => {
const { OpenOceanService } = await import('./OpenOceanService.js')
vi.mocked(globalThis.fetch).mockResolvedValue({
ok: true,
json: async () => ({ code: 500, error: 'Router unavailable' }),
} satisfies Partial<Response> as Response)
await expect(
OpenOceanService.getQuote({
chain: '8453',
inTokenAddress: '0x0000000000000000000000000000000000000001',
inTokenSymbol: 'ETH',
outTokenAddress: '0x0000000000000000000000000000000000000002',
outTokenSymbol: 'USDC',
amount: '1000000000000000000',
})
).rejects.toThrow('Router unavailable')
})
})