2017-02-16 15:43:29 -08:00
|
|
|
import {
|
|
|
|
|
browser,
|
|
|
|
|
by,
|
|
|
|
|
element,
|
|
|
|
|
ElementArrayFinder,
|
|
|
|
|
Key,
|
|
|
|
|
ExpectedConditions
|
|
|
|
|
} from 'protractor';
|
2017-06-10 01:09:08 +02:00
|
|
|
import {pressKeys} from '../util/index';
|
2017-06-01 17:49:20 +02:00
|
|
|
|
2016-07-07 14:34:59 -07:00
|
|
|
describe('tabs', () => {
|
|
|
|
|
describe('basic behavior', () => {
|
|
|
|
|
let tabLabels: ElementArrayFinder;
|
|
|
|
|
let tabBodies: ElementArrayFinder;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
browser.get('/tabs');
|
2017-02-08 11:24:31 -08:00
|
|
|
tabLabels = element.all(by.css('.mat-tab-label'));
|
2017-09-28 13:45:03 -07:00
|
|
|
tabBodies = element.all(by.css('mat-tab-body'));
|
2016-07-07 14:34:59 -07:00
|
|
|
});
|
|
|
|
|
|
2017-06-01 17:49:20 +02:00
|
|
|
it('should change tabs when the label is clicked', async () => {
|
2016-07-07 14:34:59 -07:00
|
|
|
tabLabels.get(1).click();
|
2017-11-14 03:55:53 +01:00
|
|
|
expect(await getLabelActiveStates(tabLabels)).toEqual([false, true, false]);
|
|
|
|
|
expect(await getBodyActiveStates(tabBodies)).toEqual([false, true, false]);
|
2017-06-01 17:49:20 +02:00
|
|
|
|
|
|
|
|
await browser.wait(ExpectedConditions.not(
|
|
|
|
|
ExpectedConditions.presenceOf(element(by.css('div.mat-ripple-element')))));
|
2016-07-07 14:34:59 -07:00
|
|
|
|
|
|
|
|
tabLabels.get(0).click();
|
2017-11-14 03:55:53 +01:00
|
|
|
expect(await getLabelActiveStates(tabLabels)).toEqual([true, false, false]);
|
|
|
|
|
expect(await getBodyActiveStates(tabBodies)).toEqual([true, false, false]);
|
2017-06-01 17:49:20 +02:00
|
|
|
|
|
|
|
|
await browser.wait(ExpectedConditions.not(
|
|
|
|
|
ExpectedConditions.presenceOf(element(by.css('div.mat-ripple-element')))));
|
2016-07-07 14:34:59 -07:00
|
|
|
});
|
|
|
|
|
|
2017-11-14 03:55:53 +01:00
|
|
|
it('should change focus with keyboard interaction', async () => {
|
2018-07-18 17:54:44 +02:00
|
|
|
const right = Key.RIGHT;
|
|
|
|
|
const left = Key.LEFT;
|
2017-01-18 23:13:48 +01:00
|
|
|
|
2016-07-07 14:34:59 -07:00
|
|
|
tabLabels.get(0).click();
|
2017-11-14 03:55:53 +01:00
|
|
|
expect(await getFocusStates(tabLabels)).toEqual([true, false, false]);
|
2016-07-07 14:34:59 -07:00
|
|
|
|
2017-01-18 23:13:48 +01:00
|
|
|
pressKeys(right);
|
2017-11-14 03:55:53 +01:00
|
|
|
expect(await getFocusStates(tabLabels)).toEqual([false, true, false]);
|
2016-07-07 14:34:59 -07:00
|
|
|
|
2017-01-18 23:13:48 +01:00
|
|
|
pressKeys(right);
|
2017-11-14 03:55:53 +01:00
|
|
|
expect(await getFocusStates(tabLabels)).toEqual([false, false, true]);
|
2016-07-07 14:34:59 -07:00
|
|
|
|
2017-01-18 23:13:48 +01:00
|
|
|
pressKeys(left);
|
2017-11-14 03:55:53 +01:00
|
|
|
expect(await getFocusStates(tabLabels)).toEqual([false, true, false]);
|
2016-07-07 14:34:59 -07:00
|
|
|
|
2017-01-18 23:13:48 +01:00
|
|
|
pressKeys(left);
|
2017-11-14 03:55:53 +01:00
|
|
|
expect(await getFocusStates(tabLabels)).toEqual([true, false, false]);
|
2016-07-07 14:34:59 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-11 14:27:01 -08:00
|
|
|
* Returns an array of true/false that represents the focus states of the provided elements.
|
2016-07-07 14:34:59 -07:00
|
|
|
*/
|
2017-06-01 17:49:20 +02:00
|
|
|
async function getFocusStates(elements: ElementArrayFinder) {
|
2017-11-14 03:55:53 +01:00
|
|
|
return elements.map(async el => {
|
2018-07-18 17:54:44 +02:00
|
|
|
const elementText = await el!.getText();
|
|
|
|
|
const activeText = await browser.driver.switchTo().activeElement().getText();
|
2017-06-01 17:49:20 +02:00
|
|
|
|
|
|
|
|
return activeText === elementText;
|
2016-07-07 14:34:59 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-11 14:27:01 -08:00
|
|
|
/** Returns an array of true/false that represents the active states for the provided elements. */
|
|
|
|
|
function getLabelActiveStates(elements: ElementArrayFinder) {
|
2017-02-08 11:24:31 -08:00
|
|
|
return getClassStates(elements, 'mat-tab-label-active');
|
2016-11-11 14:27:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns an array of true/false that represents the active states for the provided elements */
|
|
|
|
|
function getBodyActiveStates(elements: ElementArrayFinder) {
|
2017-02-08 11:24:31 -08:00
|
|
|
return getClassStates(elements, 'mat-tab-body-active');
|
2016-07-07 14:34:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array of true/false values that represents whether the provided className is on
|
2016-11-11 14:27:01 -08:00
|
|
|
* each element.
|
2016-07-07 14:34:59 -07:00
|
|
|
*/
|
2017-06-01 17:49:20 +02:00
|
|
|
async function getClassStates(elements: ElementArrayFinder, className: string) {
|
2017-11-14 03:55:53 +01:00
|
|
|
return elements.map(async el => {
|
2018-07-18 17:54:44 +02:00
|
|
|
const classes = await el!.getAttribute('class');
|
|
|
|
|
return classes.split(/ +/g).indexOf(className) > -1;
|
2016-07-07 14:34:59 -07:00
|
|
|
});
|
|
|
|
|
}
|