Skip to content

Commit

Permalink
oct-1366: rewards calculator test with real res
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikolajczyk committed Mar 7, 2024
1 parent d5a128f commit 5c496b1
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 84 deletions.
189 changes: 110 additions & 79 deletions client/cypress/e2e/rewardsCalculator.cy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { mockCoinPricesServer, visitWithLoader } from 'cypress/utils/e2e';
import { ETH_USD, mockCoinPricesServer, visitWithLoader } from 'cypress/utils/e2e';
import viewports from 'cypress/utils/viewports';
import { IS_ONBOARDING_ALWAYS_VISIBLE, IS_ONBOARDING_DONE } from 'src/constants/localStorageKeys';
import { ROOT_ROUTES } from 'src/routes/RootRoutes/routes';
import getFormattedEthValue from 'src/utils/getFormattedEthValue';
import { parseUnitsBigInt } from 'src/utils/parseUnitsBigInt';

Object.values(viewports).forEach(({ device, viewportWidth, viewportHeight, isDesktop }) => {
describe(`rewards calculator: ${device}`, { viewportHeight, viewportWidth }, () => {
Expand Down Expand Up @@ -38,10 +40,7 @@ Object.values(viewports).forEach(({ device, viewportWidth, viewportHeight, isDes
});

it('calculator fetches rewards values in ETH and USD based on DAYS and GLM fields', () => {
cy.intercept('POST', '/rewards/estimated_budget', {
body: { budget: '4253424657534245' },
delay: 500,
}).as('postEstimatedRewards');
cy.intercept('POST', '/rewards/estimated_budget').as('postEstimatedRewards');
cy.get('[data-test=Tooltip__rewardsCalculator__body]').click();
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto__Loader]').should(
'be.visible',
Expand All @@ -50,23 +49,32 @@ Object.values(viewports).forEach(({ device, viewportWidth, viewportHeight, isDes
'be.visible',
);
cy.wait('@postEstimatedRewards');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto__Loader]').should(
'not.exist',
);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat__Loader]').should(
'not.exist',
);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto]')
.invoke('val')
.should('eq', '0.0043');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat]')
.invoke('val')
.should('eq', '8.78');

cy.intercept('POST', '/rewards/estimated_budget', {
body: { budget: '425342465753424665' },
delay: 500,
}).as('postEstimatedRewardsGlmValueChange');
cy.get('@postEstimatedRewards').then(
({
response: {
body: { budget },
},
}) => {
const rewardsEth = getFormattedEthValue(parseUnitsBigInt(budget, 'wei')).value;
const rewardsUsd = (parseFloat(rewardsEth) * ETH_USD).toFixed(2);

cy.get(
'[data-test=RewardsCalculator__InputText--estimatedRewards--crypto__Loader]',
).should('not.exist');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat__Loader]').should(
'not.exist',
);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto]')
.invoke('val')
.should('eq', rewardsEth);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat]')
.invoke('val')
.should('eq', rewardsUsd);
},
);

cy.intercept('POST', '/rewards/estimated_budget').as('postEstimatedRewardsGlmValueChange');
cy.get('[data-test=RewardsCalculator__InputText--crypto]').type('500000');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto__Loader]').should(
'be.visible',
Expand All @@ -75,23 +83,32 @@ Object.values(viewports).forEach(({ device, viewportWidth, viewportHeight, isDes
'be.visible',
);
cy.wait('@postEstimatedRewardsGlmValueChange');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto__Loader]').should(
'not.exist',
);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat__Loader]').should(
'not.exist',
);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto]')
.invoke('val')
.should('eq', '0.4253');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat]')
.invoke('val')
.should('eq', '868.42');

cy.intercept('POST', '/rewards/estimated_budget', {
body: { budget: '4253424657534246509' },
delay: 500,
}).as('postEstimatedRewardsDaysValueChange');
cy.get('@postEstimatedRewardsGlmValueChange').then(
({
response: {
body: { budget },
},
}) => {
const rewardsEth = getFormattedEthValue(parseUnitsBigInt(budget, 'wei')).value;
const rewardsUsd = (parseFloat(rewardsEth) * ETH_USD).toFixed(2);

cy.get(
'[data-test=RewardsCalculator__InputText--estimatedRewards--crypto__Loader]',
).should('not.exist');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat__Loader]').should(
'not.exist',
);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto]')
.invoke('val')
.should('eq', rewardsEth);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat]')
.invoke('val')
.should('eq', rewardsUsd);
},
);

cy.intercept('POST', '/rewards/estimated_budget').as('postEstimatedRewardsDaysValueChange');
cy.get('[data-test=RewardsCalculator__InputText--days]').clear().type('900');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto__Loader]').should(
'be.visible',
Expand All @@ -100,18 +117,30 @@ Object.values(viewports).forEach(({ device, viewportWidth, viewportHeight, isDes
'be.visible',
);
cy.wait('@postEstimatedRewardsDaysValueChange');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto__Loader]').should(
'not.exist',
);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat__Loader]').should(
'not.exist',

