From 2bd0b6b8051c80cfc47dbcd61430839f9d17fa76 Mon Sep 17 00:00:00 2001 From: Vinai Kopp Date: Wed, 14 Sep 2022 09:16:36 +0200 Subject: [PATCH] Bundled product hyva tests (#80) * Wait for price update without fixed timeout * Add bundled product spec for hyva --- cypress/fixtures/hyva/product.json | 2 + .../integration/hyva/catalog/bundle.spec.js | 74 +++++++++++++++++++ .../integration/hyva/catalog/product.spec.js | 3 +- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 cypress/integration/hyva/catalog/bundle.spec.js diff --git a/cypress/fixtures/hyva/product.json b/cypress/fixtures/hyva/product.json index d1eb9ab..7d7e4fc 100644 --- a/cypress/fixtures/hyva/product.json +++ b/cypress/fixtures/hyva/product.json @@ -3,6 +3,8 @@ "categoryUrl": "/women/tops-women.html", "configurableProductName": "Breathe-Easy Tank", "configurableProductUrl": "/breathe-easy-tank.html", + "bundledProductName": "Sprite Yoga Companion Kit", + "bundledProductUrl": "/sprite-yoga-companion-kit.html", "couponCode": "H20", "couponProductUrl": "/affirm-water-bottle.html", "currency": "$", diff --git a/cypress/integration/hyva/catalog/bundle.spec.js b/cypress/integration/hyva/catalog/bundle.spec.js new file mode 100644 index 0000000..3ba5724 --- /dev/null +++ b/cypress/integration/hyva/catalog/bundle.spec.js @@ -0,0 +1,74 @@ +import product from "../../../fixtures/hyva/product.json"; +import selectors from "../../../fixtures/hyva/selectors/product.json"; +import homepageSelectors from "../../../fixtures/hyva/selectors/homepage.json"; + +describe('Bundle products test suite', () => { + beforeEach(() => { + cy.visit(product.bundledProductUrl); + }); + it('Can render the product name', () => { + cy.get(selectors.mainHeading) + .should('contain.text', product.bundledProductName) + .should('be.visible'); + }) + it('Can set the price to zero when every associated product qty is zero', () => { + cy.get('input[id$=-qty-input]').each(input => { + cy.wrap(input).type('{selectall}0').blur(); + cy.wait(0); // wait for alpine to process change event + }) + cy.get('.bundle-info .final-price').first().should('contain.text', '$0.00') + }) + it('Can calculate the price based on selected options', () => { + // sum up the price of all first options + const prices = []; + cy.get('.product-info-main fieldset .control').then(associatedProductOptions => { + associatedProductOptions.map((idx, product) => { + const firstOptionPrice = product.querySelector('.price-notice'); + const m = firstOptionPrice.innerText.trim().match(/(?[+-]?)\s+\S(?[\d.]+)/) + m && prices.push((m.groups.sign === '+' ? 1 : -1) * parseFloat(m.groups.price)) + + }) + }) + // set qty for each associated product to 1 + cy.get('input[id$=-qty-input]').each(input => { + cy.wrap(input).type('{selectall}1').blur(); + cy.wait(0); // wait for alpine to process change event + }) + cy.get('#bundleSummary .final-price').first().then(finalPrice => { + cy.wrap(finalPrice).should('contain.text', `$${(prices.reduce((sum, n) => sum + n, 0))}`) + }) + }) + it('Can display selection quantities', () => { + let expectedNames = []; + cy.get('.product-info-main fieldset > div > label').then(associatedProductNames => { + expectedNames = associatedProductNames.map((idx, productName) => productName.innerText.trim()); + }) + // set associated product qty to 1, 2, 3... + cy.get('input[id$=-qty-input]').each((input, idx) => { + cy.wrap(input).type(`{selectall}${idx + 1}`).blur(); + cy.wait(0); // wait for alpine to process change event + }) + + // check the order of associated product names in the summary matches the expected names + cy.get('#bundleSummary .bundle.items li > span').each((actual, idx) => { + expect(actual.text()).to.eq(expectedNames[idx]); + }) + + // check the associated product qty in the summary is 1, 2, 3... + cy.get('#bundleSummary .bundle.items li > div > span:first-child').each((actualQty, idx) => { + expect(actualQty.text()).to.eq(`${idx + 1}`); + }) + }) + it('Can add a bundled product to the cart', () => { + cy.get('input[id$=-qty-input]').each(input => { + cy.wrap(input).type('{selectall}1').blur(); + cy.wait(0); // wait for alpine to process change event + }) + cy.get(selectors.addToCartButton).click(); + cy.get(homepageSelectors.successMessage).contains( + `You added ${product.bundledProductName} to your shopping cart.` + ); + cy.get(selectors.cartIconProductCount).invoke('text').should('not.eq', '') // wait for product count to update + cy.get(selectors.cartIconProductCount).invoke('text').then(parseFloat).should('be.gte', 1); + }) +}) diff --git a/cypress/integration/hyva/catalog/product.spec.js b/cypress/integration/hyva/catalog/product.spec.js index f1a665c..ab5e8f1 100644 --- a/cypress/integration/hyva/catalog/product.spec.js +++ b/cypress/integration/hyva/catalog/product.spec.js @@ -36,8 +36,7 @@ describe('Simple Product test suite', () => { cy.get(homepageSelectors.successMessage).contains( `You added ${product.simpleProductName} to your shopping cart.` ); - // Requires a wait for the product count to update - cy.wait(1000); + cy.get(selectors.cartIconProductCount).invoke('text').should('not.eq', '') // wait for product count to update cy.get(selectors.cartIconProductCount) .invoke('text') .then(parseFloat)