/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { ComponentHarness, parallel } from '@angular/cdk/testing'; import { MatCalendarHarness } from './calendar-harness'; /** Base class for harnesses that can trigger a calendar. */ export class DatepickerTriggerHarnessBase extends ComponentHarness { /** Opens the calendar if the trigger is enabled and it has a calendar. */ async openCalendar() { const [isDisabled, hasCalendar] = await parallel(() => [this.isDisabled(), this.hasCalendar()]); if (!isDisabled && hasCalendar) { return this._openCalendar(); } } /** Closes the calendar if it is open. */ async closeCalendar() { if (await this.isCalendarOpen()) { await closeCalendar(getCalendarId(this.host()), this.documentRootLocatorFactory()); // This is necessary so that we wait for the closing animation to finish in touch UI mode. await this.forceStabilize(); } } /** Gets whether there is a calendar associated with the trigger. */ async hasCalendar() { return (await getCalendarId(this.host())) != null; } /** * Gets the `MatCalendarHarness` that is associated with the trigger. * @param filter Optionally filters which calendar is included. */ async getCalendar(filter = {}) { return getCalendar(filter, this.host(), this.documentRootLocatorFactory()); } } /** Gets the ID of the calendar that a particular test element can trigger. */ export async function getCalendarId(host) { return (await host).getAttribute('data-mat-calendar'); } /** Closes the calendar with a specific ID. */ export async function closeCalendar(calendarId, documentLocator) { // We close the calendar by clicking on the backdrop, even though all datepicker variants // have the ability to close by pressing escape. The backdrop is preferrable, because the // escape key has multiple functions inside a range picker (either cancel the current range // or close the calendar). Since we don't have access to set the ID on the backdrop in all // cases, we set a unique class instead which is the same as the calendar's ID and suffixed // with `-backdrop`. const backdropSelector = `.${await calendarId}-backdrop`; return (await documentLocator.locatorFor(backdropSelector)()).click(); } /** Gets the test harness for a calendar associated with a particular host. */ export async function getCalendar(filter, host, documentLocator) { const calendarId = await getCalendarId(host); if (!calendarId) { throw Error(`Element is not associated with a calendar`); } return documentLocator.locatorFor(MatCalendarHarness.with({ ...filter, selector: `#${calendarId}`, }))(); } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ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