Home > front end >  What testing approach should I use?
What testing approach should I use?

Time:07-14

I am testing a very complex quoting tool where I need to test discount margin price and discount price. Lets say price in catalog is 100$ And a partner is getting 25% discount, then partner price is 75$

Should I test like this -> cy.get(partnerPriceField)/should('have.value', 75)

OR -> cy.get(partner PriceField)/should('have.value', catalogPrice * (1-discount/100))

CodePudding user response:

I would suggest you go with the second option because in that case if in future the catalog price or discount value changes, you don't have to change the logic because it's generic. Having said that, for the second option to work you have to first change the catalogPrice and discount to numbers then after calculation, you have to convert the final answer to a string and then do the final assertion, something like this:

cy.get('partner PriceField').should(
  'have.value',
  ( catalogPrice * (1 -  discount / 100)).toString()
)

This is how your calculation looks like:

JS calculation

Also in case, if your catalogPrice is coming as 100$ and discount is coming as 25%, just add .replace(/\D/g,'') to remove % and $. Then in this case your assertion will look like this:

cy.get('partner PriceField').should(
  'have.value',
  (
     catalogPrice.replace(/\D/g, '') *
    (1 -  discount.replace(/\D/g, '') / 100)
  ).toString()
)

Your calculation would look like this:

calculation 2

CodePudding user response:

You can pass a regex with .contains() and followed by an assertion to check the value is visible.

const fullPrice = 100
const discount = .25
const discountPrice = fullPrice * discount
const regexMatcher = new RegExp(`$${discountPrice}`) // equals /\$75/

cy.contains('.selector-containing-price-text', regexMatcher)
  .should('be.visible')

CodePudding user response:

cy.get(partnerPriceField).should('have.value', 75) is preferred for e2e tests.

The job of testing the calculation falls to unit tests, which are close to the source code so test can be changed if/when the src calculation changes.

The job of e2e test is to make sure that given some input data the page shows the output data (black box).

Preferably input and output are supplied in a fixture table that's easily adjusted when the calculation is revised.

  • Related