From 37a01bb1d85927550f6d3dec5380c0e750fa7a94 Mon Sep 17 00:00:00 2001 From: Miquel Angel Coca Piza Date: Thu, 12 Dec 2024 17:57:23 +0100 Subject: [PATCH] Added Cypress test to check the ACDL place-order event --- .../tests/e2eTests/events/place-order.spec.js | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 cypress/src/tests/e2eTests/events/place-order.spec.js diff --git a/cypress/src/tests/e2eTests/events/place-order.spec.js b/cypress/src/tests/e2eTests/events/place-order.spec.js new file mode 100644 index 000000000..5e6018a52 --- /dev/null +++ b/cypress/src/tests/e2eTests/events/place-order.spec.js @@ -0,0 +1,70 @@ +import { + placeOrder, + setGuestEmail, + setGuestShippingAddress, +} from "../../../actions"; +import { expectsEventWithContext } from "../../../assertions"; +import { customerShippingAddress, products } from "../../../fixtures"; + +/** + * https://github.com/adobe/commerce-events/blob/main/examples/events/place-order.md + * + * Required Contexts: + * - page -> https://github.com/adobe/commerce-events/blob/main/packages/storefront-events-sdk/src/types/schemas/page.ts, + * - storefront -> https://github.com/adobe/commerce-events/blob/main/packages/storefront-events-sdk/src/types/schemas/storefrontInstance.ts, + * - shoppingCart -> https://github.com/adobe/commerce-events/blob/main/packages/storefront-events-sdk/src/types/schemas/shoppingCart.ts, + * - order -> https://github.com/adobe/commerce-events/blob/main/packages/storefront-events-sdk/src/types/schemas/order.ts + */ + +it("is sent on place order button click", () => { + // add item to cart + cy.visit(products.configurable.urlPathWithOptions); + // add to cart + cy.get(".product-details__buttons__add-to-cart button") + .should("be.visible") + .click(); + // click the minicart toggle + cy.get('button[data-count="1"]').should("be.visible").click(); + // click the checkout button + cy.get('#nav div.cart-mini-cart a[href="/checkout"]') + .should("be.visible") + .click(); + + // fill in the login form + const apiMethod = "setGuestEmailOnCart"; + const urlTest = Cypress.env("graphqlEndPoint"); + cy.intercept("POST", urlTest, (req) => { + let data = req.body.query; + if (data && typeof data == "string") { + if (data.includes(apiMethod)) { + req.alias = "setEmailOnCart"; + } + } + }); + setGuestEmail(customerShippingAddress.email); + cy.wait("@setEmailOnCart"); + // fill in the shipping address form + setGuestShippingAddress(customerShippingAddress, true); + cy.wait(2000); + // click the place order button + placeOrder(); + // wait until the URL includes '/order-details' + cy.url().should("include", "/order-details"); + + cy.waitForResource("commerce-events-collector.js").then(() => { + cy.window() + .its("adobeDataLayer") + .then((adobeDataLayer) => { + expectsEventWithContext( + "place-order", + [ + "pageContext", + "storefrontInstanceContext", + "shoppingCartContext", + "orderContext", + ], + adobeDataLayer + ); + }); + }); +});