Skip to content

Commit

Permalink
fix: selector unit tests (refactor)
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Nov 29, 2024
1 parent 021cacc commit daa9ef2
Showing 1 changed file with 135 additions and 22 deletions.
157 changes: 135 additions & 22 deletions ui/ducks/bridge/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,17 @@ describe('Bridge selectors', () => {
describe('getBridgeQuotes', () => {
it('returns quote list and fetch data, insufficientBal=false,quotesRefreshCount=5', () => {
const state = createBridgeMockStore(
{ extensionConfig: { maxRefreshCount: 5 } },
{
toChainId: '0x1',
extensionConfig: {
maxRefreshCount: 5,
},
destNetworkAllowlist: ['0x89'],
},
{
toChainId: '0x89',
fromTokenExchangeRate: 1,
fromToken: { address: zeroAddress(), symbol: 'TEST' },
toToken: { address: zeroAddress(), symbol: 'TEST' },
toTokenExchangeRate: 0.99,
toNativeExchangeRate: 0.354073,
},
Expand All @@ -516,8 +523,20 @@ describe('Bridge selectors', () => {
currencyRates: {
ETH: {
conversionRate: 1,
usdConversionRate: 1,
},
POL: {
conversionRate: 1,
usdConversionRate: 1,
},
},
marketData: {},
...mockNetworkState(
{ chainId: CHAIN_IDS.MAINNET },
{ chainId: CHAIN_IDS.LINEA_MAINNET },
{ chainId: CHAIN_IDS.POLYGON },
{ chainId: CHAIN_IDS.OPTIMISM },
),
},
);

Expand Down Expand Up @@ -568,12 +587,16 @@ describe('Bridge selectors', () => {

it('returns quote list and fetch data, insufficientBal=false,quotesRefreshCount=2', () => {
const state = createBridgeMockStore(
{ extensionConfig: { maxRefreshCount: 5 } },
{
toChainId: '0x1',
extensionConfig: { maxRefreshCount: 5 },
destNetworkAllowlist: ['0x89'],
},
{
toChainId: '0x89',
fromToken: { address: zeroAddress(), symbol: 'ETH' },
toToken: { address: zeroAddress(), symbol: 'TEST' },
fromTokenExchangeRate: 1,
toTokenExchangeRate: 0.99,
toNativeExchangeRate: 0.354073,
},
{
quoteRequest: { insufficientBal: false },
Expand All @@ -590,23 +613,34 @@ describe('Bridge selectors', () => {
ETH: {
conversionRate: 1,
},
POL: {
conversionRate: 0.354073,
usdConversionRate: 1,
},
},
marketData: {},
...mockNetworkState(
{ chainId: CHAIN_IDS.MAINNET },
{ chainId: CHAIN_IDS.LINEA_MAINNET },
{ chainId: CHAIN_IDS.POLYGON },
{ chainId: CHAIN_IDS.OPTIMISM },
),
},
);
const result = getBridgeQuotes(state as never);

const recommendedQuoteMetadata = {
adjustedReturn: {
fiat: new BigNumber('13.84343712858974048'),
fiat: new BigNumber('13.843437128589739081572'),
},
cost: { fiat: new BigNumber('0.15656287141025952') },
cost: { fiat: new BigNumber('0.156562871410260918428') },
sentAmount: {
fiat: new BigNumber('14'),
amount: new BigNumber('14'),
},
swapRate: new BigNumber('0.998877142857142857142857142857142857'),
toTokenAmount: {
fiat: new BigNumber('13.8444372'),
fiat: new BigNumber('13.844437199999998601572'),
amount: new BigNumber('13.98428'),
},
gasFee: {
Expand All @@ -620,8 +654,8 @@ describe('Bridge selectors', () => {
};
expect(result.sortedQuotes).toHaveLength(2);
const EXPECTED_SORTED_COSTS = [
{ fiat: new BigNumber('0.15656287141025952') },
{ fiat: new BigNumber('0.33900008283534464') },
{ fiat: new BigNumber('0.156562871410260918428') },
{ fiat: new BigNumber('0.33900008283534602') },
];
result.sortedQuotes.forEach((quote, idx) => {
expect(quote.cost).toStrictEqual(EXPECTED_SORTED_COSTS[idx]);
Expand All @@ -647,12 +681,16 @@ describe('Bridge selectors', () => {

it('returns quote list and fetch data, insufficientBal=true', () => {
const state = createBridgeMockStore(
{ extensionConfig: { maxRefreshCount: 5 } },
{
toChainId: '0x1',
extensionConfig: { maxRefreshCount: 5 },
destNetworkAllowlist: ['0x89'],
},
{
toChainId: '0x89',
fromToken: { address: zeroAddress(), symbol: 'ETH' },
toToken: { address: zeroAddress(), symbol: 'TEST' },
fromTokenExchangeRate: 1,
toTokenExchangeRate: 0.99,
toNativeExchangeRate: 0.354073,
},
{
quoteRequest: { insufficientBal: true },
Expand All @@ -669,7 +707,18 @@ describe('Bridge selectors', () => {
ETH: {
conversionRate: 1,
},
POL: {
conversionRate: 1,
usdConversionRate: 1,
},
},
marketData: {},
...mockNetworkState(
{ chainId: CHAIN_IDS.MAINNET },
{ chainId: CHAIN_IDS.LINEA_MAINNET },
{ chainId: CHAIN_IDS.POLYGON },
{ chainId: CHAIN_IDS.OPTIMISM },
),
},
);
const result = getBridgeQuotes(state as never);
Expand Down Expand Up @@ -861,11 +910,14 @@ describe('Bridge selectors', () => {

it('should recommend 2nd fastest quote if adjustedReturn is less than 80% of cheapest quote', () => {
const state = createBridgeMockStore(
{},
{ destNetworkAllowlist: ['0x89'] },
{
toChainId: '0x89',
fromToken: { address: zeroAddress(), symbol: 'ETH' },
toToken: { address: zeroAddress(), symbol: 'TEST' },
fromTokenExchangeRate: 2524.25,
sortOrder: SortOrder.ETA_ASC,
toTokenExchangeRate: 0.998781,
toNativeExchangeRate: 0.354073,
},
{
quotes: [
Expand All @@ -886,7 +938,18 @@ describe('Bridge selectors', () => {
ETH: {
conversionRate: 2524.25,
},
POL: {
conversionRate: 0.354073,
usdConversionRate: 1,
},
},
marketData: {},
...mockNetworkState(
{ chainId: CHAIN_IDS.MAINNET },
{ chainId: CHAIN_IDS.LINEA_MAINNET },
{ chainId: CHAIN_IDS.POLYGON },
{ chainId: CHAIN_IDS.OPTIMISM },
),
},
);

Expand Down Expand Up @@ -1049,18 +1112,39 @@ describe('Bridge selectors', () => {

it('should return isInsufficientGasBalance=true when balance is 0 and fromToken is erc20', () => {
const state = createBridgeMockStore(
{},
{ destNetworkAllowlist: ['0x89'] },
{
toChainId: '0x1',
toChainId: '0x89',
toToken: {
address: '0x3c499c542cef5e3811e1192ce70d8cc03d5c3359',
symbol: 'TEST',
},
fromTokenInputValue: '0.001',
fromToken: { address: '0x123', decimals: 6 },
fromToken: {
address: '0x3c499c542cef5e3811e1192ce70d8cc03d5c3359',
decimals: 6,
},
toTokenExchangeRate: 0.798781,
},
{
srcTokens: { '0x00': { address: '0x00', symbol: 'TEST' } },
srcTopAssets: [{ address: '0x00', symbol: 'TEST' }],
quotesLastFetched: Date.now(),
quoteRequest: { srcTokenAmount: '100000000' },
},
{
currencyRates: {
POL: {
conversionRate: 0.354073,
usdConversionRate: 1,
},
},
...mockNetworkState(
{ chainId: CHAIN_IDS.MAINNET },
{ chainId: CHAIN_IDS.LINEA_MAINNET },
{ chainId: CHAIN_IDS.POLYGON },
),
},
);
const result = getValidationErrors(state as never);

Expand Down Expand Up @@ -1163,10 +1247,13 @@ describe('Bridge selectors', () => {

it('should return isEstimatedReturnLow=true return value is 20% less than sent funds', () => {
const state = createBridgeMockStore(
{},
{ destNetworkAllowlist: ['0x89'] },
{
toChainId: '0x89',
fromToken: { address: zeroAddress(), symbol: 'ETH' },
toToken: { address: zeroAddress(), symbol: 'TEST' },
fromTokenExchangeRate: 2524.25,
toTokenExchangeRate: 0.798781,
toNativeExchangeRate: 0.354073,
},
{
quotes: mockBridgeQuotesNativeErc20,
Expand All @@ -1176,7 +1263,18 @@ describe('Bridge selectors', () => {
ETH: {
conversionRate: 2524.25,
},
POL: {
conversionRate: 0.354073,
usdConversionRate: 1,
},
},
marketData: {},
...mockNetworkState(
{ chainId: CHAIN_IDS.MAINNET },
{ chainId: CHAIN_IDS.LINEA_MAINNET },
{ chainId: CHAIN_IDS.POLYGON },
{ chainId: CHAIN_IDS.OPTIMISM },
),
},
);
const result = getValidationErrors(state as never);
Expand All @@ -1195,10 +1293,13 @@ describe('Bridge selectors', () => {

it('should return isEstimatedReturnLow=false when return value is more than 80% of sent funds', () => {
const state = createBridgeMockStore(
{},
{ destNetworkAllowlist: ['0x89'] },
{
toChainId: '0x89',
fromToken: { address: zeroAddress(), symbol: 'ETH' },
toToken: { address: zeroAddress(), symbol: 'TEST' },
fromTokenExchangeRate: 2524.25,
toTokenExchangeRate: 0.998781,
toNativeExchangeRate: 0.354073,
},
{
quotes: mockBridgeQuotesNativeErc20,
Expand All @@ -1207,8 +1308,20 @@ describe('Bridge selectors', () => {
currencyRates: {
ETH: {
conversionRate: 2524.25,
usdConversionRate: 1,
},
POL: {
conversionRate: 1,
usdConversionRate: 1,
},
},
marketData: {},
...mockNetworkState(
{ chainId: CHAIN_IDS.MAINNET },
{ chainId: CHAIN_IDS.LINEA_MAINNET },
{ chainId: CHAIN_IDS.POLYGON },
{ chainId: CHAIN_IDS.OPTIMISM },
),
},
);
const result = getValidationErrors(state as never);
Expand Down

0 comments on commit daa9ef2

Please sign in to comment.