Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrodyHughes committed Jun 6, 2024
1 parent 41ff638 commit 2a67f14
Show file tree
Hide file tree
Showing 8 changed files with 958 additions and 192 deletions.
8 changes: 6 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ module.exports = function (api) {
'date-fns',
'graphql-tag',
['lodash', { id: ['lodash', 'recompact'] }],
'react-native-reanimated/plugin',
[
'module:react-native-dotenv',
{
allowUndefined: true,
moduleName: 'react-native-dotenv',
},
],
'react-native-reanimated/plugin',
];

const presets = ['module:metro-react-native-babel-preset'];
const presets = ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'];

return {
env: {
Expand All @@ -62,6 +62,10 @@ module.exports = function (api) {
],
presets: presets,
},
test: {
plugins: [...plugins],
presets: presets,
},
},
plugins,
presets,
Expand Down
17 changes: 17 additions & 0 deletions e2e/7_swapSheetFlow1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import { expect } from '@jest/globals';

import { quoteResponse } from './mocks/quoteResponse.mock';
import { fetchedPricesResponse } from './mocks/fetchedPrices.mock';
import { SwapProvider } from '@/__swaps__/screens/Swap/providers/swap-provider';

let executeSwapMock: jest.SpyInstance;

jest.mock('@/__swaps__/screens/Swap/hooks/useSwapInputsController', () => {
const originalModule = jest.requireActual('@/__swaps__/screens/Swap/hooks/useSwapInputsController');
Expand All @@ -37,6 +40,9 @@ jest.mock('@/__swaps__/screens/Swap/hooks/useSwapInputsController', () => {
};
});

// trying to mock the swap-provider here and then call executeSwap
jest.mock('@/__swaps__/screens/Swap/providers/swap-provider');

describe('Swap Sheet Interaction Flow', () => {
beforeAll(async () => {
await beforeAllcleanApp({ hardhat: true });
Expand All @@ -45,6 +51,15 @@ describe('Swap Sheet Interaction Flow', () => {
await afterAllcleanApp({ hardhat: true });
});

beforeEach(async () => {
executeSwapMock = jest.spyOn(SwapProvider.prototype, 'executeSwap').mockImplementation(() => {
// Mock implementation
});
});
afterEach(async () => {
executeSwapMock.mockRestore();
});

it('Import a wallet and go to welcome', async () => {
await importWalletFlow({ seedPhrase: true });
});
Expand Down Expand Up @@ -94,6 +109,8 @@ describe('Swap Sheet Interaction Flow', () => {
expect(reviewActionElements.elements[1].label).toContain('DAI');
expect(reviewActionElements.elements[2].label).toContain('Tap to Swap');
await tapByText('Tap to Swap');
await delayTime('long');
expect(executeSwapMock).toHaveBeenCalled();
// TODO: add validation
/**
* tap swap button
Expand Down
2 changes: 2 additions & 0 deletions e2e/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { device } from 'detox';

require('dotenv').config({ path: '.env' });

// jest.mock('src/__swaps__/utils/swap.ts', () => require('./mocks/worklets.mock.js'));

beforeAll(async () => {
if (device.getPlatform() === 'android') {
// connecting to metro
Expand Down
9 changes: 6 additions & 3 deletions e2e/jest.e2e.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ module.exports = {
transform: {
'\\.[jt]sx?$': 'ts-jest',
},
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '../',
}),
transformIgnorePatterns: ['/node_modules/(?!(react-native|@react-native|react-native-reanimated)/)'],
moduleNameMapper: {
...pathsToModuleNameMapper(compilerOptions.paths, { prefix: '../' }),
'^src/__swaps__/utils/swap.ts$': '<rootDir>/mocks/worklets.mock.js',
'^@/__swaps__/screens/Swap/providers/swap-provider$': '<rootDir>/src/__swaps__/screens/Swap/providers/__mocks__/swap-provider.js',
},
setupFiles: ['dotenv/config'],
};
51 changes: 51 additions & 0 deletions e2e/mocks/worklets.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// unused atm
export const divWorklet = (a, b) => {
'worklet';
return a / b;
};

export const floorWorklet = value => {
'worklet';
return Math.floor(value);
};

export const log10Worklet = value => {
'worklet';
return Math.log10(value);
};

export const powWorklet = (base, exponent) => {
'worklet';
return Math.pow(base, exponent);
};

export const mulWorklet = (a, b) => {
'worklet';
return a * b;
};

export const lessThanOrEqualToWorklet = (a, b) => {
'worklet';
return a <= b;
};

export const findNiceIncrement = availableBalance => {
'worklet';

const niceFactors = [1, 2, 10];
const exactIncrement = divWorklet(availableBalance, 100);
const orderOfMagnitude = floorWorklet(log10Worklet(exactIncrement));
const baseIncrement = powWorklet(10, orderOfMagnitude);

let adjustedIncrement = baseIncrement;

for (let i = niceFactors.length - 1; i >= 0; i--) {
const potentialIncrement = mulWorklet(baseIncrement, niceFactors[i]);
if (lessThanOrEqualToWorklet(potentialIncrement, exactIncrement)) {
adjustedIncrement = potentialIncrement;
break;
}
}

return adjustedIncrement;
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@
"@babel/plugin-proposal-optional-chaining": "7.21.0",
"@babel/plugin-transform-react-inline-elements": "7.18.6",
"@babel/plugin-transform-runtime": "7.18.10",
"@babel/preset-env": "7.22.0",
"@babel/preset-env": "7.24.7",
"@babel/preset-react": "7.24.7",
"@babel/preset-typescript": "7.24.7",
"@babel/runtime": "7.22.0",
"@lavamoat/allow-scripts": "2.3.1",
"@nomiclabs/hardhat-ethers": "2.2.3",
Expand Down Expand Up @@ -366,7 +368,7 @@
"resolve": "1.22.8",
"rn-nodeify": "10.2.0",
"schedule": "0.5.0",
"ts-jest": "29.1.1",
"ts-jest": "29.1.4",
"ts-loader": "9.5.1",
"ts-migrate": "0.1.26",
"typescript": "5.1.6",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const SwapProvider = jest.createMockFromModule('@/__swaps__/screens/Swap/providers/swap-provider');

SwapProvider.prototype.executeSwap = jest.fn(() => Promise.resolve());

module.exports = SwapProvider;
Loading

0 comments on commit 2a67f14

Please sign in to comment.