|
|
import { Page } from 'playwright'; |
|
|
import { getCalypsoURL } from '../../data-helper'; |
|
|
import envVariables from '../../env-variables'; |
|
|
import { NavbarComponent } from './navbar-component'; |
|
|
|
|
|
type FocusType = 'Sites' | 'Sidebar'; |
|
|
|
|
|
const selectors = { |
|
|
sidebar: '.sidebar', |
|
|
sidebarNoticeButton: ( name: string ) => |
|
|
`.sidebar .current-site__notices button:text("${ name }")`, |
|
|
collapsedSidebar: '.is-sidebar-collapsed', |
|
|
focusedLayout: ( focus: FocusType ) => `.layout.focus-${ focus.toLowerCase() }`, |
|
|
|
|
|
|
|
|
linkWithText: ( text: string ) => `a:has-text("${ text }")`, |
|
|
planName: ':text-is("Upgrades"):visible .sidebar__inline-text', |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class SidebarComponent { |
|
|
private page: Page; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor( page: Page ) { |
|
|
this.page = page; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async waitForSidebarInitialization(): Promise< void > { |
|
|
const sidebarLocator = this.page.locator( selectors.sidebar ); |
|
|
|
|
|
await Promise.all( [ |
|
|
this.page.waitForLoadState( 'load', { timeout: 20 * 1000 } ), |
|
|
sidebarLocator.waitFor( { timeout: 20 * 1000 } ), |
|
|
] ); |
|
|
|
|
|
|
|
|
|
|
|
if ( await this.sidebarIsCollapsed() ) { |
|
|
const sidebarCollapseToggle = this.page.locator( selectors.linkWithText( 'Collapse menu' ) ); |
|
|
|
|
|
await Promise.all( [ |
|
|
this.page.waitForSelector( selectors.collapsedSidebar, { state: 'detached' } ), |
|
|
sidebarCollapseToggle.dispatchEvent( 'click' ), |
|
|
] ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async navigate( item: string, subitem?: string ): Promise< void > { |
|
|
await this.waitForSidebarInitialization(); |
|
|
|
|
|
if ( envVariables.VIEWPORT_NAME === 'mobile' ) { |
|
|
await this.openMobileSidebar(); |
|
|
} |
|
|
|
|
|
|
|
|
const itemSelector = `${ selectors.sidebar } :text-is("${ item }"):visible`; |
|
|
await this.page.dispatchEvent( itemSelector, 'click' ); |
|
|
|
|
|
|
|
|
if ( subitem ) { |
|
|
const subitemSelector = `.is-toggle-open :text-is("${ subitem }"):visible, .wp-menu-open .wp-submenu :text-is("${ subitem }"):visible`; |
|
|
await Promise.all( [ |
|
|
this.page.waitForNavigation( { timeout: 30 * 1000 } ), |
|
|
this.page.dispatchEvent( subitemSelector, 'click' ), |
|
|
] ); |
|
|
} |
|
|
|
|
|
const currentURL = this.page.url(); |
|
|
|
|
|
if ( ! currentURL.startsWith( getCalypsoURL() ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( currentURL.match( /\/(post|page|site-editor)\// ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let selectedMenuItem = `${ selectors.sidebar } .selected :text-is("${ item }")`; |
|
|
|
|
|
if ( subitem ) { |
|
|
selectedMenuItem = `${ selectors.sidebar } .selected :text-is("${ subitem }")`; |
|
|
} |
|
|
|
|
|
|
|
|
const locator = this.page.locator( selectedMenuItem ); |
|
|
await locator.waitFor( { state: 'attached' } ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async openNotice( noticeButtonName: string, expectedUrl?: string ): Promise< void > { |
|
|
await this.waitForSidebarInitialization(); |
|
|
|
|
|
if ( envVariables.VIEWPORT_NAME === 'mobile' ) { |
|
|
await this.openMobileSidebar(); |
|
|
} |
|
|
|
|
|
|
|
|
const itemSelector = selectors.sidebarNoticeButton( noticeButtonName ); |
|
|
await this.page.dispatchEvent( itemSelector, 'click' ); |
|
|
|
|
|
const currentURL = this.page.url(); |
|
|
|
|
|
if ( ! currentURL.startsWith( getCalypsoURL() ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( currentURL.match( /\/(post|page|site-editor)\// ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( expectedUrl ) { |
|
|
await this.page.waitForURL( expectedUrl ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async switchSite(): Promise< void > { |
|
|
await this.waitForSidebarInitialization(); |
|
|
|
|
|
if ( envVariables.VIEWPORT_NAME === 'mobile' ) { |
|
|
await this.openMobileSidebar(); |
|
|
} |
|
|
|
|
|
await this.page.click( selectors.linkWithText( 'Switch Site' ) ); |
|
|
await this.page.waitForSelector( selectors.focusedLayout( 'Sites' ) ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async addSite(): Promise< void > { |
|
|
await Promise.all( [ |
|
|
this.page.waitForNavigation(), |
|
|
this.page.click( selectors.linkWithText( 'Add New Site' ) ), |
|
|
] ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getCurrentPlanName(): Promise< string > { |
|
|
await this.waitForSidebarInitialization(); |
|
|
|
|
|
if ( envVariables.VIEWPORT_NAME === 'mobile' ) { |
|
|
if ( this.page.url().includes( getCalypsoURL( 'plans' ) ) ) { |
|
|
throw new Error( |
|
|
'Unable to retrieve current plan name on mobile sidebar.\nNavigate away from Upgrades > Plans page and try again.' |
|
|
); |
|
|
} |
|
|
|
|
|
await this.openMobileSidebar(); |
|
|
} |
|
|
|
|
|
const planNameLocator = this.page.locator( selectors.planName ); |
|
|
return await planNameLocator.innerText(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async sidebarIsCollapsed(): Promise< boolean > { |
|
|
const collapsedSidebarLocator = this.page.locator( selectors.collapsedSidebar ); |
|
|
return ( await collapsedSidebarLocator.count() ) > 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async openMobileSidebar(): Promise< void > { |
|
|
await this.waitForSidebarInitialization(); |
|
|
|
|
|
const navbarComponent = new NavbarComponent( this.page ); |
|
|
await navbarComponent.clickMobileMenu(); |
|
|
|
|
|
|
|
|
const layoutElement = await this.page.waitForSelector( selectors.focusedLayout( 'Sidebar' ) ); |
|
|
await layoutElement.waitForElementState( 'stable' ); |
|
|
} |
|
|
} |
|
|
|