Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
fpbrault committed Nov 13, 2024
1 parent c64c6ba commit 82eae79
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 97 deletions.
68 changes: 29 additions & 39 deletions packages/samples/headless-ssr-commerce/e2e/cart/cart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,43 @@ let initialItemPrice: number;
let initialCartTotal: number;

test('should display the cart', async ({cart}) => {
const cartSection = await cart.getCart();
const cartSection = cart.cart;
await expect(cartSection).toBeVisible();
});

test.describe('when adding a new item to the cart', () => {
test.beforeEach(async ({page, cart}) => {
await page.goto('/toys');

await (await cart.getAddToCartButton()).first().click();
await cart.addToCartButton.first().click();
});

test('should add the item to the cart', async ({cart, page}) => {
await page.goto('/cart');

const cartItemsCount = await (await cart.getItems()).count();
const cartItemsCount = await cart.items.count();

expect(cartItemsCount).toBe(4);
});
});

test.describe('when increasing the quantity of an item', () => {
test.beforeEach(async ({cart}) => {
const item = (await cart.getItems()).first();
const item = cart.items.first();

initialItemQuantity = parseInt(
(await (await cart.getItemQuantity(item)).textContent()) || ''
);
initialItemPrice = parseInt(
(await (await cart.getItemPrice(item)).textContent()) || ''
);
initialCartTotal = parseInt(
(await (await cart.getTotal()).textContent()) || ''
);
initialCartTotal = parseInt((await cart.total.textContent()) || '');

await (await cart.getAddOneButton()).first().click();
await cart.addOneButton.first().click();
});

test('should increase the quantity', async ({cart}) => {
const item = (await cart.getItems()).first();
const item = cart.items.first();

const quantity = parseInt(
(await (await cart.getItemQuantity(item)).textContent()) || ''
Expand All @@ -86,7 +84,7 @@ test.describe('when increasing the quantity of an item', () => {
});

test('should increase the total price', async ({cart}) => {
const item = (await cart.getItems()).first();
const item = cart.items.first();

const totalPrice = parseInt(
(await (await cart.getItemTotalPrice(item)).textContent()) || ''
Expand All @@ -96,7 +94,7 @@ test.describe('when increasing the quantity of an item', () => {
});

test('should increase the cart total', async ({cart}) => {
const total = parseInt((await (await cart.getTotal()).textContent()) || '');
const total = parseInt((await cart.total.textContent()) || '');

expect(total).toBe(initialCartTotal + initialItemPrice);
});
Expand All @@ -107,25 +105,23 @@ test.describe('when decreasing the quantity of an item', () => {
let initialCartItemsCount: number;

test.beforeEach(async ({cart}) => {
const item = (await cart.getItems()).nth(1);
const item = cart.items.nth(1);

initialItemQuantity = parseInt(
(await (await cart.getItemQuantity(item)).textContent()) || ''
);
initialItemPrice = parseInt(
(await (await cart.getItemPrice(item)).textContent()) || ''
);
initialCartTotal = parseInt(
(await (await cart.getTotal()).textContent()) || ''
);
initialCartTotal = parseInt((await cart.total.textContent()) || '');

initialCartItemsCount = await (await cart.getItems()).count();
initialCartItemsCount = await cart.items.count();

await (await cart.getRemoveOneButton()).nth(1).click();
await cart.removeOneButton.nth(1).click();
});

test('should decrease the quantity', async ({cart}) => {
const item = (await cart.getItems()).nth(1);
const item = cart.items.nth(1);

const quantity = parseInt(
(await (await cart.getItemQuantity(item)).textContent()) || ''
Expand All @@ -135,7 +131,7 @@ test.describe('when decreasing the quantity of an item', () => {
});

test('should decrease the total price', async ({cart}) => {
const item = (await cart.getItems()).nth(1);
const item = cart.items.nth(1);

const totalPrice = parseInt(
(await (await cart.getItemTotalPrice(item)).textContent()) || ''
Expand All @@ -145,15 +141,13 @@ test.describe('when decreasing the quantity of an item', () => {
});

test('should decrease the cart total', async ({cart}) => {
const total = parseInt(
(await (await cart.getTotal()).textContent()) || ''
);
const total = parseInt((await cart.total.textContent()) || '');

expect(total).toBe(initialCartTotal - initialItemPrice);
});

test('should not remove the item', async ({cart}) => {
const cartItemsCount = await (await cart.getItems()).count();
const cartItemsCount = await cart.items.count();

expect(cartItemsCount).toBe(initialCartItemsCount);
});
Expand All @@ -163,33 +157,29 @@ test.describe('when decreasing the quantity of an item', () => {
let initialCartItemsCount: number;

test.beforeEach(async ({cart}) => {
const item = (await cart.getItems()).first();
const item = cart.items.first();

initialItemQuantity = parseInt(
(await (await cart.getItemQuantity(item)).textContent()) || ''
);
initialItemPrice = parseInt(
(await (await cart.getItemPrice(item)).textContent()) || ''
);
initialCartTotal = parseInt(
(await (await cart.getTotal()).textContent()) || ''
);
initialCartTotal = parseInt((await cart.total.textContent()) || '');

initialCartItemsCount = await (await cart.getItems()).count();
initialCartItemsCount = await cart.items.count();

await (await cart.getRemoveOneButton()).first().click();
await cart.removeOneButton.first().click();
});

test('should remove the item', async ({cart}) => {
const cartItemsCount = await (await cart.getItems()).count();
const cartItemsCount = await cart.items.count();

expect(cartItemsCount).toBe(initialCartItemsCount - 1);
});

test('should decrease the cart total', async ({cart}) => {
const total = parseInt(
(await (await cart.getTotal()).textContent()) || ''
);
const total = parseInt((await cart.total.textContent()) || '');

expect(total).toBe(initialCartTotal - initialItemPrice * 1);
});
Expand All @@ -199,31 +189,31 @@ test.describe('when decreasing the quantity of an item', () => {
test.describe('when clicking the remove all button on an item', () => {
let initialCartItemsCount: number;
test.beforeEach(async ({cart}) => {
initialCartItemsCount = await (await cart.getItems()).count();
initialCartItemsCount = await cart.items.count();

await (await cart.getRemoveAllButton()).first().click();
await cart.removeAllButton.first().click();
});

test('should remove the item', async ({cart}) => {
const cartItemsCount = await (await cart.getItems()).count();
const cartItemsCount = await cart.items.count();

expect(cartItemsCount).toBe(initialCartItemsCount - 1);
});
});

test.describe('when clicking the empty cart button', () => {
test.beforeEach(async ({cart}) => {
await (await cart.getEmptyCartButton()).click();
await cart.emptyCartButton.click();
});

test('should remove all items', async ({cart}) => {
const cartItemsCount = await (await cart.getItems()).count();
const cartItemsCount = await cart.items.count();

expect(cartItemsCount).toBe(0);
});

test('should set the cart total to 0', async ({cart}) => {
const total = parseInt((await (await cart.getTotal()).textContent()) || '');
const total = parseInt((await cart.total.textContent()) || '');

expect(total).toBe(0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ test.beforeEach(async ({page}) => {
});

test('should load and display the search box', async ({search}) => {
await expect(await search.searchBox()).toBeVisible();
await expect(search.searchBox).toBeVisible();
});

test.describe('when entering a query', () => {
test.beforeEach(async ({search}) => {
const searchBox = await search.searchBox();
const searchBox = search.searchBox;
await searchBox.fill('shoes');
});

test('should display suggestions', async ({search}) => {
const suggestionsContainer = await search.getSuggestionsContainer();
const suggestionsContainer = search.suggestionsContainer;
await expect(suggestionsContainer).toBeVisible();

const suggestions = await search.getSuggestions();
const suggestions = search.suggestions;
expect(await suggestions.count()).toBeGreaterThan(0);
});

test.describe('when clicking a suggestion', () => {
let suggestionValue: string;
test.beforeEach(async ({search}) => {
const suggestions = await search.getSuggestions();
const suggestions = search.suggestions;
suggestionValue =
(await suggestions.first().textContent()) || 'no value found';
await suggestions.first().click();
Expand All @@ -42,7 +42,7 @@ test.describe('when entering a query', () => {

test.describe('when clicking search button', () => {
test.beforeEach(async ({search}) => {
(await search.getSearchButton()).click();
search.searchButton.click();
});

test('should go to search page', async ({page}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export class CartPageObject {
this.page = page;
}

async getCart() {
get cart() {
return this.page.locator('div:has(> button:has-text("Purchase"))');
}

async getItems() {
const cart = await this.getCart();
get items() {
const cart = this.cart;
return cart.locator('ul > li');
}

Expand All @@ -28,31 +28,31 @@ export class CartPageObject {
return item.locator('p').nth(3).locator('span').nth(2);
}

async getAddToCartButton() {
get addToCartButton() {
return this.page.getByRole('button', {name: 'Add to cart'});
}

async getAddOneButton() {
get addOneButton() {
return this.page.getByRole('button', {name: 'Add one'});
}

async getRemoveOneButton() {
get removeOneButton() {
return this.page.getByRole('button', {name: 'Remove one'});
}

async getRemoveAllButton() {
get removeAllButton() {
return this.page.getByRole('button', {name: 'Remove all'});
}

async getPurchaseButton() {
get purchaseButton() {
return this.page.getByRole('button', {name: 'Purchase'});
}

async getEmptyCartButton() {
get emptyCartButton() {
return this.page.getByRole('button', {name: 'Empty cart'});
}

async getTotal() {
return (await this.getCart()).locator('p').last().locator('span').nth(2);
get total() {
return this.cart.locator('p').last().locator('span').nth(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export class FacetPageObject {
constructor(page: Page) {
this.page = page;
}
async getFacetsSection() {
get facetsSection() {
return this.page.locator('.Facets');
}

async getFirstFacet() {
get firstFacet() {
return this.page.getByLabel(/(Select|Deselect) facet value.*/).first();
}

async getFacetLoading() {
get facetLoading() {
return this.page.locator('.FacetLoading').first();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ export class SearchPageObject {
this.page = page;
}

async searchBox() {
get searchBox() {
return this.page.getByPlaceholder('search');
}

async getSearchButton() {
get searchButton() {
return this.page.getByRole('button', {name: 'Search', exact: true});
}

async getSuggestionsContainer() {
get suggestionsContainer() {
return this.page.getByText('Suggestions :');
}

async getSuggestions() {
get suggestions() {
return this.page.getByText('Suggestions :').locator('li >> button');
}

async getProductList() {
get productList() {
return this.page.getByLabel('Product List');
}

async getProductItems() {
const productList = await this.getProductList();
get productItems() {
const productList = this.productList;
return productList.getByRole('listitem').all();
}

async getResultSummary() {
get resultSummary() {
return this.page.getByText(/Showing results \d+ - \d+ of/);
}
}
Loading

0 comments on commit 82eae79

Please sign in to comment.