File size: 3,883 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import { Page } from 'playwright';
/**
* A class to help manage social connections in the UI.
*/
export class SocialConnectionsManager {
/**
* Constructor to set the props
*/
constructor(
private page: Page,
private siteId: number
) {
// Ensure the methods are bound to the class.
this.isTargetUrl = this.isTargetUrl.bind( this );
}
/**
* Get the patterns for the social connections URLs
*/
get patterns() {
return {
CONNECTION_TESTS: new RegExp(
// The request that deals with connection test results on Simple sites
`wpcom/v2/(?<site_prefix>sites/${ this.siteId }/)?publicize/connections`
),
GET_POST: new RegExp(
// The request that gets the post data in the editor
`wp/v2/sites/${ this.siteId }/posts/[0-9]+`
),
POST_AUTOSAVES: new RegExp(
// The request that deals with post autosaves in the editor
`wp/v2/sites/${ this.siteId }/posts/[0-9]+/autosaves`
),
};
}
/**
* Get the test connections
*/
get testConnections() {
return [
{
connection_id: '123456',
display_name: 'Tumblr Test Connection',
service_name: 'tumblr',
},
{
connection_id: '654321',
display_name: 'Bluesky Test Connection',
service_name: 'bluesky',
},
];
}
/**
* Whether the URL is the one we want to intercept
*/
isTargetUrl( url: URL ) {
return (
this.isWpGetPostUrl( url ) ||
this.isSimpleConnectionTestUrl( url ) ||
this.isAtomicConnectionTestUrl( url )
);
}
/**
* Check if the URL is a WP get post URL
*/
isWpGetPostUrl( url: URL ) {
return (
this.patterns.GET_POST.test( url.pathname ) &&
// We don't want to intercept autosave requests.
! this.patterns.POST_AUTOSAVES.test( url.pathname )
);
}
/**
* Check if the URL is a connection test URL for simple sites
*/
isSimpleConnectionTestUrl( url: URL ) {
return Boolean(
url.searchParams.get( 'test_connections' ) === '1' &&
this.patterns.CONNECTION_TESTS.exec( url.pathname )?.groups?.site_prefix
);
}
/**
* Check if the URL is a connection test URL for atomic sites
*/
isAtomicConnectionTestUrl( url: URL ) {
const match = this.patterns.CONNECTION_TESTS.exec( url.pathname );
return Boolean(
url.searchParams.get( 'test_connections' ) === '1' &&
match &&
// Atomic sites don't have a site prefix in the URL.
! match.groups?.site_prefix
);
}
/**
* Intercept the requests and to modify the response
*/
async interceptRequests() {
await this.page.route( this.isTargetUrl, async ( route ) => {
// Fetch original response.
const response = await route.fetch();
const result = await response.json();
const url = new URL( route.request().url() );
if ( this.isWpGetPostUrl( url ) ) {
// For posts, add a test connection to post attributes.
result.body.jetpack_publicize_connections.push( ...this.testConnections );
} else if ( this.isSimpleConnectionTestUrl( url ) ) {
// For connection tests on Simple sites, add test connections to the body.
// Because the response is an object {"body":[],"status":200,"headers":{}}
result.body.push( ...this.testConnections );
} else if ( this.isAtomicConnectionTestUrl( url ) ) {
// For Atomic connection tests, add test connections directly to the result.
// because the response is already an array.
result.push( ...this.testConnections );
}
await route.fulfill( {
response,
body: JSON.stringify( result ),
} );
} );
}
/**
* Clear the intercepts
*/
async clearIntercepts() {
await this.page.unroute( this.isTargetUrl );
}
/**
* Wait for the connection test results response.
*/
async waitForConnectionTests() {
await this.page.waitForResponse( ( response ) => {
const url = new URL( response.url() );
return this.isSimpleConnectionTestUrl( url ) || this.isAtomicConnectionTestUrl( url );
} );
}
}
|