Skip to content

Commit

Permalink
test: update tests, fix datePicker opens with a clean slate in input
Browse files Browse the repository at this point in the history
Signed-off-by: Rong Sen Ng (motss) <wes.ngrongsen@gmail.com>
  • Loading branch information
motss committed Feb 26, 2022
1 parent 6bf625b commit 050447b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
11 changes: 11 additions & 0 deletions src/__demo__/demo-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ export class DemoApp extends RootElement {
.value=${'2020-02-02'}
></app-date-picker-input>
<app-date-picker-input
id="datePickerInput1"
?outlined=${true}
.label=${'DOB'}
.placeholder=${'Select your date of birth'}
.max=${'2100-12-31'}
.min=${'1970-01-01'}
.value=${'2020-02-02'}
.startView=${'yearGrid'}
></app-date-picker-input>
<button data-id="datePickerDialog1" @click=${this.#showDialog}>Open</button>
<app-date-picker-dialog id="datePickerDialog1"></app-date-picker-dialog>
Expand Down
29 changes: 17 additions & 12 deletions src/__tests__/date-picker-input/app-date-picker-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ describe(appDatePickerInputName, () => {
];
casesOptionalLocale.forEach(a => {
const [testLocale, expectedLocale] = a;

it(
messageFormatter('renders with optional locale (%s)', testLocale),
async () => {
Expand Down Expand Up @@ -216,7 +217,7 @@ describe(appDatePickerInputName, () => {
datePicker = el.query<AppDatePicker>(elementSelectors.datePicker);

expect(datePickerInputSurface).exist;
expect(datePicker).exist;
expect(datePicker).not.exist;
});

type CaseCloseDatePickerByTriggerType = [
Expand Down Expand Up @@ -273,8 +274,7 @@ describe(appDatePickerInputName, () => {
break;
}
case 'escape': {
await sendKeys({ down: keyEscape });
await sendKeys({ up: keyEscape });
await sendKeys({ press: keyEscape });
break;
}
case 'tab': {
Expand All @@ -283,7 +283,11 @@ describe(appDatePickerInputName, () => {
expect(yearDropdown).exist;

yearDropdown?.focus();
for (const _ of Array(4)) await sendKeys({ press: keyTab });

for (const _ of Array(4)) {
await sendKeys({ press: keyTab });
}

break;
}
default:
Expand All @@ -298,7 +302,7 @@ describe(appDatePickerInputName, () => {
datePicker = el.query<AppDatePicker>(elementSelectors.datePicker);

expect(datePickerInputSurface).exist;
expect(datePicker).exist;
expect(datePicker).not.exist;
}
);
});
Expand Down Expand Up @@ -330,8 +334,7 @@ describe(appDatePickerInputName, () => {
CustomEvent<unknown>>(el, 'opened');

el.focus();
await sendKeys({ down: testKey });
await sendKeys({ up: testKey });
await sendKeys({ press: testKey });

const opened = await openedTask;
await el.updateComplete;
Expand Down Expand Up @@ -449,8 +452,7 @@ describe(appDatePickerInputName, () => {
CustomEvent<DialogClosedEventDetail>>(el, 'closed');

newSelectedDate?.focus();
await sendKeys({ down: testKey });
await sendKeys({ up: testKey });
await sendKeys({ press: testKey });

await closedTask;
await el.updateComplete;
Expand Down Expand Up @@ -507,7 +509,7 @@ describe(appDatePickerInputName, () => {
expect(closed).not.undefined;
});

it('always re-opens in calendar view', async () => {
it('always re-opens in the correct startView', async () => {
const el = await fixture<AppDatePickerInput>(
html`<app-date-picker-input
.label=${label}
Expand Down Expand Up @@ -564,8 +566,11 @@ describe(appDatePickerInputName, () => {
yearGrid = datePicker?.query<AppYearGrid>(elementSelectors.yearGrid);
const monthCalendar = datePicker?.query<AppMonthCalendar>(elementSelectors.monthCalendar);

// ensure calendar view when it re-opens
expect(monthCalendar).exist;
/**
* NOTE: Year view should render when it re-opens because `.startView=${'yearGrid'}` is set
*/
expect(monthCalendar).not.exist;
expect(yearGrid).exist;
});

});
2 changes: 2 additions & 0 deletions src/date-picker-input-surface/stylings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const DatePickerInputSurfaceStyling = css`
display: block;
position: absolute; /* NOTE(motss): Set this so that surface can be placed on top of its anchor element */
top: 0; /** NOTE(motss): This ensures inputSurface renders downwards on top of input */
bottom: 0; /** NOTE(motss): This ensures inputSurface renders upwards on top of input */
}
.mdc-menu-surface.mdc-menu-surface--open {
Expand Down
16 changes: 13 additions & 3 deletions src/date-picker-input/date-picker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class DatePickerInput extends ElementMixin(DatePickerMixin(DatePickerMinM

return html`
${super.render()}
${_lazyLoaded && _open ? this.$renderContent() : nothing}
${_lazyLoaded ? this.$renderContent() : nothing}
`;
}

Expand Down Expand Up @@ -220,7 +220,12 @@ export class DatePickerInput extends ElementMixin(DatePickerMixin(DatePickerMinM
?stayOpenOnBodyClick=${true}
.anchor=${this as HTMLElement}
@closed=${this.#onClosed}
>${this.$renderSlot()}</app-date-picker-input-surface>
>${
/**
* NOTE(motss): This removes/ renders datePicker with a clean slate.
*/
this._open ? this.$renderSlot() : nothing
}</app-date-picker-input-surface>
`;
}

Expand Down Expand Up @@ -319,6 +324,12 @@ export class DatePickerInput extends ElementMixin(DatePickerMixin(DatePickerMinM
}
}

/**
* NOTE(motss): `#lazyLoad()` is called within `render()` so this needs to wait for next update
* to re-trigger update when it updates `_lazyLoaded`.
*/
await this.updateComplete;

this.#lazyLoading = false;
this._lazyLoaded = true;
};
Expand All @@ -333,7 +344,6 @@ export class DatePickerInput extends ElementMixin(DatePickerMixin(DatePickerMinM

#onClosed = ({ detail }: CustomEvent): void => {
this._open = false;
this.#picker && (this.#picker.startView = 'calendar');
this.fire({ detail, type: 'closed' });
};

Expand Down

0 comments on commit 050447b

Please sign in to comment.