-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(menusurface): Add
flipMenuHorizontally
property, add unit tests.
PiperOrigin-RevId: 465404408
- Loading branch information
1 parent
f8d950f
commit 884c3a2
Showing
6 changed files
with
209 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import './menu-surface'; | ||
import '../list/list-item'; | ||
|
||
import {Environment} from '@material/web/testing/environment'; | ||
import {listenOnce} from '@material/web/testing/events'; | ||
import {html} from 'lit'; | ||
import {styleMap} from 'lit/directives/style-map'; | ||
|
||
import {MdMenuSurface} from './menu-surface'; | ||
|
||
const ANCHOR_WIDTH = '100px'; | ||
const ANCHOR_HEIGHT = '50px'; | ||
|
||
describe('menu surface tests', () => { | ||
const env = new Environment(); | ||
let surface: MdMenuSurface; | ||
let root: HTMLElement; // Inner root element to which styling is applied. | ||
|
||
beforeEach(async () => { | ||
const el = env.render(getMenuSurfaceTemplate()); | ||
surface = el.querySelector('md-menu-surface')!; | ||
const button = el.querySelector('button')!; | ||
surface.anchor = button; | ||
await surface.updateComplete; | ||
root = surface.shadowRoot!.querySelector('.md3-menu-surface')!; | ||
}); | ||
|
||
describe('menu positioning options', () => { | ||
it('Corner.TOP_START positions menu correctly', async () => { | ||
surface.corner = 'TOP_START'; | ||
await surface.updateComplete; | ||
surface.show(); | ||
// TOOD(b/241244423): Waiting for the event fired can be removed when | ||
// this bug is fixed. | ||
await listenOnce(surface, 'opened'); | ||
await surface.updateComplete; | ||
|
||
expect(root.style.top).toBe('0px'); | ||
expect(root.style.left).toBe('0px'); | ||
}); | ||
|
||
it('Corner.TOP_END positions menu correctly', async () => { | ||
surface.corner = 'TOP_END'; | ||
await surface.updateComplete; | ||
surface.show(); | ||
// TOOD(b/241244423): Waiting for the event fired can be removed when | ||
// this bug is fixed. | ||
await listenOnce(surface, 'opened'); | ||
await surface.updateComplete; | ||
|
||
expect(root.style.top).toBe('0px'); | ||
expect(root.style.left).toBe(ANCHOR_WIDTH); | ||
}); | ||
|
||
it('Corner.BOTTOM_START positions menu correctly', async () => { | ||
surface.corner = 'BOTTOM_START'; | ||
await surface.updateComplete; | ||
surface.show(); | ||
// TOOD(b/241244423): Waiting for the event fired can be removed when | ||
// this bug is fixed. | ||
await listenOnce(surface, 'opened'); | ||
await surface.updateComplete; | ||
|
||
expect(root.style.top).toBe(ANCHOR_HEIGHT); | ||
expect(root.style.left).toBe('0px'); | ||
}); | ||
|
||
it('Corner.BOTTOM_END positions menu correctly', async () => { | ||
surface.corner = 'BOTTOM_END'; | ||
await surface.updateComplete; | ||
surface.show(); | ||
// TOOD(b/241244423): Waiting for the event fired can be removed when | ||
// this bug is fixed. | ||
await listenOnce(surface, 'opened'); | ||
await surface.updateComplete; | ||
|
||
expect(root.style.top).toBe(ANCHOR_HEIGHT); | ||
expect(root.style.left).toBe(ANCHOR_WIDTH); | ||
}); | ||
|
||
it('`flipMenuHorizontally` flips TOP_START corner', async () => { | ||
surface.corner = 'TOP_START'; | ||
surface.flipMenuHorizontally = true; | ||
await surface.updateComplete; | ||
surface.show(); | ||
// TOOD(b/241244423): Waiting for the event fired can be removed when | ||
// this bug is fixed. | ||
await listenOnce(surface, 'opened'); | ||
await surface.updateComplete; | ||
|
||
expect(root.style.top).toBe('0px'); | ||
expect(root.style.right).toBe('0px'); | ||
}); | ||
|
||
it('`flipMenuHorizontally` flips TOP_END corner', async () => { | ||
surface.corner = 'TOP_END'; | ||
surface.flipMenuHorizontally = true; | ||
await surface.updateComplete; | ||
surface.show(); | ||
// TOOD(b/241244423): Waiting for the event fired can be removed when | ||
// this bug is fixed. | ||
await listenOnce(surface, 'opened'); | ||
await surface.updateComplete; | ||
|
||
expect(root.style.top).toBe('0px'); | ||
expect(root.style.right).toBe(ANCHOR_WIDTH); | ||
}); | ||
|
||
it('`flipMenuHorizontally` flips BOTTOM_START corner', async () => { | ||
surface.corner = 'BOTTOM_START'; | ||
surface.flipMenuHorizontally = true; | ||
await surface.updateComplete; | ||
surface.show(); | ||
// TOOD(b/241244423): Waiting for the event fired can be removed when | ||
// this bug is fixed. | ||
await listenOnce(surface, 'opened'); | ||
await surface.updateComplete; | ||
|
||
expect(root.style.top).toBe(ANCHOR_HEIGHT); | ||
expect(root.style.right).toBe('0px'); | ||
}); | ||
|
||
it('`flipMenuHorizontally` flips BOTTOM_END corner', async () => { | ||
surface.corner = 'BOTTOM_END'; | ||
surface.flipMenuHorizontally = true; | ||
await surface.updateComplete; | ||
surface.show(); | ||
// TOOD(b/241244423): Waiting for the event fired can be removed when | ||
// this bug is fixed. | ||
await listenOnce(surface, 'opened'); | ||
await surface.updateComplete; | ||
|
||
expect(root.style.top).toBe(ANCHOR_HEIGHT); | ||
expect(root.style.right).toBe(ANCHOR_WIDTH); | ||
}); | ||
}); | ||
}); | ||
|
||
function getMenuSurfaceTemplate(propsInit: Partial<MdMenuSurface> = {}) { | ||
return html` | ||
<div class="root" style="position: relative; margin: 100px;"> | ||
<button style="${styleMap({ | ||
width: ANCHOR_WIDTH, | ||
height: ANCHOR_HEIGHT | ||
})}"> | ||
Open Menu | ||
</button> | ||
<md-menu-surface .quick="${propsInit.quick ?? true}" | ||
.corner="${propsInit.corner ?? 'BOTTOM_START'}" | ||
.flipMenuHorizontally="${propsInit.flipMenuHorizontally ?? false}"> | ||
Menu surface content | ||
</md-menu-surface> | ||
</div> | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* Adds an event listener for `eventName` on the given element. | ||
* @return Promise that resolves when `eventName` has been fired on the element. | ||
*/ | ||
export function listenOnce( | ||
element: HTMLElement, eventName: string): Promise<CustomEvent> { | ||
return new Promise((res) => { | ||
const listener = (e: CustomEvent) => { | ||
element.removeEventListener(eventName, listener as EventListener); | ||
res(e); | ||
}; | ||
|
||
element.addEventListener(eventName, listener as EventListener); | ||
}); | ||
} |