|
|
import { BlockFlow, EditorContext, PublishedPostContext } from '.'; |
|
|
|
|
|
interface ConfigurationData { |
|
|
embedUrl: string; |
|
|
expectedTweetText: string; |
|
|
} |
|
|
|
|
|
const blockParentSelector = '[aria-label*="Block: Twitter"]:has-text("Twitter")'; |
|
|
const selectors = { |
|
|
embedUrlInput: `${ blockParentSelector } input`, |
|
|
embedButton: `${ blockParentSelector } button:has-text("Embed")`, |
|
|
editorTwitterIframe: 'iframe[title="Embedded content from twitter.com"]', |
|
|
publishedTwitterIframe: 'iframe[title="X Post"]', |
|
|
publishedTwitterBareLink: |
|
|
'figure.wp-block-embed-twitter > div.wp-block-embed__wrapper > a[href^="https://twitter.com/automattic/"]', |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class TwitterBlockFlow implements BlockFlow { |
|
|
private configurationData: ConfigurationData; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor( configurationData: ConfigurationData ) { |
|
|
this.configurationData = configurationData; |
|
|
} |
|
|
|
|
|
blockSidebarName = 'Twitter Embed'; |
|
|
blockEditorSelector = blockParentSelector; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async configure( context: EditorContext ): Promise< void > { |
|
|
const editorCanvas = await context.editorPage.getEditorCanvas(); |
|
|
|
|
|
const urlInputLocator = editorCanvas.locator( selectors.embedUrlInput ); |
|
|
await urlInputLocator.fill( this.configurationData.embedUrl ); |
|
|
|
|
|
const embedButtonLocator = editorCanvas.locator( selectors.embedButton ); |
|
|
await embedButtonLocator.click(); |
|
|
|
|
|
|
|
|
const twitterIframeLocator = editorCanvas.locator( selectors.editorTwitterIframe ); |
|
|
await twitterIframeLocator.waitFor(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async validateAfterPublish( context: PublishedPostContext ): Promise< void > { |
|
|
|
|
|
const iframeTweetLocator = context.page |
|
|
.frameLocator( selectors.publishedTwitterIframe ) |
|
|
.locator( `text=${ this.configurationData.expectedTweetText }` ) |
|
|
.first(); |
|
|
|
|
|
|
|
|
const bareTweetLinkLocator = context.page.locator( selectors.publishedTwitterBareLink ).first(); |
|
|
|
|
|
await Promise.any( [ iframeTweetLocator.waitFor(), bareTweetLinkLocator.waitFor() ] ); |
|
|
} |
|
|
} |
|
|
|