Skip to content

Commit

Permalink
Add e2e tests for the currency switcher widget (#7822)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarcosta99 authored Dec 6, 2023
1 parent 3fdc62b commit ab16d2f
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 8 deletions.
4 changes: 4 additions & 0 deletions changelog/e2e-7349-currency-switcher-widget
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Add e2e tests for the currency switcher widget.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
/**
* External dependencies
*/
const { merchant } = require( '@woocommerce/e2e-utils' );
const { merchant, WP_ADMIN_DASHBOARD } = require( '@woocommerce/e2e-utils' );

/**
* Internal dependencies
*/
import { merchantWCP, takeScreenshot } from '../../../utils';
import { merchantWCP, takeScreenshot, uiLoaded } from '../../../utils';

describe( 'Admin Multi-Currency', () => {
let wasMulticurrencyEnabled;

beforeAll( async () => {
await merchant.login();
wasMulticurrencyEnabled = await merchantWCP.activateMulticurrency();
} );

afterAll( async () => {
if ( ! wasMulticurrencyEnabled ) {
await merchantWCP.deactivateMulticurrency();
}
await merchant.logout();
} );

it( 'page should load without any errors', async () => {
Expand All @@ -20,4 +30,31 @@ describe( 'Admin Multi-Currency', () => {
} );
await takeScreenshot( 'merchant-admin-multi-currency' );
} );

it( 'should be possible to add the currency switcher to the sidebar', async () => {
await merchantWCP.addMulticurrencyWidget();
} );

it( 'should be possible to add the currency switcher to a post/page', async () => {
await page.goto( `${ WP_ADMIN_DASHBOARD }post-new.php` );
await uiLoaded();

const closeWelcomeModal = await page.$( 'button[aria-label="Close"]' );
if ( closeWelcomeModal ) {
await closeWelcomeModal.click();
}

await page.click( 'button[aria-label="Add block"]' );

const searchInput = await page.waitForSelector(
'input.components-search-control__input'
);
searchInput.type( 'switcher', { delay: 20 } );

await page.waitForSelector( 'button[role="option"]' );
await expect( page ).toMatchElement( 'button[role="option"]', {
text: 'Currency Switcher Block',
} );
await page.waitFor( 1000 );
} );
} );
180 changes: 180 additions & 0 deletions tests/e2e/specs/wcpay/shopper/shopper-multi-currency-widget.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/**
* External dependencies
*/
const { merchant, shopper } = require( '@woocommerce/e2e-utils' );

/**
* Internal dependencies
*/
import config from 'config';
import { merchantWCP, shopperWCP } from '../../../utils';
import { fillCardDetails, setupProductCheckout } from '../../../utils/payments';

const PAY_FOR_ORDER_LINK_SELECTOR = '.wc-order-status a';
const pagesTable = [
[
'home page',
async () => {
await shopper.goToShop();
},
'shop',
],
[
'product page',
async () => {
await page.click( 'li.product > a > img[src*="beanie"' );
},
'product/beanie',
],
[
'cart',
async () => {
await shopperWCP.addToCartBySlug( 'beanie' );
await shopper.goToCart();
},
'cart',
],
[
'checkout',
async () => {
await setupProductCheckout(
config.get( 'addresses.customer.billing' )
);
},
'checkout',
],
];

describe( 'Shopper Multi-Currency widget', () => {
let wasMulticurrencyEnabled;

beforeAll( async () => {
await merchant.login();
wasMulticurrencyEnabled = await merchantWCP.activateMulticurrency();
await merchantWCP.addCurrency( 'EUR' );
} );

afterAll( async () => {
if ( ! wasMulticurrencyEnabled ) {
await merchant.login();
await merchantWCP.deactivateMulticurrency();
await merchant.logout();
}
} );

it( 'should display currency switcher widget if multi-currency is enabled', async () => {
await merchantWCP.addMulticurrencyWidget();
await shopper.goToShop();
await page.waitForSelector( '.widget select[name=currency]', {
visible: true,
timeout: 5000,
} );
} );

it( 'should not display currency switcher widget if multi-currency is disabled', async () => {
await merchantWCP.openWCPSettings();
await merchantWCP.deactivateMulticurrency();
await shopper.goToShop();

const currencySwitcher = await page.$(
'.widget select[name=currency]'
);
expect( currencySwitcher ).toBeNull();

// Activate it again for the other tests.
await merchantWCP.activateMulticurrency();
await merchant.logout();
} );

describe.each( pagesTable )(
'Should allow shopper to switch currency',
( pageName, setupTest, url ) => {
it( `at the ${ pageName }`, async () => {
await setupTest();
await page.waitForSelector( '.widget select[name=currency]', {
visible: true,
} );
await page.select( '.widget select[name=currency]', 'EUR' );
await expect( page.url() ).toContain(
`${ url }/?currency=EUR`
);
await page.waitForSelector(
'.widget select[name=currency] option[value=EUR][selected]'
);
// Change it back to USD for the other tests.
await page.select( '.widget select[name=currency]', 'USD' );
} );
}
);

it( 'should not affect prices when currency switching on My account > Orders', async () => {
await shopper.login();
await page.select( '.widget select[name=currency]', 'USD' );
await setupProductCheckout(
config.get( 'addresses.customer.billing' )
);
await fillCardDetails( page, config.get( 'cards.basic' ) );
await shopper.placeOrder();
await expect( page ).toMatch( 'Order received' );

const orderId = await page.evaluate(
() => document.querySelector( 'li.order strong' ).innerText
);
const orderTotal = Number(
await page.evaluate( () =>
document
.querySelector( 'li.total strong' )
.innerText.replace( /[^\d.]/g, '' )
)
);

await shopperWCP.goToOrders();
await page.select( '.widget select[name=currency]', 'EUR' );
await page.waitForSelector(
'.widget select[name=currency] option[value=EUR][selected]'
);
await expect( page ).toMatch( `#${ orderId }` );
await expect( page ).toMatch( `${ orderTotal.toFixed( 2 ) } USD` );
} );

it( 'should not affect prices when currency switching at the order received page', async () => {
await page.select( '.widget select[name=currency]', 'USD' );
await setupProductCheckout(
config.get( 'addresses.customer.billing' )
);
await fillCardDetails( page, config.get( 'cards.basic' ) );
await shopper.placeOrder();
await expect( page ).toMatch( 'Order received' );

const orderId = await page.evaluate(
() => document.querySelector( 'li.order strong' ).innerText
);
const orderTotal = Number(
await page.evaluate( () =>
document
.querySelector( 'li.total strong' )
.innerText.replace( /[^\d.]/g, '' )
)
);

await page.select( '.widget select[name=currency]', 'EUR' );
await page.waitForSelector(
'.widget select[name=currency] option[value=EUR][selected]'
);
await expect( page ).toMatch( `${ orderId }` );
await expect( page ).toMatch( `${ orderTotal.toFixed( 2 ) } USD` );
await page.select( '.widget select[name=currency]', 'USD' );
await shopper.logout();
} );

it( 'should not display currency switcher on pay for order page', async () => {
await merchant.login();
await merchantWCP.createPayForOrder();
await page.click( PAY_FOR_ORDER_LINK_SELECTOR );

const currencySwitcher = await page.$(
'.widget select[name=currency]'
);
expect( currencySwitcher ).toBeNull();
} );
} );
60 changes: 60 additions & 0 deletions tests/e2e/utils/flows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
clearAndFillInput,
setCheckbox,
SHOP_PAGE,
WP_ADMIN_DASHBOARD,
} = require( '@woocommerce/e2e-utils' );
const {
fillCardDetails,
Expand Down Expand Up @@ -832,4 +833,63 @@ export const merchantWCP = {
}
return wasInitiallyEnabled;
},

