Skip to content

fix(datepicker): all cells being read out as selected #12006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/datepicker/calendar-body.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
[class.mat-calendar-body-active]="_isActiveCell(rowIndex, colIndex)"
[attr.aria-label]="item.ariaLabel"
[attr.aria-disabled]="!item.enabled || null"
[attr.aria-selected]="selectedValue === item.value"
(click)="_cellClicked(item)"
[style.width.%]="100 / numCols"
[style.paddingTop.%]="50 * cellAspectRatio / numCols"
Expand Down
43 changes: 26 additions & 17 deletions src/lib/datepicker/calendar-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ describe('MatCalendarBody', () => {
let fixture: ComponentFixture<StandardCalendarBody>;
let testComponent: StandardCalendarBody;
let calendarBodyNativeElement: Element;
let rowEls: NodeListOf<Element>;
let labelEls: NodeListOf<Element>;
let cellEls: NodeListOf<Element>;
let rowEls: Element[];
let labelEls: Element[];
let cellEls: Element[];

let refreshElementLists = () => {
rowEls = calendarBodyNativeElement.querySelectorAll('tr');
labelEls = calendarBodyNativeElement.querySelectorAll('.mat-calendar-body-label');
cellEls = calendarBodyNativeElement.querySelectorAll('.mat-calendar-body-cell');
};
function refreshElementLists() {
rowEls = Array.from(calendarBodyNativeElement.querySelectorAll('tr'));
labelEls = Array.from(calendarBodyNativeElement.querySelectorAll('.mat-calendar-body-label'));
cellEls = Array.from(calendarBodyNativeElement.querySelectorAll('.mat-calendar-body-cell'));
}

beforeEach(() => {
fixture = TestBed.createComponent(StandardCalendarBody);
fixture.detectChanges();

let calendarBodyDebugElement = fixture.debugElement.query(By.directive(MatCalendarBody));
const calendarBodyDebugElement = fixture.debugElement.query(By.directive(MatCalendarBody));
calendarBodyNativeElement = calendarBodyDebugElement.nativeElement;
testComponent = fixture.componentInstance;

Expand All @@ -51,17 +51,26 @@ describe('MatCalendarBody', () => {
});

it('highlights today', () => {
let todayCell = calendarBodyNativeElement.querySelector('.mat-calendar-body-today')!;
const todayCell = calendarBodyNativeElement.querySelector('.mat-calendar-body-today')!;
expect(todayCell).not.toBeNull();
expect(todayCell.innerHTML.trim()).toBe('3');
});

it('highlights selected', () => {
let selectedCell = calendarBodyNativeElement.querySelector('.mat-calendar-body-selected')!;
const selectedCell = calendarBodyNativeElement.querySelector('.mat-calendar-body-selected')!;
expect(selectedCell).not.toBeNull();
expect(selectedCell.innerHTML.trim()).toBe('4');
});

it('should set aria-selected correctly', () => {
const selectedCells = cellEls.filter(c => c.getAttribute('aria-selected') === 'true');
const deselectedCells = cellEls.filter(c => c.getAttribute('aria-selected') === 'false');

expect(selectedCells.length).toBe(1, 'Expected one cell to be marked as selected.');
expect(deselectedCells.length)
.toBe(cellEls.length - 1, 'Expected remaining cells to be marked as deselected.');
});

it('places label in first row if space is available', () => {
testComponent.rows[0] = testComponent.rows[0].slice(3);
testComponent.rows = testComponent.rows.slice();
Expand All @@ -77,7 +86,7 @@ describe('MatCalendarBody', () => {
});

it('cell should be selected on click', () => {
let todayElement =
const todayElement =
calendarBodyNativeElement.querySelector('.mat-calendar-body-today') as HTMLElement;
todayElement.click();
fixture.detectChanges();
Expand All @@ -96,28 +105,28 @@ describe('MatCalendarBody', () => {
let fixture: ComponentFixture<CalendarBodyWithDisabledCells>;
let testComponent: CalendarBodyWithDisabledCells;
let calendarBodyNativeElement: Element;
let cellEls: NodeListOf<Element>;
let cellEls: HTMLElement[];

beforeEach(() => {
fixture = TestBed.createComponent(CalendarBodyWithDisabledCells);
fixture.detectChanges();

let calendarBodyDebugElement = fixture.debugElement.query(By.directive(MatCalendarBody));
const calendarBodyDebugElement = fixture.debugElement.query(By.directive(MatCalendarBody));
calendarBodyNativeElement = calendarBodyDebugElement.nativeElement;
testComponent = fixture.componentInstance;
cellEls = calendarBodyNativeElement.querySelectorAll('.mat-calendar-body-cell');
cellEls = Array.from(calendarBodyNativeElement.querySelectorAll('.mat-calendar-body-cell'));
});

it('should only allow selection of disabled cells when allowDisabledSelection is true', () => {
(cellEls[0] as HTMLElement).click();
cellEls[0].click();
fixture.detectChanges();

expect(testComponent.selected).toBeFalsy();

testComponent.allowDisabledSelection = true;
fixture.detectChanges();

(cellEls[0] as HTMLElement).click();
cellEls[0].click();
fixture.detectChanges();

expect(testComponent.selected).toBe(1);
Expand Down