cy.get('@postEstimatedRewardsDaysValueChange').then(
({
response: {
body: { budget },
},
}) => {
const rewardsEth = getFormattedEthValue(parseUnitsBigInt(budget, 'wei')).value;
const rewardsUsd = (parseFloat(rewardsEth) * ETH_USD).toFixed(2);

cy.get(
'[data-test=RewardsCalculator__InputText--estimatedRewards--crypto__Loader]',
).should('not.exist');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat__Loader]').should(
'not.exist',
);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto]')
.invoke('val')
.should('eq', rewardsEth);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat]')
.invoke('val')
.should('eq', rewardsUsd);
},
);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto]')
.invoke('val')
.should('eq', '4.2534');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat]')
.invoke('val')
.should('eq', '8685.06');
});

it('If DAYS or GLM input is empty rewards inputs are empty too', () => {
Expand Down Expand Up @@ -160,46 +189,48 @@ Object.values(viewports).forEach(({ device, viewportWidth, viewportHeight, isDes
});

it('Max GLM amount is 1000000000', () => {
cy.intercept('POST', '/rewards/estimated_budget', {
body: { budget: '850684931506849269541' },
delay: 500,
}).as('postEstimatedRewards');
cy.intercept('POST', '/rewards/estimated_budget').as('postEstimatedRewards');

cy.get('[data-test=Tooltip__rewardsCalculator__body]').click();

cy.get('[data-test=RewardsCalculator__InputText--crypto]').type('1000000000');
cy.wait('@postEstimatedRewards');

cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto]')
.invoke('val')
.should('eq', '850.6849');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat]')
.invoke('val')
.should('eq', '1737022.00');

cy.get('[data-test=RewardsCalculator__InputText--crypto]')
.clear()
.type('1000000001')
.should('have.css', 'border-color', 'rgb(255, 97, 87)');
cy.get('[data-test=RewardsCalculator__InputText--crypto__error]')
.should('be.visible')
.invoke('text')
.should('eq', 'That isn’t a valid amount');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto]')
.invoke('val')
.should('eq', '');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat]')
.invoke('val')
.should('eq', '');
cy.get('@postEstimatedRewards').then(
({
response: {
body: { budget },
},
}) => {
const rewardsEth = getFormattedEthValue(parseUnitsBigInt(budget, 'wei')).value;
const rewardsUsd = (parseFloat(rewardsEth) * ETH_USD).toFixed(2);

cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto]')
.invoke('val')
.should('eq', rewardsEth);
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat]')
.invoke('val')
.should('eq', rewardsUsd);

cy.get('[data-test=RewardsCalculator__InputText--crypto]')
.clear()
.type('1000000001')
.should('have.css', 'border-color', 'rgb(255, 97, 87)');
cy.get('[data-test=RewardsCalculator__InputText--crypto__error]')
.should('be.visible')
.invoke('text')
.should('eq', 'That isn’t a valid amount');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--crypto]')
.invoke('val')
.should('eq', '');
cy.get('[data-test=RewardsCalculator__InputText--estimatedRewards--fiat]')
.invoke('val')
.should('eq', '');
},
);
});

it('Closing the modal successfully cancels the request /estimated_budget', () => {
cy.intercept('POST', '/rewards/estimated_budget', {
body: { budget: '850684931506849269541' },
// Long enough to never complete.
delay: 5000000,
}).as('postEstimatedRewards');

cy.window().then(win => {
cy.spy(win.console, 'error').as('consoleErrSpy');
});
Expand Down
4 changes: 3 additions & 1 deletion client/cypress/utils/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { readContractEpochs } from 'src/hooks/contracts/readContracts';

import Chainable = Cypress.Chainable;

export const ETH_USD = 2041.91;

export const loadersShouldNotExist = (): Chainable<any> => {
cy.get('[data-test*=AppLoader]').should('not.exist');
return cy.get('[data-test=MainLayout__Loader]').should('not.exist');
Expand All @@ -29,7 +31,7 @@ export const navigateWithCheck = (urlEnter: string): Chainable<any> => {

export const mockCoinPricesServer = (): Chainable<any> => {
return cy.intercept('GET', '/simple/price?*', {
body: { ethereum: { usd: 2041.91 }, golem: { usd: 0.260878 } },
body: { ethereum: { usd: ETH_USD }, golem: { usd: 0.260878 } },
statusCode: 200,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ const EarnRewardsCalculator: FC = () => {
formik.values.valueCrypto && formik.values.days && calculateRewards
? getFormattedEthValue(parseUnitsBigInt(calculateRewards.budget, 'wei'))
: {
fullString: '',
suffix: 'ETH',
value: '',
};
fullString: '',
suffix: 'ETH',
value: '',
};

const cryptoFiatRatio = cryptoValues?.ethereum[displayCurrency || 'usd'] || 1;
const fiat = estimatedFormattedRewardsValue.value
Expand Down

0 comments on commit 5c496b1

Please sign in to comment.