From 3c74ae08a0e32a195cf308089cf50e8299f38915 Mon Sep 17 00:00:00 2001 From: Robert Messerle Date: Thu, 7 Jul 2016 14:34:59 -0700 Subject: [PATCH] feat(tabs): adds e2e tests for tabs (#650) closes #549 --- e2e/components/tabs/tabs.e2e.ts | 97 ++++++++++++++++++++++++++++++ src/components/tabs/tab-group.html | 15 ++--- src/components/tabs/tabs.ts | 22 +++++++ src/e2e-app/e2e-app/e2e-app.html | 1 + src/e2e-app/e2e-app/e2e-app.ts | 14 ----- src/e2e-app/e2e-app/routes.ts | 2 + src/e2e-app/tabs/tabs-e2e.html | 16 +++++ src/e2e-app/tabs/tabs-e2e.ts | 10 +++ 8 files changed, 156 insertions(+), 21 deletions(-) create mode 100644 e2e/components/tabs/tabs.e2e.ts create mode 100644 src/e2e-app/tabs/tabs-e2e.html create mode 100644 src/e2e-app/tabs/tabs-e2e.ts diff --git a/e2e/components/tabs/tabs.e2e.ts b/e2e/components/tabs/tabs.e2e.ts new file mode 100644 index 000000000000..a4696d790899 --- /dev/null +++ b/e2e/components/tabs/tabs.e2e.ts @@ -0,0 +1,97 @@ +import ElementArrayFinder = protractor.ElementArrayFinder; +import ElementFinder = protractor.ElementFinder; + +describe('tabs', () => { + describe('basic behavior', () => { + let tabGroup: ElementFinder; + let tabLabels: ElementArrayFinder; + let tabBodies: ElementArrayFinder; + + beforeEach(() => { + browser.get('/tabs'); + tabGroup = element(by.css('md-tab-group')); + tabLabels = element.all(by.css('.md-tab-label')); + tabBodies = element.all(by.css('.md-tab-body')); + }); + + it('should change tabs when the label is clicked', () => { + tabLabels.get(1).click(); + expect(getActiveStates(tabLabels)).toEqual([false, true, false]); + expect(getActiveStates(tabBodies)).toEqual([false, true, false]); + + tabLabels.get(0).click(); + expect(getActiveStates(tabLabels)).toEqual([true, false, false]); + expect(getActiveStates(tabBodies)).toEqual([true, false, false]); + }); + + it('should change focus with keyboard interaction', () => { + tabLabels.get(0).click(); + expect(getFocusStates(tabLabels)).toEqual([true, false, false]); + + pressKey(protractor.Key.RIGHT); + expect(getFocusStates(tabLabels)).toEqual([false, true, false]); + + pressKey(protractor.Key.RIGHT); + expect(getFocusStates(tabLabels)).toEqual([false, false, true]); + + pressKey(protractor.Key.RIGHT); + expect(getFocusStates(tabLabels)).toEqual([false, false, true]); + + pressKey(protractor.Key.LEFT); + expect(getFocusStates(tabLabels)).toEqual([false, true, false]); + + pressKey(protractor.Key.LEFT); + expect(getFocusStates(tabLabels)).toEqual([true, false, false]); + + pressKey(protractor.Key.LEFT); + expect(getFocusStates(tabLabels)).toEqual([true, false, false]); + }); + }); +}); + +/** + * A helper function to perform the sendKey action + * @param key + */ +function pressKey(key: string) { + browser.actions().sendKeys(key).perform(); +} + +/** + * Returns an array of true/false that represents the focus states of the provided elements + * @param elements + * @returns {webdriver.promise.Promise[]>|webdriver.promise.Promise} + */ +function getFocusStates(elements: ElementArrayFinder) { + return elements.map(element => { + return element.getText().then(elementText => { + return browser.driver.switchTo().activeElement().getText().then(activeText => { + return activeText === elementText; + }); + }); + }); +} + +/** + * Returns an array of true/false that represents the active states for the provided elements + * @param elements + * @returns {webdriver.promise.Promise[]>|webdriver.promise.Promise} + */ +function getActiveStates(elements: ElementArrayFinder) { + return getClassStates(elements, 'md-active'); +} + +/** + * Returns an array of true/false values that represents whether the provided className is on + * each element + * @param elements + * @param className + * @returns {webdriver.promise.Promise[]>|webdriver.promise.Promise} + */ +function getClassStates(elements: ElementArrayFinder, className: string) { + return elements.map(element => { + return element.getAttribute('class').then(classes => { + return classes.split(/ +/g).indexOf(className) >= 0; + }); + }); +} diff --git a/src/components/tabs/tab-group.html b/src/components/tabs/tab-group.html index 3eb8ccc05e0b..1811d1b3cd88 100644 --- a/src/components/tabs/tab-group.html +++ b/src/components/tabs/tab-group.html @@ -1,7 +1,5 @@
+ (keydown)="handleKeydown($event)">