Skip to content

Commit

Permalink
Add all required context
Browse files Browse the repository at this point in the history
  • Loading branch information
cctina516 committed Dec 3, 2024
1 parent 59d54a1 commit ad94984
Showing 1 changed file with 17 additions and 35 deletions.
52 changes: 17 additions & 35 deletions cypress/src/tests/e2eTests/events/add-to-cart.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
});
});
});

0 comments on commit ad94984

Please sign in to comment.