From ad949841391e5e3899329c75990a69fb0d16e308 Mon Sep 17 00:00:00 2001 From: chutingy Date: Tue, 3 Dec 2024 15:26:26 -0600 Subject: [PATCH] Add all required context --- .../tests/e2eTests/events/add-to-cart.spec.js | 52 ++++++------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/cypress/src/tests/e2eTests/events/add-to-cart.spec.js b/cypress/src/tests/e2eTests/events/add-to-cart.spec.js index cdddacaf5..47cccc2b2 100644 --- a/cypress/src/tests/e2eTests/events/add-to-cart.spec.js +++ b/cypress/src/tests/e2eTests/events/add-to-cart.spec.js @@ -1,51 +1,33 @@ import { products } from "../../../fixtures"; +import { expectsEventWithContext } from "../../../assertions"; /** * https://github.com/adobe/commerce-events/blob/main/examples/events/add-to-cart.md * - * Required Contexts: page, storefront, shoppingCart + * Required Contexts: page, storefront, product, shoppingCart, changedProducts */ it("is sent on add to cart button click", () => { cy.visit(products.configurable.urlPathWithOptions); + // add to cart + cy.get(".product-details__buttons__add-to-cart button") + .should("be.visible") + .click(); + cy.waitForResource("commerce-events-collector.js").then(() => { cy.window() .its("adobeDataLayer") .then((adobeDataLayer) => { - const pageContextIndex = adobeDataLayer.findIndex( - (event) => !!event?.pageContext - ); - const storefrontInstanceContextIndex = adobeDataLayer.findIndex( - (event) => !!event?.storefrontInstanceContext + expectsEventWithContext( + "add-to-cart", + [ + "pageContext", + "storefrontInstanceContext", + "productContext", + "shoppingCartContext", + "changedProductsContext", + ], + adobeDataLayer ); - // page and storefront pushed before spy - expect(pageContextIndex).to.be.greaterThan(-1); - expect(storefrontInstanceContextIndex).to.be.greaterThan(-1); }); }); - cy.waitForResource("commerce-events-collector.js").then(() => { - cy.window().then((win) => { - cy.spy(win.adobeDataLayer, "push").as("adl"); - // add to cart - cy.get(".product-details__buttons__add-to-cart button") - .should("be.visible") - .click() - .then(() => { - cy.window() - .its("adobeDataLayer") - .then(() => { - cy.get("@adl", { timeout: 1000 }).should((adobeDataLayerPush) => { - const targetEventIndex = adobeDataLayerPush.args.findIndex( - (event) => event[0]?.event === "add-to-cart" - ); - const shoppingCartContextIndex = - adobeDataLayerPush.args.findIndex( - (event) => !!event[0]?.shoppingCartContext - ); - expect(targetEventIndex).to.be.greaterThan(-1); - expect(shoppingCartContextIndex).to.be.greaterThan(-1); - }); - }); - }); - }); - }); });