Skip to content

Commit

Permalink
packages/calypso-e2e/src/rest-api-client.ts
Browse files Browse the repository at this point in the history
- implement new method to see if there are posts on the site.

test/e2e/specs/tools/advertising__promote.ts
- stop creating new post unnecessarily
  • Loading branch information
worldomonation committed Sep 11, 2023
1 parent 114d746 commit 19b9206
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
28 changes: 28 additions & 0 deletions packages/calypso-e2e/src/rest-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type {
JetpackSearchResponse,
JetpackSearchParams,
Subscriber,
SitePostState,
} from './types';
import type { BodyInit, HeadersInit, RequestInit } from 'node-fetch';

Expand Down Expand Up @@ -635,6 +636,33 @@ export class RestAPIClient {

/* Posts */

/**
* Given a siteID, checks whether any posts exists of a given state.
*
* @param {number} siteID Site ID.
* @param param1 Keyed object parameter.
* @param {SitePostState} param1.state State of the published post.
*/
async siteHasPost(
siteID: number,
{ state = 'publish' }: { state: SitePostState }
): Promise< boolean > {
const params: RequestParams = {
method: 'get',
headers: {
Authorization: await this.getAuthorizationHeader( 'bearer' ),
'Content-Type': this.getContentTypeHeader( 'json' ),
},
};

const response = await this.sendRequest(
this.getRequestURL( '1.1', `/sites/${ siteID }/post-counts/post` ),
params
);

return response.counts.all[ state ] !== 0 ? true : false;
}

/**
* Creates a post on the site.
*
Expand Down
2 changes: 2 additions & 0 deletions packages/calypso-e2e/src/types/rest-api-client.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* Parameter Interfaces */

export type SitePostState = 'publish' | 'draft' | 'private';

export interface AccountDetails {
userID: number;
username: string;
Expand Down
26 changes: 19 additions & 7 deletions test/e2e/specs/tools/advertising__promote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,23 @@ skipDescribeIf( envVariables.ATOMIC_VARIATION === 'private' )(
const accountName = getTestAccountByFeature( envToFeatureKey( envVariables ) );
testAccount = new TestAccount( accountName );

// Createa a new test post before starting the test, to ensure at least one
// available post.
restAPIClient = new RestAPIClient( testAccount.credentials );
newPostDetails = await restAPIClient.createPost(

// Createa a new test post before starting the test if site has no published post.
const hasPosts = await restAPIClient.siteHasPost(
testAccount.credentials.testSites?.primary.id as number,
{
title: pageTitle,
}
{ state: 'publish' }
);

if ( ! hasPosts ) {
newPostDetails = await restAPIClient.createPost(
testAccount.credentials.testSites?.primary.id as number,
{
title: pageTitle,
}
);
}

await testAccount.authenticate( page );

advertisingPage = new AdvertisingPage( page );
Expand All @@ -75,11 +82,12 @@ skipDescribeIf( envVariables.ATOMIC_VARIATION === 'private' )(
} );

it( 'Click on Promote for the first post', async function () {
await advertisingPage.clickButtonByNameOnRow( 'Promote', { postTitle: pageTitle } );
await advertisingPage.clickButtonByNameOnRow( 'Promote', { row: 1 } );
} );

it( 'Land in Blaze campaign landing page', async function () {
await page.waitForURL( /advertising/ );

blazeCampaignPage = new BlazeCampaignPage( page );
} );

Expand All @@ -102,6 +110,10 @@ skipDescribeIf( envVariables.ATOMIC_VARIATION === 'private' )(
} );

afterAll( async function () {
if ( ! newPostDetails ) {
return;
}

await restAPIClient.deletePost(
testAccount.credentials.testSites?.primary.id as number,
newPostDetails.ID
Expand Down

0 comments on commit 19b9206

Please sign in to comment.