File: D:/HostingSpaces/SBogers10/shop.komma.nl/tests/Cypress/integration/site/cart.spec.js
/// <reference types="Cypress" />
import PagesInfo from "../../support/pagesInfo";
const pagesInfo = new PagesInfo();
describe('Cart', function() {
describe('Shopping cart item', function() {
it('Quantity control can be manipulated', function() {
cy.clearCookies();
cy.request('GET', '/testapi/v1/cart/addToCart', {
items: [
{'productable_type' : 'product', 'id' : 2, 'quantity': 2}, //Hammer
]
}).then(function(response) {
cy.request('GET', '/checkout/information').then(function(response) {
let firstCartItem = response.body.items[0];
cy.visit(pagesInfo.getByName('cart').locations.nl)
//By keyboard
cy.get('[data-test="cart-items"] > [data-test="cart-item"]:first-child [data-test="whole"]').should('have.value', firstCartItem.quantity.toString(10));
cy.get('[data-test="cart-items"] > [data-test="cart-item"]:first-child [data-test="whole"]').type('{backspace}' + (firstCartItem.quantity + 1).toString(10));
cy.get('[data-test="cart-items"] > [data-test="cart-item"]:first-child [data-test="whole"]').should('have.value', (firstCartItem.quantity + 1).toString(10));
cy.get('[data-test="cart-items"] > [data-test="cart-item"]:first-child [data-test="whole"]').type('{backspace}1');
cy.get('[data-test="cart-items"] > [data-test="cart-item"]:first-child [data-test="whole"]').should('have.value', '1')
//By clicking on the buttons
cy.get('[data-test="cart-items"] > [data-test="cart-item"]:first-child [data-test="whole-up"]').click()
cy.get('[data-test="cart-items"] > [data-test="cart-item"]:first-child [data-test="whole"]').should('have.value', '2')
cy.get('[data-test="cart-items"] > [data-test="cart-item"]:first-child [data-test="whole-down"]').click()
cy.get('[data-test="cart-items"] > [data-test="cart-item"]:first-child [data-test="whole"]').should('have.value', '1')
})
})
});
it('Price is shown inc vat', function() {
cy.clearCookies();
cy.request('GET', '/testapi/v1/cart/addToCart', {
items: [
{'productable_type' : 'product', 'id' : 2, 'quantity': 2}, //Hammer
]
}).then(function(response) {
cy.request('GET', '/checkout/information').then(function(response) {
cy.visit(pagesInfo.getByName('cart').locations.nl)
response.body.items.forEach(function(cartItem, index) {
cy.get('[data-test="cart-items"] [data-test="cart-item-price"]').eq(index).should('have.text', cartItem.price_inc_formatted.toString(10));
})
})
})
});
it('Can be removed, and an "cart is empty" message is shown', function() {
cy.clearCookies();
cy.request('GET', '/testapi/v1/cart/addToCart', {
items: [
{'productable_type' : 'product', 'id' : 2, 'quantity': 2}, //Hammer
{'productable_type' : 'product', 'id' : 4, 'quantity': 1}, //Handsaw
{'productable_type' : 'product', 'id' : 8, 'quantity': 1}, //Dry aged tomahawk
{'productable_type' : 'product_group', 'id' : 2, 'quantity': 1}, //Led set lamp tripod concept
]
}).then(function(response) {
cy.request('GET', '/checkout/information').then(function(response) {
cy.visit(pagesInfo.getByName('cart').locations.nl)
//Empty cart message and continue shopping button should not be visible
cy.get('[data-test="empty-cart-message"]').should('not.be.visible')
cy.get('[data-test="continue-shopping"]').should('not.be.visible')
//The cart should have the items that where randomly created
cy.get('[data-test="cart-items"] > [data-test="cart-item"]').should('have.length', response.body.items.length)
//Lets delete all the items one by one and check that they are removed.
cy.get('[data-test="cart-items"] [data-test="cart-item-remove"]').each(function(item, index) {
cy.wrap(item).click();
cy.log('Initially there where ' + response.body.items.length + ' items. Current index: ' + index + '. Current items count: ' + response.body.items.length)
cy.get('[data-test="cart-items"]').children().should('have.length', response.body.items.length - index - 1)
})
//Check that we dont have any items in the cart
cy.get('[data-test="cart-items"] > [data-test="cart-item"]').should('have.length', 0)
//Check that we see a message that tells is that the cart is empty and that we can continue shopping.
cy.get('[data-test="empty-cart-message"]').should('be.visible')
cy.get('[data-test="continue-shopping"]').should('be.visible')
})
})
});
});
describe('"Send to" select', function() {
it('Shows shipping cost for a country that has costs defined', function() {
cy.clearCookies();
cy.request('GET', '/testapi/v1/cart/addToCart', {
items: [
{'productable_type' : 'product', 'id' : 2, 'quantity': 2}, //Hammer
{'productable_type' : 'product', 'id' : 8, 'quantity': 1}, //Dry aged tomahawk
]
}) //This request is ran first...
//...then this one is ran. In cypress request run synchronously instead of async.
cy.request('GET', '/testapi/v1/shipping_costs/index').then(function(shippingCostsResponse) {
let definedShippingCosts = shippingCostsResponse.body.data;
//Get the checkout information BEFORE we change the shipping country, so we can compare the data after the change
cy.request('GET', '/checkout/information').then(function(checkoutInformationResponseBeforeChange) {
cy.visit(pagesInfo.getByName('cart').locations.nl)
//Get all options that are not disabled
cy.get('[data-test="shipping-country"] option:not([disabled])').then(options => {
//Get all options that have shipping costs and do not have the default shipping costs
const nonDefaultOptionsWithShippingCosts = options.toArray().filter(option => {
const optionsDefinedShippingCost = definedShippingCosts.filter((shippingCost) => option.value === shippingCost.code).shift();
return (optionsDefinedShippingCost && !optionsDefinedShippingCost.default);
})
//Select the first select OPTION that has shipping costs but isn't the default one
cy.get('[data-test="shipping-country"]').select(nonDefaultOptionsWithShippingCosts[0].value);
//get the shipping costs that are not marked as default. This must be exactly one of course.
let nonDefaultShippingCosts = shippingCostsResponse.body.data.filter(shippingCosts => !shippingCosts.default)[0]
//Lets see if we can see the non default shipping costs on the screen
cy.get('[data-test="shipping-country"]').select(nonDefaultOptionsWithShippingCosts[0].value)
cy.get('[data-test="shipping-costs"]').should('have.text', nonDefaultShippingCosts.price_inc_formatted);
//Check if the information on the screen did change after we did change the shipping country
cy.request('GET', '/checkout/information').then(function(checkoutInformationResponseAfterChange) {
//Check that the new checkout information values are on the screen...
cy.contains(checkoutInformationResponseAfterChange.body.subTotalIncFormatted).should('exist')
cy.contains(checkoutInformationResponseAfterChange.body.totalFormatted).should('exist')
cy.contains(checkoutInformationResponseAfterChange.body.shippingCosts.price_inc_formatted).should('exist')
//And then compare them with the checkout information BEFORE the change to verify them
expect(checkoutInformationResponseAfterChange.body.subTotalIncFormatted).to.be.equal(checkoutInformationResponseBeforeChange.body.subTotalIncFormatted)
expect(checkoutInformationResponseAfterChange.body.totalFormatted).not.to.be.equal(checkoutInformationResponseBeforeChange.body.totalFormatted)
expect(checkoutInformationResponseAfterChange.body.shippingCosts.price_inc_formatted).not.to.be.equal(checkoutInformationResponseBeforeChange.body.shippingCosts.price_inc_formatted)
})
})
});
});
});
it('Shows default shipping cost for a country that has no costs defined', function() {
cy.clearCookies();
cy.request('GET', '/testapi/v1/cart/addToCart', {
items: [
{'productable_type' : 'product', 'id' : 2, 'quantity': 2}, //Hammer
]
}) //This request is ran first...
//...then this one is ran. In cypress request run synchronously instead of async.
cy.request('GET', '/testapi/v1/shipping_costs/index').then(function(shippingCostsResponse) {
cy.request('GET', '/checkout/information').then(function(checkoutInformationResponseBeforeChange) {
cy.visit(pagesInfo.getByName('cart').locations.nl)
//Create a list of ISO_3 codes for the countries that have shipping costs
let definedShippingCostCountries = shippingCostsResponse.body.data.map(shippingCost => shippingCost.code);
//Get all non disabled shipping cost options. The disabled one if the "Select an country" one.
cy.get('[data-test="shipping-country"] option:not([disabled])').then(options => {
const optionsWithoutShippingCosts = options.toArray().filter(option => definedShippingCostCountries.indexOf(option.value) === -1)
cy.get('[data-test="shipping-country"]').select(optionsWithoutShippingCosts[0].value);
//Get the default shipping costs.
const countryIso3WithNoShippingCosts = optionsWithoutShippingCosts[0];
let defaultShippingCosts = shippingCostsResponse.body.data.filter(shippingCosts => shippingCosts.default)[0];
//Lets see if we can see it on the screen
cy.get('[data-test="shipping-country"]').select(countryIso3WithNoShippingCosts.value)
cy.get('[data-test="shipping-costs"]').should('have.text', defaultShippingCosts.price_inc_formatted);
})
//Check if the information on the screen did nont change after we did change the shipping country to a country which also has default shipping costs
cy.request('GET', '/checkout/information').then(function(checkoutInformationResponseAfterChange) {
//Check that the new checkout information values are on the screen...
cy.contains(checkoutInformationResponseAfterChange.body.subTotalIncFormatted).should('exist')
cy.contains(checkoutInformationResponseAfterChange.body.totalFormatted).should('exist')
cy.contains(checkoutInformationResponseAfterChange.body.shippingCosts.price_inc_formatted).should('exist')
//And then compare them with the checkout information BEFORE the change to verify them
expect(checkoutInformationResponseAfterChange.body.subTotalIncFormatted).to.be.equal(checkoutInformationResponseBeforeChange.body.subTotalIncFormatted)
expect(checkoutInformationResponseAfterChange.body.totalFormatted).to.be.equal(checkoutInformationResponseBeforeChange.body.totalFormatted)
expect(checkoutInformationResponseAfterChange.body.shippingCosts.price_inc_formatted).to.be.equal(checkoutInformationResponseBeforeChange.body.shippingCosts.price_inc_formatted)
})
})
});
});
});
describe('"Order" button', function() {
it('should navigate to the "customer details" page', function() {
cy.clearCookies();
cy.request('GET', '/testapi/v1/cart/addToCart', {
items: [
{'productable_type' : 'product', 'id' : 2, 'quantity': 2}, //Hammer
]
}) //This request is ran first...
//...then this one is ran. In cypress request run synchronously instead of async.
cy.visit(pagesInfo.getByName('cart').locations.nl)
cy.url().should('contain', pagesInfo.getByName('cart').locations.nl)
cy.get('[data-test="checkout"]').click();
cy.url().should('contain', pagesInfo.getByName('customer_details').locations.nl)
})
});
});