Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Remove promise from shopping-cart-widget-component.js and add async/await #1153

Merged
merged 3 commits into from
Apr 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 20 additions & 84 deletions lib/components/shopping-cart-widget-component.js
Original file line number Diff line number Diff line change
@@ -1,105 +1,41 @@
/** @format */

import webdriver, { By as by } from 'selenium-webdriver';
import { By as by } from 'selenium-webdriver';
import * as driverHelper from '../driver-helper.js';

import BaseContainer from '../base-container.js';

export default class ShoppingCartWidgetComponent extends BaseContainer {
constructor( driver ) {
super( driver, by.className( 'cart-toggle-button' ) );
super( driver, by.css( '.cart-toggle-button' ) );
}

open() {
let d = webdriver.promise.defer();
const driver = this.driver;
const explicitWait = this.explicitWait;

this.displayedOpen().then(
function cartAlreadyOpen() {
d.fulfill( true );
},
function cartClosed() {
driverHelper
.clickWhenClickable( driver, by.css( '.cart-toggle-button' ), explicitWait )
.then(
function success() {
d.fulfill( true );
},
function failure() {
d.reject( 'Unable to find Shopping Cart Widget button' );
}
);
}
);

return d.promise;
}

displayedOpen() {
let d = webdriver.promise.defer();

driverHelper.isElementPresent( this.driver, by.css( 'cart-body' ) ).then(
function( present ) {
if ( present ) {
d.fulfill( true );
} else {
d.reject( 'Cart not open' );
}
},
function() {
d.reject( 'Cart not open' );
}
async open() {
return await driverHelper.clickWhenClickable(
this.driver,
by.css( '.cart-toggle-button' ),
this.explicitWaitMS
);

return d.promise;
}

removeItem( self ) {
let d = webdriver.promise.defer();

driverHelper
.isElementPresent( this.driver, by.css( '.cart-empty' ) )
.then( function( cartEmpty ) {
if ( ! cartEmpty ) {
driverHelper.clickWhenClickable( self.driver, by.css( '.cart__remove-item' ) );
}
} );

return d.promise;
async removeItem( self ) {
let cartEmpty = await driverHelper.isElementPresent( this.driver, by.css( '.cart-empty' ) );
if ( ! cartEmpty ) {
return await driverHelper.clickWhenClickable( self.driver, by.css( '.cart__remove-item' ) );
}
}

empty() {
let d = webdriver.promise.defer();
async empty() {
let self = this;
const cartBadgeSelector = by.css( '.cart__count-badge' );

driverHelper.isElementPresent( this.driver, cartBadgeSelector ).then( function( present ) {
if ( present ) {
self.open().then( function() {
self.driver
.findElement( cartBadgeSelector )
.getText()
.then( function( numItems ) {
let flow = self.driver.controlFlow();
let promiseArray = [];
for ( let i = 0; i < numItems; i++ ) {
promiseArray.push(
flow.execute( function() {
self.removeItem( self );
} )
);
}
webdriver.promise.all( promiseArray ).then( function() {
d.fulfill( true );
} );
} );
} );
} else {
d.fulfill( true );
let present = await driverHelper.isElementPresent( self.driver, cartBadgeSelector );
if ( present ) {
await self.open();
let numItems = await self.driver.findElement( cartBadgeSelector ).getText();
for ( let i = 0; i < numItems; i++ ) {
await self.removeItem( self );
}
} );

return d.promise;
}
}
}