addMulticurrencyWidget: async () => {
await page.goto( `${ WP_ADMIN_DASHBOARD }widgets.php`, {
waitUntil: 'networkidle0',
} );
await uiLoaded();

const closeWelcomeModal = await page.$( 'button[aria-label="Close"]' );
if ( closeWelcomeModal ) {
await closeWelcomeModal.click();
}

const isWidgetAdded = await page.$(
'.wp-block iframe[srcdoc*=\'name="currency"\']'
);
if ( ! isWidgetAdded ) {
await page.click( 'button[aria-label="Add block"]' );

const searchInput = await page.waitForSelector(
'input.components-search-control__input'
);
searchInput.type( 'switcher', { delay: 20 } );

await page.waitForSelector(
'button.components-button[role="option"]'
);
await page.click( 'button.components-button[role="option"]' );
await page.waitFor( 2000 );
await page.waitForSelector(
'.edit-widgets-header .edit-widgets-header__actions button.is-primary'
);
await page.click(
'.edit-widgets-header .edit-widgets-header__actions button.is-primary'
);
await expect( page ).toMatchElement( '.components-snackbar', {
text: 'Widgets saved.',
timeout: 15000,
} );
}
},
createPayForOrder: async () => {
await merchant.openNewOrder();
await page.click( 'button.add-line-item' );
await page.click( 'button.add-order-item' );
await page.click( 'select.wc-product-search' );
await page.type(
'.select2-search--dropdown > input',
config.get( 'products.simple.name' ),
{
delay: 20,
}
);
await page.waitFor( 2000 );
await page.click( '.select2-results .select2-results__option' );
await page.click( '#btn-ok' );
await page.waitFor( 2000 );
await page.click( 'button.save_order' );
await page.waitForNavigation( { waitUntil: 'networkidle0' } );
},
};

0 comments on commit ab16d2f

Please sign in to comment.