From 1958336acc49609b3c88f7d488aab088282ff033 Mon Sep 17 00:00:00 2001 From: chutingy Date: Mon, 2 Dec 2024 14:23:19 -0600 Subject: [PATCH 1/3] Add add-to-cart event test --- .../tests/e2eTests/events/add-to-cart.spec.js | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 cypress/src/tests/e2eTests/events/add-to-cart.spec.js diff --git a/cypress/src/tests/e2eTests/events/add-to-cart.spec.js b/cypress/src/tests/e2eTests/events/add-to-cart.spec.js new file mode 100644 index 0000000000..22d0ff27ae --- /dev/null +++ b/cypress/src/tests/e2eTests/events/add-to-cart.spec.js @@ -0,0 +1,52 @@ +import { products } from "../../../fixtures"; +import { expectsEventWithContext } from "../../../assertions"; +/** + * https://github.com/adobe/commerce-events/blob/main/examples/events/initiate-checkout.md + * + * Required Contexts: page, storefront, shoppingCart + */ + +it("is sent on add to cart button click", () => { + cy.visit(products.configurable.urlPathWithOptions); + 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 + ); + // 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); + }); + }); + }); + }); + }); +}); From b50fa6054bd0aae29ea35865571b736d05903f2e Mon Sep 17 00:00:00 2001 From: chutingy Date: Tue, 3 Dec 2024 11:45:40 -0600 Subject: [PATCH 2/3] update --- cypress/src/tests/e2eTests/events/add-to-cart.spec.js | 3 +-- 1 file changed, 1 insertion(+), 2 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 22d0ff27ae..cdddacaf5e 100644 --- a/cypress/src/tests/e2eTests/events/add-to-cart.spec.js +++ b/cypress/src/tests/e2eTests/events/add-to-cart.spec.js @@ -1,7 +1,6 @@ import { products } from "../../../fixtures"; -import { expectsEventWithContext } from "../../../assertions"; /** - * https://github.com/adobe/commerce-events/blob/main/examples/events/initiate-checkout.md + * https://github.com/adobe/commerce-events/blob/main/examples/events/add-to-cart.md * * Required Contexts: page, storefront, shoppingCart */ From ad949841391e5e3899329c75990a69fb0d16e308 Mon Sep 17 00:00:00 2001 From: chutingy Date: Tue, 3 Dec 2024 15:26:26 -0600 Subject: [PATCH 3/3] 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 cdddacaf5e..47cccc2b2a 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); - }); - }); - }); - }); - }); });