Commit 519ba896 authored by eddie's avatar eddie Committed by GitHub

test: unit tests for parseRemote (#7359)

parent ec784ccb
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`parseRemote parseRemoteActivities should parse NFT approval 1`] = `
Object {
"chainId": 1,
"descriptor": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [],
"nonce": 12345,
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Unknown Approval",
}
`;
exports[`parseRemote parseRemoteActivities should parse NFT approval for all 1`] = `
Object {
"chainId": 1,
"descriptor": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [],
"nonce": 12345,
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Unknown Approval",
}
`;
exports[`parseRemote parseRemoteActivities should parse NFT receive 1`] = `
Object {
"chainId": 1,
"currencies": undefined,
"descriptor": "1 SomeCollectionName from ",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"imageUrl",
],
"nonce": 12345,
"otherAccount": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Received",
}
`;
exports[`parseRemote parseRemoteActivities should parse NFT transfer 1`] = `
Object {
"chainId": 1,
"descriptor": "1 SomeCollectionName",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"imageUrl",
],
"nonce": 12345,
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Minted",
}
`;
exports[`parseRemote parseRemoteActivities should parse closed UniswapX order 1`] = `
Object {
"chainId": 1,
"currencies": Array [
Token {
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "DAI",
"symbol": "DAI",
},
Token {
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "Wrapped Ether",
"symbol": "WETH",
},
],
"descriptor": "100 DAI for 200 WETH",
"from": "someOfferer",
"hash": "someHash",
"logos": Array [
"someUrl",
"someUrl",
],
"offchainOrderStatus": "expired",
"prefixIconSrc": "bolt.svg",
"status": "FAILED",
"statusMessage": "Your swap could not be fulfilled at this time. Please try again.",
"timestamp": 10000,
"title": "Swap expired",
}
`;
exports[`parseRemote parseRemoteActivities should parse moonpay purchase 1`] = `
Object {
"chainId": 1,
"currencies": Array [
Token {
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "Wrapped Ether",
"symbol": "WETH",
},
],
"descriptor": "100 WETH for 100",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"moonpay.svg",
],
"nonce": 12345,
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Purchased",
}
`;
exports[`parseRemote parseRemoteActivities should parse nft purchase 1`] = `
Object {
"chainId": 1,
"descriptor": "1 SomeCollectionName",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"imageUrl",
],
"nonce": 12345,
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Bought",
}
`;
exports[`parseRemote parseRemoteActivities should parse receive 1`] = `
Object {
"chainId": 1,
"currencies": Array [
Token {
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "Wrapped Ether",
"symbol": "WETH",
},
],
"descriptor": "100 WETH from ",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"logoUrl",
],
"nonce": 12345,
"otherAccount": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Received",
}
`;
exports[`parseRemote parseRemoteActivities should parse remove liquidity 1`] = `
Object {
"chainId": 1,
"currencies": Array [
Token {
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "Wrapped Ether",
"symbol": "WETH",
},
Token {
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "DAI",
"symbol": "DAI",
},
],
"descriptor": "100 WETH and 100 DAI",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"logoUrl",
"logoUrl",
],
"nonce": 12345,
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Removed Liquidity",
}
`;
exports[`parseRemote parseRemoteActivities should parse send 1`] = `
Object {
"chainId": 1,
"currencies": Array [
Token {
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "DAI",
"symbol": "DAI",
},
],
"descriptor": "100 DAI to ",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"logoUrl",
],
"nonce": 12345,
"otherAccount": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Sent",
}
`;
exports[`parseRemote parseRemoteActivities should parse swap 1`] = `
Object {
"chainId": 1,
"currencies": Array [
Token {
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "DAI",
"symbol": "DAI",
},
Token {
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "Wrapped Ether",
"symbol": "WETH",
},
],
"descriptor": "100 DAI for 100 WETH",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"logoUrl",
],
"nonce": 12345,
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Swapped",
}
`;
exports[`parseRemote parseRemoteActivities should parse swap order 1`] = `
Object {
"chainId": 1,
"currencies": Array [
Token {
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "DAI",
"symbol": "DAI",
},
Token {
"address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "Wrapped Ether",
"symbol": "WETH",
},
],
"descriptor": "100 DAI for 100 WETH",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"logoUrl",
],
"nonce": 12345,
"prefixIconSrc": "bolt.svg",
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Swapped",
}
`;
exports[`parseRemote parseRemoteActivities should parse token approval 1`] = `
Object {
"chainId": 1,
"currencies": Array [
Token {
"address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
"chainId": 1,
"decimals": 18,
"isNative": false,
"isToken": true,
"name": "DAI",
"symbol": "DAI",
},
],
"descriptor": "DAI",
"from": "0x50EC05ADe8280758E2077fcBC08D878D4aef79C3",
"hash": "someHash",
"logos": Array [
"logoUrl",
],
"nonce": 12345,
"status": "CONFIRMED",
"timestamp": 10000,
"title": "Approved",
}
`;
import { act, renderHook } from '@testing-library/react'
import ms from 'ms'
import {
MockClosedUniswapXOrder,
MockMoonpayPurchase,
MockNFTApproval,
MockNFTApprovalForAll,
MockNFTPurchase,
MockNFTReceive,
MockNFTTransfer,
MockOpenUniswapXOrder,
MockRemoveLiquidity,
MockSwapOrder,
MockTokenApproval,
MockTokenReceive,
MockTokenSend,
MockTokenTransfer,
} from './fixtures/activity'
import { parseRemoteActivities, useTimeSince } from './parseRemote'
describe('parseRemote', () => {
beforeEach(() => {
jest.useFakeTimers()
})
describe('parseRemoteActivities', () => {
it('should not parse open UniswapX order', () => {
const result = parseRemoteActivities(jest.fn(), [MockOpenUniswapXOrder])
expect(result).toEqual({})
})
it('should parse closed UniswapX order', () => {
const result = parseRemoteActivities(jest.fn(), [MockClosedUniswapXOrder])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse NFT approval', () => {
const result = parseRemoteActivities(jest.fn(), [MockNFTApproval])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse NFT approval for all', () => {
const result = parseRemoteActivities(jest.fn(), [MockNFTApprovalForAll])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse NFT transfer', () => {
const result = parseRemoteActivities(jest.fn(), [MockNFTTransfer])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse swap', () => {
const result = parseRemoteActivities(jest.fn().mockReturnValue('100'), [MockTokenTransfer])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse nft purchase', () => {
const result = parseRemoteActivities(jest.fn().mockReturnValue('100'), [MockNFTPurchase])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse token approval', () => {
const result = parseRemoteActivities(jest.fn(), [MockTokenApproval])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse send', () => {
const result = parseRemoteActivities(jest.fn().mockReturnValue(100), [MockTokenSend])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse receive', () => {
const result = parseRemoteActivities(jest.fn().mockReturnValue(100), [MockTokenReceive])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse NFT receive', () => {
const result = parseRemoteActivities(jest.fn().mockReturnValue(100), [MockNFTReceive])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse remove liquidity', () => {
const result = parseRemoteActivities(jest.fn().mockReturnValue(100), [MockRemoveLiquidity])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse moonpay purchase', () => {
const result = parseRemoteActivities(jest.fn().mockReturnValue('100'), [MockMoonpayPurchase])
expect(result?.['someHash']).toMatchSnapshot()
})
it('should parse swap order', () => {
const result = parseRemoteActivities(jest.fn().mockReturnValue('100'), [MockSwapOrder])
expect(result?.['someHash']).toMatchSnapshot()
})
})
describe('useTimeSince', () => {
beforeEach(() => {
jest.useFakeTimers()
})
afterEach(() => {
jest.useRealTimers()
})
it('should initialize with the correct time since', () => {
const timestamp = Math.floor(Date.now() / 1000) - 60 // 60 seconds ago
const { result } = renderHook(() => useTimeSince(timestamp))
expect(result.current).toBe('1m')
})
it('should update time since every second', async () => {
const timestamp = Math.floor(Date.now() / 1000) - 50 // 50 seconds ago
const { result, rerender } = renderHook(() => useTimeSince(timestamp))
act(() => {
jest.advanceTimersByTime(ms('1.1s'))
})
rerender()
expect(result.current).toBe('51s')
})
it('should stop updating after 61 seconds', () => {
const timestamp = Math.floor(Date.now() / 1000) - 61 // 61 seconds ago
const { result, rerender } = renderHook(() => useTimeSince(timestamp))
act(() => {
jest.advanceTimersByTime(ms('121.1s'))
})
rerender()
// maxes out at 1m
expect(result.current).toBe('1m')
})
})
})
......@@ -118,7 +118,7 @@ export function gqlToCurrency(token: {
const chainId = supportedChainIdFromGQLChain(token.chain)
if (!chainId) return undefined
if (token.standard === TokenStandard.Native || !token.address) return nativeOnChain(chainId)
else return new Token(chainId, token.address, token.decimals ?? 18, token.name, token.symbol)
else return new Token(chainId, token.address, token.decimals ?? 18, token.symbol, token.name)
}
const URL_CHAIN_PARAM_TO_BACKEND: { [key: string]: InterfaceGqlChain } = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment