Skip to content

Commit

Permalink
fix(addon-mobile): InputDateRange throws error on single date selec…
Browse files Browse the repository at this point in the history
…tion (#9411)
  • Loading branch information
nsbarsukov authored Oct 10, 2024
1 parent d0577d6 commit 93c08d4
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ export class TuiMobileCalendarDropdown {
}

protected confirm(value: any): void {
const normalizedValue = this.range ? this.selectedPeriod : value;

if (this.control) {
this.control.value = value;
this.control.value = normalizedValue;
}

this.observer?.next(value);
this.observer?.next(normalizedValue);
this.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,42 @@ import {DemoRoute} from '@demo/routes';
import type {Locator} from '@playwright/test';
import {expect, test} from '@playwright/test';

import {
TUI_PLAYWRIGHT_MOBILE_USER_AGENT,
TUI_PLAYWRIGHT_MOBILE_VIEWPORT_HEIGHT,
TUI_PLAYWRIGHT_MOBILE_VIEWPORT_WIDTH,
} from '../../../playwright.options';
import {
CHAR_NO_BREAK_SPACE,
TuiCalendarPO,
TuiCalendarSheetPO,
TuiDocumentationPagePO,
tuiGoto,
TuiInputDateRangePO,
TuiMobileCalendarPO,
} from '../../../utils';

test.describe('InputDateRange', () => {
let example!: Locator;
let inputDateRange!: TuiInputDateRangePO;
let documentationPage!: TuiDocumentationPagePO;

test.use({
viewport: {width: 650, height: 650},
});

test.beforeEach(async ({page}) => {
await tuiGoto(page, DemoRoute.InputDateRange);

test.beforeEach(({page}) => {
documentationPage = new TuiDocumentationPagePO(page);
example = documentationPage.apiPageExample;

inputDateRange = new TuiInputDateRangePO(example.locator('tui-input-date-range'));
});

test.describe('API', () => {
let example!: Locator;

test.beforeEach(() => {
example = documentationPage.apiPageExample;
inputDateRange = new TuiInputDateRangePO(
example.locator('tui-input-date-range'),
);
});

['s', 'm', 'l'].forEach((size) => {
test(`correct filler display for size ${size.toUpperCase()}`, async ({
page,
Expand All @@ -43,7 +52,7 @@ test.describe('InputDateRange', () => {
await expect(inputDateRange.textfield).toHaveScreenshot(
`01-textfield-size-${size}-empty.png`,
);
await expect(inputDateRange.calendarRange).toHaveScreenshot(
await expect(inputDateRange.calendar).toHaveScreenshot(
`01-calendar-size-${size}-empty.png`,
);

Expand All @@ -52,7 +61,7 @@ test.describe('InputDateRange', () => {
await expect(inputDateRange.textfield).toHaveScreenshot(
`02-textfield-size-${size}-set-day.png`,
);
await expect(inputDateRange.calendarRange).toHaveScreenshot(
await expect(inputDateRange.calendar).toHaveScreenshot(
`02-calendar-size-${size}-set-day.png`,
);

Expand All @@ -61,7 +70,7 @@ test.describe('InputDateRange', () => {
await expect(inputDateRange.textfield).toHaveScreenshot(
`03-textfield-size-${size}-set-from-date.png`,
);
await expect(inputDateRange.calendarRange).toHaveScreenshot(
await expect(inputDateRange.calendar).toHaveScreenshot(
`03-calendar-size-${size}-set-from-date.png`,
);

Expand All @@ -70,7 +79,7 @@ test.describe('InputDateRange', () => {
await expect(inputDateRange.textfield).toHaveScreenshot(
`04-textfield-size-${size}-set-to-date.png`,
);
await expect(inputDateRange.calendarRange).toHaveScreenshot(
await expect(inputDateRange.calendar).toHaveScreenshot(
`04-calendar-size-${size}-set-to-date.png`,
);
});
Expand All @@ -83,7 +92,7 @@ test.describe('InputDateRange', () => {
await expect(inputDateRange.textfield).toHaveScreenshot(
'05-textfield-maximum-month.png',
);
await expect(inputDateRange.calendarRange).toHaveScreenshot(
await expect(inputDateRange.calendar).toHaveScreenshot(
'05-calendar-maximum-month.png',
);
});
Expand All @@ -104,7 +113,7 @@ test.describe('InputDateRange', () => {
await expect(inputDateRange.textfield).toHaveScreenshot(
'06-textfield-maximum-month-with-items.png',
);
await expect(inputDateRange.calendarRange).toHaveScreenshot(
await expect(inputDateRange.calendar).toHaveScreenshot(
'06-calendar-maximum-month-with-items.png',
);
});
Expand Down Expand Up @@ -156,8 +165,8 @@ test.describe('InputDateRange', () => {
});

test('Select from [items] => select date range from calendar', async ({page}) => {
const calendar = new TuiCalendarPO(
inputDateRange.calendarRange.locator('tui-calendar'),
const calendarSheet = new TuiCalendarSheetPO(
inputDateRange.calendar.locator('tui-calendar-sheet'),
);

await tuiGoto(
Expand All @@ -171,7 +180,7 @@ test.describe('InputDateRange', () => {
await expect(inputDateRange.textfield).toHaveValue('Today');

await inputDateRange.textfield.click();
await calendar.clickOnCalendarDay(21);
await calendarSheet.clickOnDay(21);

await expect(inputDateRange.textfield).toHaveValue(
`21.09.2020${CHAR_NO_BREAK_SPACE}${CHAR_NO_BREAK_SPACE}25.09.2020`,
Expand All @@ -191,9 +200,45 @@ test.describe('InputDateRange', () => {

await expect(example).toHaveScreenshot('09-calendar-shows-end-of-period.png');
});

test.describe('Mobile emulation', () => {
test.use({
viewport: {
width: TUI_PLAYWRIGHT_MOBILE_VIEWPORT_WIDTH,
height: TUI_PLAYWRIGHT_MOBILE_VIEWPORT_HEIGHT,
},
userAgent: TUI_PLAYWRIGHT_MOBILE_USER_AGENT,
});

let mobileCalendar!: TuiMobileCalendarPO;

test.beforeEach(() => {
mobileCalendar = new TuiMobileCalendarPO(inputDateRange.calendar);
});

test('Selection of only single date produces range with the same start and end', async ({
page,
}) => {
await tuiGoto(page, `${DemoRoute.InputDateRange}/API`);
await inputDateRange.textfieldIcon.click();

const [calendarSheet] = await mobileCalendar.getCalendarSheets();

await calendarSheet?.clickOnDay(17);
await mobileCalendar.confirmButton.click();

await expect(inputDateRange.textfield).toHaveValue(
`17.08.2020${CHAR_NO_BREAK_SPACE}${CHAR_NO_BREAK_SPACE}17.08.2020`,
);
});
});
});

test.describe('Examples', () => {
test.beforeEach(async ({page}) => {
await tuiGoto(page, DemoRoute.InputDateRange);
});

test('Select second same range => after close/open calendar displays selected period displays correctly', async () => {
const example = documentationPage.getExample('#custom-period');

Expand All @@ -209,7 +254,7 @@ test.describe('InputDateRange', () => {
expect(await inputDateRange.itemHasCheckmark(2)).toBeTruthy();

await expect(inputDateRange.textfield).toHaveValue('Yet another yesterday');
await expect(inputDateRange.calendarRange).toHaveScreenshot(
await expect(inputDateRange.calendar).toHaveScreenshot(
'08-calendar-correct-selected-period-after-close-open.png',
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type {Locator} from '@playwright/test';

export class TuiCalendarPO {
export class TuiCalendarSheetPO {
constructor(private readonly host: Locator) {}

public async getDays(): Promise<Locator[]> {
return this.host.locator('[automation-id="tui-calendar-sheet__cell"]').all();
return this.host
.locator(
'[automation-id="tui-calendar-sheet__cell"], [automation-id="tui-primitive-calendar-mobile__cell"]',
)
.all();
}

public async clickOnCalendarDay(day: number): Promise<void> {
public async clickOnDay(day: number): Promise<void> {
const cells = await this.getDays();

for (const cell of cells) {
Expand Down
3 changes: 2 additions & 1 deletion projects/demo-playwright/utils/page-objects/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './calendar.po';
export * from './calendar-range.po';
export * from './calendar-sheet.po';
export * from './combo-box.po';
export * from './documentation-api-page.po';
export * from './documentation-page.po';
Expand All @@ -14,6 +14,7 @@ export * from './input-range.po';
export * from './input-slider.po';
export * from './input-tag.po';
export * from './input-time.po';
export * from './mobile-calendar.po';
export * from './multi-select.po';
export * from './range.po';
export * from './select.po';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ import {expect} from '@playwright/test';

export class TuiInputDateRangePO {
public readonly textfield: Locator = this.host.getByRole('textbox');
public readonly calendarRange: Locator = this.host
public readonly textfieldIcon: Locator = this.host.getByTestId(
'tui-input-date-range__icon',
);

public readonly calendar: Locator = this.host
.page()
.locator('tui-calendar-range');
.locator('tui-calendar-range, tui-mobile-calendar');

public readonly items = this.calendarRange.locator(
public readonly items = this.calendar.locator(
'[automation-id="tui-calendar-range__menu"]',
);

constructor(private readonly host: Locator) {}

public async getItems(): Promise<Locator[]> {
const dataList = this.calendarRange.locator(
const dataList = this.calendar.locator(
'[automation-id="tui-calendar-range__menu"]',
);

Expand Down
18 changes: 18 additions & 0 deletions projects/demo-playwright/utils/page-objects/mobile-calendar.po.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {TuiCalendarSheetPO} from '@demo-playwright/utils';
import type {Locator} from '@playwright/test';

export class TuiMobileCalendarPO {
public cancelButton = this.host.getByTestId('tui-mobile-calendar__cancel');
public confirmButton = this.host.getByTestId('tui-mobile-calendar__confirm');

constructor(private readonly host: Locator) {}

public async getCalendarSheets(): Promise<TuiCalendarSheetPO[]> {
const locators = await this.host
.page()
.locator('tui-calendar-sheet, tui-mobile-calendar-sheet')
.all();

return locators.map((x) => new TuiCalendarSheetPO(x));
}
}

0 comments on commit 93c08d4

Please sign in to comment.