Skip to content

Commit

Permalink
Saleor 5021 add tests for customers (#1688)
Browse files Browse the repository at this point in the history
* customers

* all tests for customers

* change data-test attribute name

* add empty lines
  • Loading branch information
karola312 authored Jan 3, 2022
1 parent 3647681 commit 17968ef
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 36 deletions.
15 changes: 12 additions & 3 deletions cypress/elements/customer/customer-details.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
export const CUSTOMER_DETAILS = {
nameInput: '[name="customerFirstName"]',
lastNameInput: '[name="customerLastName"]',
nameInput: '[name="firstName"]',
customerAddressNameInput: '[name="customerFirstName"]',
lastNameInput: '[name="lastName"]',
customerAddressLastNameInput: '[name="customerLastName"]',
emailInput: '[name="email"]',
noteInput: '[name="note"]'
noteInput: '[name="note"]',
activeCheckbox: '[name="isActive"]',
menageAddressesButton: '[data-test-id="manageAddresses"]',
addAddressButton: '[data-test-id="add-address"]',
deleteAddressMenuItem: '[data-test-id="delete-address"]',
setAddressAsDefaultShipping: '[data-test-id="set-default-shipping-address"]',
setAddressAsDefaultBilling: '[data-test-id="set-default-billing-address"]',
editAddressMenuitem: '[data-test-id="edit-address"]'
};
2 changes: 1 addition & 1 deletion cypress/elements/shared/button-selectors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const BUTTON_SELECTORS = {
back: '[data-test-id="app-header-back-button"]',
submit: '[data-test="submit"]',
submit: '[data-test="submit"], [data-test-id="submit"]',
confirm: '[data-test="button-bar-confirm"]',
goBackButton: "[data-test-id='app-header-back-button']",
checkbox: "[type='checkbox']",
Expand Down
12 changes: 6 additions & 6 deletions cypress/fixtures/addresses.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
"secondUsAddress": {
"firstName": "test",
"lastName": "test",
"companyName": "Test2",
"streetAddress1": "Antonio Ridge",
"streetAddress2": "34930",
"city": "North Patrickfort",
"postalCode": "70958",
"companyName": "Test1",
"streetAddress1": "Rock Quarry Rd",
"streetAddress2": "345",
"city": "Afton",
"postalCode": "37745",
"country": "US",
"countryArea": "LA",
"countryArea": "TN",
"phone": "+12025550169",
"currency": "USD",
"countryFullName": "United States of America"
Expand Down
224 changes: 209 additions & 15 deletions cypress/integration/configuration/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,52 @@ import { CUSTOMER_DETAILS } from "../../elements/customer/customer-details";
import { CUSTOMERS_LIST } from "../../elements/customer/customers-list";
import { BUTTON_SELECTORS } from "../../elements/shared/button-selectors";
import { SHARED_ELEMENTS } from "../../elements/shared/sharedElements";
import { urlList } from "../../fixtures/urlList";
import { customerDetailsUrl, urlList } from "../../fixtures/urlList";
import { ONE_PERMISSION_USERS } from "../../fixtures/users";
import { getCustomer } from "../../support/api/requests/Customer";
import {
addressCreate,
createCustomer,
deleteCustomersStartsWith,
getCustomer
} from "../../support/api/requests/Customer";
import filterTests from "../../support/filterTests";

filterTests({ definedTags: ["all"] }, () => {
describe("Tests for customer", () => {
const channelStartsWith = `Customers`;
const startsWith = `Customers`;
let address;
let secondAddress;

before(() => {
cy.clearSessionData().loginUserViaRequest();
deleteCustomersStartsWith(startsWith);
cy.fixture("addresses").then(({ usAddress, secondUsAddress }) => {
address = usAddress;
secondAddress = secondUsAddress;
});
});

beforeEach(() => {
cy.clearSessionData().loginUserViaRequest();
});

it("should create customer", () => {
const randomName = `${channelStartsWith}${faker.datatype.number()}`;
const randomName = `${startsWith}${faker.datatype.number()}`;
const email = `${randomName}@example.com`;
const note = faker.lorem.paragraph();
let address;

cy.clearSessionData()
.loginUserViaRequest("auth", ONE_PERMISSION_USERS.user)
.visit(urlList.customers)
cy.visit(urlList.customers)
.get(CUSTOMERS_LIST.createCustomerButton)
.click()
.get(SHARED_ELEMENTS.progressBar)
.should("not.be.visible")
.get(CUSTOMER_DETAILS.nameInput)
.get(CUSTOMER_DETAILS.customerAddressNameInput)
.type(randomName)
.get(CUSTOMER_DETAILS.lastNameInput)
.get(CUSTOMER_DETAILS.customerAddressLastNameInput)
.type(randomName)
.get(CUSTOMER_DETAILS.emailInput)
.type(email)
.fixture("addresses")
.then(({ usAddress }) => {
address = usAddress;
cy.fillUpAddressForm(address);
})
.fillUpAddressForm(address)
.get(CUSTOMER_DETAILS.noteInput)
.type(note)
.addAliasToGraphRequest("CreateCustomer")
Expand All @@ -63,5 +76,186 @@ filterTests({ definedTags: ["all"] }, () => {
cy.expectCorrectFullAddress(customer.addresses[0], address);
});
});

it("should add address to customer", () => {
const randomName = `${startsWith}${faker.datatype.number()}`;
const email = `${randomName}@example.com`;

createCustomer(email, randomName).then(({ user }) => {
cy.visit(customerDetailsUrl(user.id))
.get(CUSTOMER_DETAILS.menageAddressesButton)
.click()
.get(CUSTOMER_DETAILS.addAddressButton)
.click()
.addAliasToGraphRequest("CreateCustomerAddress")
.fillUpAddressFormAndSubmit(secondAddress)
.waitForRequestAndCheckIfNoErrors("@CreateCustomerAddress");
getCustomer(user.id).then(({ addresses }) => {
expect(addresses).to.have.length(1);
cy.expectCorrectFullAddress(addresses[0], secondAddress);
});
});
});

it("should remove address from customer", () => {
const randomName = `${startsWith}${faker.datatype.number()}`;
const email = `${randomName}@example.com`;

createCustomer(email, randomName, address).then(({ user }) => {
cy.visit(customerDetailsUrl(user.id))
.get(CUSTOMER_DETAILS.menageAddressesButton)
.click()
.waitForProgressBarToNotExist()
.get(BUTTON_SELECTORS.showMoreButton)
.should("be.enabled")
.first()
.click()
.get(CUSTOMER_DETAILS.deleteAddressMenuItem)
.click()
.addAliasToGraphRequest("RemoveCustomerAddress")
.get(BUTTON_SELECTORS.submit)
.click()
.waitForRequestAndCheckIfNoErrors("@RemoveCustomerAddress");
getCustomer(user.id).then(({ addresses }) => {
expect(addresses).to.have.length(1);
});
});
});

it("should set address as default", () => {
const randomName = `${startsWith}${faker.datatype.number()}`;
const email = `${randomName}@example.com`;
let user;

createCustomer(email, randomName)
.then(({ user: userResp }) => {
user = userResp;
addressCreate(user.id, address);
})
.then(() => {
cy.visit(customerDetailsUrl(user.id))
.get(CUSTOMER_DETAILS.menageAddressesButton)
.click()
.waitForProgressBarToNotExist()
.get(BUTTON_SELECTORS.showMoreButton)
.should("be.enabled")
.click()
.addAliasToGraphRequest("SetCustomerDefaultAddress")
.get(CUSTOMER_DETAILS.setAddressAsDefaultShipping)
.click()
.wait("@SetCustomerDefaultAddress");
getCustomer(user.id);
})
.then(({ addresses }) => {
chai.softExpect(addresses[0].isDefaultShippingAddress).to.eq(true);
cy.get(BUTTON_SELECTORS.showMoreButton)
.should("be.enabled")
.click()
.addAliasToGraphRequest("SetCustomerDefaultAddress")
.get(CUSTOMER_DETAILS.setAddressAsDefaultBilling)
.click()
.wait("@SetCustomerDefaultAddress");
getCustomer(user.id);
})
.then(({ addresses }) => {
expect(addresses[0].isDefaultBillingAddress).to.be.true;
});
});

it("should update address", () => {
const randomName = `${startsWith}${faker.datatype.number()}`;
const email = `${randomName}@example.com`;

createCustomer(email, randomName, address).then(({ user }) => {
cy.visit(customerDetailsUrl(user.id))
.get(CUSTOMER_DETAILS.menageAddressesButton)
.click()
.get(BUTTON_SELECTORS.showMoreButton)
.should("be.enabled")
.first()
.click()
.get(CUSTOMER_DETAILS.editAddressMenuitem)
.click()
.addAliasToGraphRequest("UpdateCustomerAddress")
.fillUpAddressFormAndSubmit(secondAddress)
.waitForRequestAndCheckIfNoErrors("@UpdateCustomerAddress");
getCustomer(user.id).then(({ addresses }) => {
expect(addresses).to.have.length(2);
const addedAddress = addresses.find(
element =>
element.city.toUpperCase() === secondAddress.city.toUpperCase()
);
cy.expectCorrectFullAddress(addedAddress, secondAddress);
});
});
});

it("should delete customer", () => {
const randomName = `${startsWith}${faker.datatype.number()}`;
const email = `${randomName}@example.com`;

createCustomer(email, randomName, address).then(({ user }) => {
cy.visit(customerDetailsUrl(user.id))
.get(BUTTON_SELECTORS.deleteButton)
.click()
.addAliasToGraphRequest("RemoveCustomer")
.get(BUTTON_SELECTORS.submit)
.click()
.wait("@RemoveCustomer");
getCustomer(user.id).should("be.null");
});
});

it("should deactivate customer", () => {
const randomName = `${startsWith}${faker.datatype.number()}`;
const email = `${randomName}@example.com`;

createCustomer(email, randomName, address, true).then(({ user }) => {
cy.visit(customerDetailsUrl(user.id))
.get(CUSTOMER_DETAILS.activeCheckbox)
.click()
.addAliasToGraphRequest("UpdateCustomer")
.get(BUTTON_SELECTORS.confirm)
.click()
.wait("@UpdateCustomer");
getCustomer(user.id).then(({ isActive }) => {
expect(isActive).to.be.false;
});
});
});

it("should update customer", () => {
const randomName = `${startsWith}${faker.datatype.number()}`;
const updatedName = `${startsWith}UpdatedName`;
const email = `${randomName}@example.com`;

createCustomer(email, randomName, address, true).then(({ user }) => {
cy.visit(customerDetailsUrl(user.id))
.get(CUSTOMER_DETAILS.nameInput)
.clearAndType(updatedName)
.get(CUSTOMER_DETAILS.lastNameInput)
.clearAndType(updatedName)
.get(CUSTOMER_DETAILS.noteInput)
.clearAndType(updatedName)
.get(CUSTOMER_DETAILS.emailInput)
.clearAndType(`${updatedName}@example.com`)
.addAliasToGraphRequest("UpdateCustomer")
.get(BUTTON_SELECTORS.confirm)
.click()
.wait("@UpdateCustomer");
getCustomer(user.id).then(user => {
chai
.softExpect(user.firstName, "Expect correct first name")
.to.eq(updatedName);
chai
.softExpect(user.lastName, "Expect correct last name")
.to.eq(updatedName);
chai
.softExpect(user.email, "Expect correct email")
.to.eq(`${updatedName}@example.com`);
chai.softExpect(user.note, "Expect correct note").to.eq(updatedName);
});
});
});
});
});
32 changes: 28 additions & 4 deletions cypress/support/api/requests/Customer.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { getDefaultAddress } from "./utils/Utils";
import {
getDefaultAddress,
getDefaultAddressWithoutType,
getValueWithDefault
} from "./utils/Utils";

export function createCustomer(email, customerName, address, isActive = false) {
const addressesLines = getValueWithDefault(
address,
`${getDefaultAddress(address, "defaultBillingAddress")}
${getDefaultAddress(address, "defaultShippingAddress")}`
);
const mutation = `
mutation{
customerCreate(input:{
firstName: "${customerName}"
lastName: "${customerName}"
email: "${email}"
isActive: ${isActive}
${getDefaultAddress(address, "defaultBillingAddress")}
${getDefaultAddress(address, "defaultShippingAddress")}
${addressesLines}
}){
user{
id
Expand All @@ -21,7 +29,7 @@ export function createCustomer(email, customerName, address, isActive = false) {
}
}
}`;
return cy.sendRequestWithQuery(mutation);
return cy.sendRequestWithQuery(mutation).its("body.data.customerCreate");
}

export function deleteCustomersStartsWith(startsWith) {
Expand Down Expand Up @@ -143,8 +151,24 @@ export function getCustomer(customerId) {
}
countryArea
phone
isDefaultShippingAddress
isDefaultBillingAddress
}
}
}`;
return cy.sendRequestWithQuery(query).its("body.data.user");
}

export function addressCreate(userId, address) {
const mutation = `mutation{
addressCreate(userId:"${userId}" input:{
${getDefaultAddressWithoutType(address)}
}){
errors{
field
message
}
}
}`;
return cy.sendRequestWithQuery(mutation);
}
16 changes: 16 additions & 0 deletions cypress/support/api/requests/utils/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ export function getVariantsLines(variantsList, quantity) {
);
}

export function getDefaultAddressWithoutType(address, withName = true) {
const defaultAddress = `city: "${address.city}"
country: ${address.country}
countryArea: "${address.countryArea}"
phone: "${address.phone}"
postalCode: "${address.postalCode}"
streetAddress1: "${address.streetAddress1}"
streetAddress2: "${address.streetAddress2}"`;
if (withName) {
defaultAddress.concat(`firstName: "Test"
lastName: "Test"
companyName: "${address.companyName}"`);
}
return defaultAddress;
}

export function getVariantsIdsLines(variantsList) {
return variantsList.map(variant => `${variant.id}`);
}
Expand Down
4 changes: 2 additions & 2 deletions cypress/support/customCommands/softAssertions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ Cypress.Commands.add(
"expectCorrectBasicAddress",
(responseAddress, expectedAddress) => {
chai
.softExpect(responseAddress)
.to.have.property("city", expectedAddress.city.toUpperCase());
.softExpect(responseAddress.city.toUpperCase())
.to.eq(expectedAddress.city.toUpperCase());
chai
.softExpect(responseAddress)
.to.have.property("countryArea", expectedAddress.countryArea);
Expand Down
Loading

0 comments on commit 17968ef

Please sign in to comment.