|
1 | | -declare global { |
2 | | - // eslint-disable-next-line @typescript-eslint/no-namespace |
3 | | - namespace Cypress { |
4 | | - interface Chainable { |
5 | | - /** |
6 | | - * Selects a given date into a QDate. |
7 | | - * @example cy.get('.q-date').selectDate('11/02/2021') |
8 | | - * @example cy.dataCy('start-date').selectDate(new Date()) |
9 | | - */ |
10 | | - selectDate<E extends HTMLElement = HTMLElement>( |
11 | | - value: string | Date, |
12 | | - ): Chainable<JQueryWithSelector<E>>; |
13 | | - } |
14 | | - } |
15 | | -} |
16 | | - |
17 | | -export function registerSelectDate() { |
18 | | - Cypress.Commands.add( |
19 | | - 'selectDate', |
20 | | - { prevSubject: 'element' }, |
21 | | - (subject, value) => { |
22 | | - if (!subject.hasClass('q-date')) { |
23 | | - throw new Error('Subject is not a QDate'); |
24 | | - } |
25 | | - |
26 | | - const targetDate = typeof value === 'string' ? new Date(value) : value; |
27 | | - |
28 | | - cy.wrap(subject).within(() => { |
29 | | - cy.get('.q-date__navigation div:not(.q-date__arrow)') |
30 | | - .last() |
31 | | - .as('yearSelector'); |
32 | | - cy.get('.q-date__navigation div:not(.q-date__arrow)') |
33 | | - .first() |
34 | | - .as('monthSelector'); |
35 | | - |
36 | | - const targetYear = targetDate.getFullYear(); |
37 | | - const targetMonth = targetDate.getMonth(); |
38 | | - const targetDay = targetDate.getDate(); |
39 | | - |
40 | | - // Since it's easy to detect it with the year selector, |
41 | | - // we avoid selecting the year if it's already the target one |
42 | | - cy.get('@yearSelector') |
43 | | - .invoke('text') |
44 | | - .then((currentYear) => { |
45 | | - if (currentYear !== targetYear.toString()) { |
46 | | - cy.get('@yearSelector').click(); |
47 | | - cy.contains(targetYear).click(); |
48 | | - } |
49 | | - }); |
50 | | - |
51 | | - cy.get('@monthSelector').click(); |
52 | | - cy.get('.q-date__months-item').eq(targetMonth).click(); |
53 | | - |
54 | | - // The target day number is searched only into days buttons, |
55 | | - // skipping filler and out of range days |
56 | | - cy.get('.q-date__calendar-item--in').contains(targetDay).click(); |
57 | | - }); |
58 | | - |
59 | | - return cy.wrap(subject); |
60 | | - }, |
61 | | - ); |
62 | | -} |
| 1 | +declare global { |
| 2 | + // eslint-disable-next-line @typescript-eslint/no-namespace |
| 3 | + namespace Cypress { |
| 4 | + interface Chainable { |
| 5 | + /** |
| 6 | + * Selects a given date into a QDate. |
| 7 | + * @example cy.get('.q-date').selectDate('11/02/2021') |
| 8 | + * @example cy.dataCy('start-date').selectDate(new Date()) |
| 9 | + */ |
| 10 | + selectDate<E extends HTMLElement = HTMLElement>( |
| 11 | + value: string | Date, |
| 12 | + ): Chainable<JQueryWithSelector<E>>; |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +export function registerSelectDate() { |
| 18 | + Cypress.Commands.add( |
| 19 | + 'selectDate', |
| 20 | + { prevSubject: 'element' }, |
| 21 | + (subject, value) => { |
| 22 | + if (!subject.hasClass('q-date')) { |
| 23 | + throw new Error('Subject is not a QDate'); |
| 24 | + } |
| 25 | + |
| 26 | + const targetDate = typeof value === 'string' ? new Date(value) : value; |
| 27 | + |
| 28 | + cy.wrap(subject).within(() => { |
| 29 | + cy.get('.q-date__navigation div:not(.q-date__arrow)') |
| 30 | + .last() |
| 31 | + .as('yearSelector'); |
| 32 | + cy.get('.q-date__navigation div:not(.q-date__arrow)') |
| 33 | + .first() |
| 34 | + .as('monthSelector'); |
| 35 | + |
| 36 | + const targetYear = targetDate.getFullYear(); |
| 37 | + const targetMonth = targetDate.getMonth(); |
| 38 | + const targetDay = targetDate.getDate(); |
| 39 | + |
| 40 | + // Since it's easy to detect it with the year selector, |
| 41 | + // we avoid selecting the year if it's already the target one |
| 42 | + cy.get('@yearSelector') |
| 43 | + .invoke('text') |
| 44 | + .then((currentYear) => { |
| 45 | + if (currentYear !== targetYear.toString()) { |
| 46 | + cy.get('@yearSelector').click(); |
| 47 | + cy.contains(targetYear).click(); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + cy.get('@monthSelector').click(); |
| 52 | + cy.get('.q-date__months-item').eq(targetMonth).click(); |
| 53 | + |
| 54 | + // The target day number is searched only into days buttons, |
| 55 | + // skipping filler and out of range days |
| 56 | + cy.get('.q-date__calendar-item--in').contains(targetDay).click(); |
| 57 | + }); |
| 58 | + |
| 59 | + return cy.wrap(subject); |
| 60 | + }, |
| 61 | + ); |
| 62 | +} |
0 commit comments