-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(menu): add custom position support to menu (#893)
- Loading branch information
1 parent
1efbbb9
commit 16eb6be
Showing
14 changed files
with
315 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import ElementFinder = protractor.ElementFinder; | ||
|
||
export class MenuPage { | ||
|
||
constructor() { | ||
browser.get('/menu'); | ||
} | ||
|
||
menu() { return element(by.css('.md-menu')); } | ||
|
||
trigger() { return element(by.id('trigger')); } | ||
|
||
triggerTwo() { return element(by.id('trigger-two')); } | ||
|
||
body() { return element(by.tagName('body')); } | ||
|
||
items(index: number) { | ||
return element.all(by.css('[md-menu-item]')).get(index); | ||
} | ||
|
||
textArea() { return element(by.id('text')); } | ||
|
||
beforeTrigger() { return element(by.id('before-t')); } | ||
|
||
aboveTrigger() { return element(by.id('above-t')); } | ||
|
||
combinedTrigger() { return element(by.id('combined-t')); } | ||
|
||
beforeMenu() { return element(by.css('.md-menu.before')); } | ||
|
||
aboveMenu() { return element(by.css('.md-menu.above')); } | ||
|
||
combinedMenu() { return element(by.css('.md-menu.combined')); } | ||
|
||
expectMenuPresent(expected: boolean) { | ||
return browser.isElementPresent(by.css('.md-menu')).then((isPresent) => { | ||
expect(isPresent).toBe(expected); | ||
}); | ||
} | ||
|
||
expectMenuLocation(el: ElementFinder, {x,y}: {x: number, y: number}) { | ||
el.getLocation().then((loc) => { | ||
expect(loc.x).toEqual(x); | ||
expect(loc.y).toEqual(y); | ||
}); | ||
} | ||
|
||
expectMenuAlignedWith(el: ElementFinder, id: string) { | ||
element(by.id(id)).getLocation().then((loc) => { | ||
this.expectMenuLocation(el, {x: loc.x, y: loc.y}); | ||
}); | ||
} | ||
|
||
getResultText() { | ||
return this.textArea().getText(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,81 +1,113 @@ | ||
describe('menu', function () { | ||
import { MenuPage } from './menu-page'; | ||
|
||
describe('menu', () => { | ||
let page: MenuPage; | ||
|
||
beforeEach(function() { | ||
browser.get('/menu'); | ||
page = new MenuPage(); | ||
}); | ||
|
||
it('should open menu when the trigger is clicked', function () { | ||
expectMenuPresent(false); | ||
element(by.id('trigger')).click(); | ||
it('should open menu when the trigger is clicked', () => { | ||
page.expectMenuPresent(false); | ||
page.trigger().click(); | ||
|
||
expectMenuPresent(true); | ||
expect(element(by.css('.md-menu')).getText()).toEqual("One\nTwo\nThree"); | ||
page.expectMenuPresent(true); | ||
expect(page.menu().getText()).toEqual("One\nTwo\nThree"); | ||
}); | ||
|
||
it('should align menu when open', function() { | ||
element(by.id('trigger')).click(); | ||
expectMenuAlignedWith('trigger'); | ||
it('should close menu when area outside menu is clicked', () => { | ||
page.trigger().click(); | ||
page.body().click(); | ||
page.expectMenuPresent(false); | ||
}); | ||
|
||
it('should close menu when area outside menu is clicked', function () { | ||
element(by.id('trigger')).click(); | ||
element(by.tagName('body')).click(); | ||
expectMenuPresent(false); | ||
it('should close menu when menu item is clicked', () => { | ||
page.trigger().click(); | ||
page.items(0).click(); | ||
page.expectMenuPresent(false); | ||
}); | ||
|
||
it('should close menu when menu item is clicked', function () { | ||
element(by.id('trigger')).click(); | ||
element(by.id('one')).click(); | ||
expectMenuPresent(false); | ||
it('should run click handlers on regular menu items', () => { | ||
page.trigger().click(); | ||
page.items(0).click(); | ||
expect(page.getResultText()).toEqual('one'); | ||
|
||
page.trigger().click(); | ||
page.items(1).click(); | ||
expect(page.getResultText()).toEqual('two'); | ||
}); | ||
|
||
it('should run click handlers on regular menu items', function() { | ||
element(by.id('trigger')).click(); | ||
element(by.id('one')).click(); | ||
expect(element(by.id('text')).getText()).toEqual('one'); | ||
it('should run not run click handlers on disabled menu items', () => { | ||
page.trigger().click(); | ||
page.items(2).click(); | ||
expect(page.getResultText()).toEqual(''); | ||
}); | ||
|
||
it('should support multiple triggers opening the same menu', () => { | ||
page.triggerTwo().click(); | ||
expect(page.menu().getText()).toEqual("One\nTwo\nThree"); | ||
page.expectMenuAlignedWith(page.menu(), 'trigger-two'); | ||
|
||
page.body().click(); | ||
page.expectMenuPresent(false); | ||
|
||
page.trigger().click(); | ||
expect(page.menu().getText()).toEqual("One\nTwo\nThree"); | ||
page.expectMenuAlignedWith(page.menu(), 'trigger'); | ||
|
||
element(by.id('trigger')).click(); | ||
element(by.id('two')).click(); | ||
expect(element(by.id('text')).getText()).toEqual('two'); | ||
page.body().click(); | ||
page.expectMenuPresent(false); | ||
}); | ||
|
||
it('should run not run click handlers on disabled menu items', function() { | ||
element(by.id('trigger')).click(); | ||
element(by.id('three')).click(); | ||
expect(element(by.id('text')).getText()).toEqual(''); | ||
it('should mirror classes on host to menu template in overlay', () => { | ||
page.trigger().click(); | ||
page.menu().getAttribute('class').then((classes) => { | ||
expect(classes).toEqual('md-menu custom'); | ||
}); | ||
}); | ||
|
||
it('should support multiple triggers opening the same menu', function() { | ||
element(by.id('trigger-two')).click(); | ||
expect(element(by.css('.md-menu')).getText()).toEqual("One\nTwo\nThree"); | ||
expectMenuAlignedWith('trigger-two'); | ||
describe('position - ', () => { | ||
|
||
element(by.tagName('body')).click(); | ||
expectMenuPresent(false); | ||
it('should default menu alignment to "after below" when not set', () => { | ||
page.trigger().click(); | ||
|
||
element(by.id('trigger')).click(); | ||
expect(element(by.css('.md-menu')).getText()).toEqual("One\nTwo\nThree"); | ||
expectMenuAlignedWith('trigger'); | ||
// menu.x should equal trigger.x, menu.y should equal trigger.y | ||
page.expectMenuAlignedWith(page.menu(), 'trigger'); | ||
}); | ||
|
||
element(by.tagName('body')).click(); | ||
expectMenuPresent(false); | ||
}); | ||
it('should align overlay end to origin end when x-position is "before"', () => { | ||
page.beforeTrigger().click(); | ||
page.beforeTrigger().getLocation().then((trigger) => { | ||
|
||
function expectMenuPresent(bool: boolean) { | ||
return browser.isElementPresent(by.css('.md-menu')).then((isPresent) => { | ||
expect(isPresent).toBe(bool); | ||
// the menu's right corner must be attached to the trigger's right corner. | ||
// menu = 112px wide. trigger = 60px wide. 112 - 60 = 52px of menu to the left of trigger. | ||
// trigger.x (left corner) - 52px (menu left of trigger) = expected menu.x (left corner) | ||
// menu.y should equal trigger.y because only x position has changed. | ||
page.expectMenuLocation(page.beforeMenu(), {x: trigger.x - 52, y: trigger.y}); | ||
}); | ||
}); | ||
} | ||
|
||
function expectMenuAlignedWith(id: string) { | ||
element(by.id(id)).getLocation().then((loc) => { | ||
expectMenuLocation({x: loc.x, y: loc.y}); | ||
it('should align overlay bottom to origin bottom when y-position is "above"', () => { | ||
page.aboveTrigger().click(); | ||
page.aboveTrigger().getLocation().then((trigger) => { | ||
|
||
// the menu's bottom corner must be attached to the trigger's bottom corner. | ||
// menu.x should equal trigger.x because only y position has changed. | ||
// menu = 64px high. trigger = 20px high. 64 - 20 = 44px of menu extending up past trigger. | ||
// trigger.y (top corner) - 44px (menu above trigger) = expected menu.y (top corner) | ||
page.expectMenuLocation(page.aboveMenu(), {x: trigger.x, y: trigger.y - 44}); | ||
}); | ||
}); | ||
} | ||
|
||
function expectMenuLocation({x,y}: {x: number, y: number}) { | ||
element(by.css('.md-menu')).getLocation().then((loc) => { | ||
expect(loc.x).toEqual(x); | ||
expect(loc.y).toEqual(y); | ||
it('should align menu to top left of trigger when "below" and "above"', () => { | ||
page.combinedTrigger().click(); | ||
page.combinedTrigger().getLocation().then((trigger) => { | ||
|
||
// trigger.x (left corner) - 52px (menu left of trigger) = expected menu.x | ||
// trigger.y (top corner) - 44px (menu above trigger) = expected menu.y | ||
page.expectMenuLocation(page.combinedMenu(), {x: trigger.x - 52, y: trigger.y - 44}); | ||
}); | ||
}); | ||
} | ||
|
||
}); | ||
}); |
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,4 @@ | ||
|
||
export type MenuPositionX = 'before' | 'after'; | ||
|
||
export type MenuPositionY = 'above' | 'below'; |
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
Oops, something went wrong.