-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
datetime.e2e.ts
314 lines (242 loc) · 11.1 KB
/
datetime.e2e.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { expect } from '@playwright/test';
import type { E2EPage } from '@utils/test/playwright';
import { test } from '@utils/test/playwright';
test.describe('datetime: closing time popover', () => {
test('it should not change months', async ({ page }) => {
test.info().annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/25438',
});
await page.setContent(`
<ion-datetime></ion-datetime>
`);
const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent');
const ionPopoverDidDismiss = await page.spyOnEvent('ionPopoverDidDismiss');
const timeButton = page.locator('.time-body');
const calendarMonthYear = page.locator('ion-datetime .calendar-month-year');
const currentMonthAndYear = await calendarMonthYear.evaluate((el: HTMLElement) => el.innerText);
await timeButton.click();
await ionPopoverDidPresent.next();
await page.keyboard.press('Escape');
await ionPopoverDidDismiss.next();
await page.waitForChanges();
await expect(calendarMonthYear).toHaveText(currentMonthAndYear);
});
});
test.describe('datetime: selecting a day', () => {
const testHighlight = async (page: E2EPage, datetimeID: string) => {
const today = new Date();
await page.goto('/src/components/datetime/test/basic');
await page.setIonViewport();
const todayBtn = page.locator(
`#${datetimeID} .calendar-day[data-day='${today.getDate()}'][data-month='${today.getMonth() + 1}']`
);
await expect(todayBtn).toHaveClass(/calendar-day-today/);
await expect(todayBtn).not.toHaveClass(/calendar-day-active/);
await todayBtn.click();
await page.waitForChanges();
await expect(todayBtn).toHaveClass(/calendar-day-active/);
};
test('should not highlight a day until one is selected', async ({ page }) => {
await testHighlight(page, 'inline-datetime-no-value');
});
test('should not highlight a day until one is selected, with default-buttons', async ({ page }) => {
await testHighlight(page, 'custom-datetime');
});
test('should update the active day', async ({ page }) => {
await page.setContent(`
<ion-datetime show-default-buttons="true" value="2021-12-25T12:40:00.000Z"></ion-datetime>
`);
const activeDay = page.locator('ion-datetime .calendar-day-active');
await expect(activeDay).toHaveText('25');
const dayBtn = page.locator('ion-datetime .calendar-day[data-day="13"][data-month="12"]');
await dayBtn.click();
await page.waitForChanges();
await expect(activeDay).toHaveText('13');
});
test('should set both date and time when no value is initially set', async ({ page }) => {
await page.setContent(`
<ion-datetime locale="en-US" presentation="date-time"></ion-datetime>
<script>
const mockToday = '2022-10-10T16:22';
Date = class extends Date {
constructor(...args) {
if (args.length === 0) {
super(mockToday)
} else {
super(...args);
}
}
}
</script>
`);
await page.waitForSelector('.datetime-ready');
const datetime = page.locator('ion-datetime');
const ionChange = await page.spyOnEvent('ionChange');
// Oct 1, 2022
await page.click('.calendar-day[data-month="10"][data-year="2022"][data-day="1"]');
await ionChange.next();
const value = await datetime.evaluate((el: HTMLIonDatetimeElement) => el.value);
await expect(typeof value).toBe('string');
// Check to make sure value includes current time
await expect(value!.includes('2022-10-01T16:22')).toBe(true);
});
});
test.describe('datetime: confirm date', () => {
test('should not update value if Done was clicked without selecting a day first', async ({ page }) => {
await page.goto('/src/components/datetime/test/basic');
const datetime = page.locator('#custom-datetime');
const value = await datetime.evaluate((el: HTMLIonDatetimeElement) => el.value);
expect(value).toBeUndefined();
await datetime.evaluate(async (el: HTMLIonDatetimeElement) => {
await el.confirm();
});
const valueAgain = await datetime.evaluate((el: HTMLIonDatetimeElement) => el.value);
expect(valueAgain).toBeUndefined();
});
test('should set the date value based on the selected date', async ({ page }) => {
await page.setContent(`
<button id="bind">Bind datetimeMonthDidChange event</button>
<ion-datetime value="2021-12-25T12:40:00.000Z"></ion-datetime>
<script type="module">
import { InitMonthDidChangeEvent } from '/src/components/datetime/test/utils/month-did-change-event.js';
document.querySelector('button').addEventListener('click', function() {
InitMonthDidChangeEvent();
});
</script>
`);
const datetimeMonthDidChange = await page.spyOnEvent('datetimeMonthDidChange');
const eventButton = page.locator('button#bind');
await eventButton.click();
const buttons = page.locator('ion-datetime .calendar-next-prev ion-button');
await buttons.nth(1).click();
await datetimeMonthDidChange.next();
const datetime = page.locator('ion-datetime');
await datetime.evaluate((el: HTMLIonDatetimeElement) => el.confirm());
// Value may include timezone information so we need to check
// that the value at least includes the correct date/time info.
const value = (await datetime.evaluate((el: HTMLIonDatetimeElement) => el.value))!;
expect(value.includes('2021-12-25T12:40:00')).toBe(true);
});
});
test.describe('datetime: footer', () => {
test('should render default buttons', async ({ page }) => {
await page.setContent('<ion-datetime value="2022-05-03" show-default-buttons="true"></ion-datetime>');
const cancelButton = page.locator('ion-datetime #cancel-button');
await expect(cancelButton).toHaveText('Cancel');
const confirmButton = page.locator('ion-datetime #confirm-button');
await expect(confirmButton).toHaveText('Done');
const datetime = page.locator('ion-datetime');
expect(await datetime.screenshot()).toMatchSnapshot(
`datetime-footer-default-buttons-${page.getSnapshotSettings()}.png`
);
});
test('should render clear button', async ({ page }) => {
await page.setContent('<ion-datetime value="2022-05-03" show-clear-button="true"></ion-datetime>');
const clearButton = page.locator('ion-datetime #clear-button');
await expect(clearButton).toHaveText('Clear');
const datetime = page.locator('ion-datetime');
expect(await datetime.screenshot()).toMatchSnapshot(
`datetime-footer-clear-button-${page.getSnapshotSettings()}.png`
);
});
test('should render default and clear buttons', async ({ page }) => {
await page.setContent(
'<ion-datetime value="2022-05-03" show-default-buttons="true" show-clear-button="true"></ion-datetime>'
);
const cancelButton = page.locator('ion-datetime #cancel-button');
await expect(cancelButton).toHaveText('Cancel');
const confirmButton = page.locator('ion-datetime #confirm-button');
await expect(confirmButton).toHaveText('Done');
const clearButton = page.locator('ion-datetime #clear-button');
await expect(clearButton).toHaveText('Clear');
const datetime = page.locator('ion-datetime');
expect(await datetime.screenshot()).toMatchSnapshot(
`datetime-footer-default-clear-buttons-${page.getSnapshotSettings()}.png`
);
});
test('should render custom buttons', async ({ page }) => {
await page.setContent(`
<ion-datetime value="2022-05-03">
<ion-buttons slot="buttons">
<ion-button id="custom-button" color="primary">Hello!</ion-button>
</ion-buttons>
</ion-datetime>
`);
const customButton = page.locator('ion-datetime #custom-button');
await expect(customButton).toBeVisible();
const datetime = page.locator('ion-datetime');
expect(await datetime.screenshot()).toMatchSnapshot(
`datetime-footer-custom-buttons-${page.getSnapshotSettings()}.png`
);
});
});
test.describe('datetime: swiping', () => {
test.beforeEach(({ skip }) => {
skip.rtl();
skip.mode('ios', 'This does not have mode-specific logic.');
});
test('should move to prev month by swiping', async ({ page }) => {
await page.setContent(`
<ion-datetime value="2022-05-03"></ion-datetime>
`);
await page.waitForSelector('.datetime-ready');
const calendarBody = page.locator('ion-datetime .calendar-body');
const calendarHeader = page.locator('ion-datetime .calendar-month-year');
await expect(calendarHeader).toHaveText(/May 2022/);
await calendarBody.evaluate((el: HTMLElement) => (el.scrollLeft = 0));
await page.waitForChanges();
await expect(calendarHeader).toHaveText(/April 2022/);
});
test('should move to next month by swiping', async ({ page }) => {
await page.setContent(`
<ion-datetime value="2022-05-03"></ion-datetime>
`);
await page.waitForSelector('.datetime-ready');
const calendarBody = page.locator('ion-datetime .calendar-body');
const calendarHeader = page.locator('ion-datetime .calendar-month-year');
await expect(calendarHeader).toHaveText(/May 2022/);
await calendarBody.evaluate((el: HTMLElement) => (el.scrollLeft = el.scrollWidth));
await page.waitForChanges();
await expect(calendarHeader).toHaveText(/June 2022/);
});
test('should not re-render if swipe is in progress', async ({ page, skip }) => {
skip.browser('webkit', 'Wheel is not available in WebKit');
await page.setContent(`
<ion-datetime value="2022-05-03"></ion-datetime>
`);
await page.waitForSelector('.datetime-ready');
const calendarBody = page.locator('ion-datetime .calendar-body');
const calendarHeader = page.locator('ion-datetime .calendar-month-year');
await expect(calendarHeader).toHaveText(/May 2022/);
const box = await calendarBody.boundingBox();
if (box) {
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
await page.mouse.wheel(-50, 0);
await page.waitForChanges();
await expect(calendarHeader).toHaveText(/May 2022/);
}
});
});
test.describe('datetime: visibility', () => {
test('should reset month/year interface when hiding datetime', async ({ page, skip }) => {
skip.rtl();
skip.mode('md');
await page.setContent(`
<ion-datetime></ion-datetime>
`);
await page.waitForSelector('.datetime-ready');
const monthYearButton = page.locator('ion-datetime .calendar-month-year');
const monthYearInterface = page.locator('ion-datetime .datetime-year');
const datetime = page.locator('ion-datetime');
await monthYearButton.click();
await page.waitForChanges();
await expect(monthYearInterface).toBeVisible();
await datetime.evaluate((el: HTMLIonDatetimeElement) => el.style.setProperty('display', 'none'));
await expect(datetime).toBeHidden();
await datetime.evaluate((el: HTMLIonDatetimeElement) => el.style.removeProperty('display'));
await expect(datetime).toBeVisible();
// month/year interface should be reset
await expect(monthYearInterface).toBeHidden();
});
});