Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API PULL] Add E2E tests for Notifications Grant Access #2474

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
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
136 changes: 136 additions & 0 deletions tests/e2e/specs/notifications.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* External dependencies
*/
import { expect, test } from '@playwright/test';

/**
* Internal dependencies
*/
import SettingsPage from '../utils/pages/settings';
import { clearOnboardedMerchant, setOnboardedMerchant } from '../utils/api';
import { LOAD_STATE } from '../utils/constants';

test.use( { storageState: process.env.ADMINSTATE } );

test.describe.configure( { mode: 'serial' } );

/**
* @type {import('../utils/pages/settings.js').default } settingsPage
*/
let settingsPage = null;

/**
* @type {import('@playwright/test').Page} page
*/
let page = null;

test.describe( 'Notifications Feature', () => {
test.beforeAll( async ( { browser } ) => {
page = await browser.newPage();
settingsPage = new SettingsPage( page );
await setOnboardedMerchant();
await Promise.all( [
// Mock Jetpack as connected
settingsPage.mockJetpackConnected(),

// Mock google as connected.
settingsPage.mockGoogleConnected(),
] );

settingsPage.goto();
} );

test.afterAll( async () => {
await clearOnboardedMerchant();
await page.close();
} );

test( 'Grant Access button is not visible on Settings page when notifications service is disabled', async () => {
const button = settingsPage.getGrantAccessBtn();
await expect( button ).not.toBeVisible();
} );

test( 'Grant Access button is visible on Settings page when notifications service is enabled', async () => {
// Mock Merchant Center as connected
settingsPage.mockMCConnected( 1234, true );
const button = settingsPage.getGrantAccessBtn();

await expect( button ).toBeVisible();
} );

test( 'When click on Grant Access button redirect to Auth page', async () => {
const mockAuthURL = 'https://example.com';
// Mock Merchant Center as connected
settingsPage.mockMCConnected( 1234, true );
settingsPage.fulfillRESTApiAuthorize( { auth_url: mockAuthURL } );
const button = settingsPage.getGrantAccessBtn();

button.click();
await page.waitForLoadState( LOAD_STATE.DOM_CONTENT_LOADED );
await page.waitForURL( mockAuthURL );
expect( page.url() ).toMatch( mockAuthURL );
} );

test( 'When REST API is Approved it shows a success notice in MC and allows to disable it', async () => {
settingsPage.goto();
settingsPage.mockMCConnected( 1234, true, 'approved' );
const grantedAccessMessage = page
.locator( '#woocommerce-layout__primary' )
.getByText(
'Google has been granted access to fetch your product data.'
);
await expect( grantedAccessMessage ).toBeVisible();

const disableDataFetchButton = page.getByRole( 'button', {
name: 'Disable product data fetch',
exact: true,
} );
const modalConfirmBtn = page.getByRole( 'button', {
name: 'Disable data fetching',
exact: true,
} );
const modalDismissBtn = page.getByRole( 'button', {
name: 'Never mind',
exact: true,
} );
const modalCheck = page.getByRole( 'checkbox', {
name: 'Yes, I want to disable the data fetching feature.',
exact: true,
} );

await expect( disableDataFetchButton ).toBeVisible();
disableDataFetchButton.click();

await expect( modalConfirmBtn ).toBeDisabled();
await expect( modalDismissBtn ).toBeEnabled();
await expect( modalCheck ).toBeVisible();
modalCheck.check();
await expect( modalConfirmBtn ).toBeEnabled();
modalConfirmBtn.click();
await page.waitForLoadState( LOAD_STATE.DOM_CONTENT_LOADED );
await page.waitForURL( /path=%2Fgoogle%2Fsettings/ );
await expect( modalConfirmBtn ).not.toBeVisible();
} );

test( 'When REST API is Error it shows a waring notice in MC and allows to grant access', async () => {
settingsPage.goto();
settingsPage.mockMCConnected( 1234, true, 'error' );
const mockAuthURL = 'https://example.com';
settingsPage.fulfillRESTApiAuthorize( { auth_url: mockAuthURL } );
const errorAccessMessage = page
.locator( '#woocommerce-layout__primary' )
.getByText(
'There was an issue granting access to Google for fetching your products.'
);
const grantAccessBtn = page.getByRole( 'button', {
name: 'Grant access',
exact: true,
} );
await expect( errorAccessMessage ).toBeVisible();
await expect( grantAccessBtn ).toBeVisible();
grantAccessBtn.click();
await page.waitForLoadState( LOAD_STATE.DOM_CONTENT_LOADED );
await page.waitForURL( mockAuthURL );
expect( page.url() ).toMatch( mockAuthURL );
} );
} );
26 changes: 25 additions & 1 deletion tests/e2e/utils/mock-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,19 @@ export default class MockRequests {
* Mock MC as connected.
*
* @param {number} id
* @param {boolean} notificationServiceEnabled
* @param {null|'approved'|'error'|'dissaproved'} wpcomRestApiStatus
*/
async mockMCConnected( id = 1234 ) {
async mockMCConnected(
id = 1234,
notificationServiceEnabled = false,
wpcomRestApiStatus = null
) {
await this.fulfillMCConnection( {
id,
status: 'connected',
notification_service_enabled: notificationServiceEnabled,
wpcom_rest_api_status: wpcomRestApiStatus,
} );
}

Expand Down Expand Up @@ -707,4 +715,20 @@ export default class MockRequests {
message: 'Successfully synchronized settings with Google.',
} );
}

/**
* Fulfill the REST API Authorize request.
*
* @param {Object} payload
* @param {Array} methods
* @return {Promise<void>}
*/
async fulfillRESTApiAuthorize( payload, methods = [] ) {
await this.fulfillRequest(
/\/wc\/gla\/rest-api\/authorize\b/,
payload,
200,
methods
);
}
}
48 changes: 48 additions & 0 deletions tests/e2e/utils/pages/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Internal dependencies
*/
import MockRequests from '../mock-requests';
import { LOAD_STATE } from '../constants';

export default class SettingsPage extends MockRequests {
/**
* @param {import('@playwright/test').Page} page
*/
constructor( page ) {
super( page );
this.page = page;
}

/**
* Close the Settings page.
*
* @return {Promise<void>}
*/
async closePage() {
await this.page.close();
}

/**
* Go to the Settings page.
*
* @return {Promise<void>}
*/
async goto() {
await this.page.goto(
'/wp-admin/admin.php?page=wc-admin&path=%2Fgoogle%2Fsettings',
{ waitUntil: LOAD_STATE.DOM_CONTENT_LOADED }
);
}

/**
* Get the Grant Access Button.
*
* @return {Promise<import('@playwright/test').Locator>} The Grant Access Button
*/
getGrantAccessBtn() {
return this.page.getByRole( 'button', {
name: 'Get early access',
exact: true,
} );
}
}