|
1 | 1 | /* eslint-disable no-undef */
|
2 | 2 |
|
| 3 | +const flashClassMap = { |
| 4 | + warning: 'warning', |
| 5 | + error: 'danger', |
| 6 | + info: 'info', |
| 7 | + success: 'success', |
| 8 | +}; |
| 9 | + |
3 | 10 | /**
|
4 | 11 | * Custom Cypress command to validate flash messages.
|
5 | 12 | * @param {string} alertType - Type of alert (success, warning, error, info).
|
6 | 13 | * @param {string} [containsText] - Optional text that the alert should contain.
|
7 | 14 | * @returns {Cypress.Chainable} - The alert element if found, or an assertion failure.
|
8 | 15 | */
|
9 |
| -Cypress.Commands.add('expect_flash', (alertType = 'success', containsText) => { |
10 |
| - let alertClassName; |
11 |
| - switch (alertType) { |
12 |
| - case 'warning': |
13 |
| - alertClassName = 'warning'; |
14 |
| - break; |
15 |
| - case 'error': |
16 |
| - alertClassName = 'danger'; |
17 |
| - break; |
18 |
| - case 'info': |
19 |
| - alertClassName = 'info'; |
20 |
| - break; |
21 |
| - default: |
22 |
| - alertClassName = 'success'; |
23 |
| - break; |
24 |
| - } |
25 |
| - const alert = cy |
26 |
| - .get(`#main_div #flash_msg_div .alert-${alertClassName}`) |
27 |
| - .should('be.visible'); |
| 16 | +Cypress.Commands.add( |
| 17 | + 'expect_flash', |
| 18 | + (flashType = flashClassMap.success, containsText) => { |
| 19 | + const alertClassName = flashClassMap[flashType] || flashClassMap.success; |
| 20 | + const alert = cy |
| 21 | + .get(`#main_div #flash_msg_div .alert-${alertClassName}`) |
| 22 | + .should('be.visible'); |
28 | 23 |
|
29 |
| - if (containsText) { |
30 |
| - return alert.should(($el) => { |
31 |
| - const actualText = $el.text().toLowerCase(); |
32 |
| - expect(actualText).to.include(containsText.toLowerCase()); |
33 |
| - }); |
34 |
| - } |
| 24 | + if (containsText) { |
| 25 | + return alert.should(($el) => { |
| 26 | + const actualText = $el.text().toLowerCase(); |
| 27 | + expect(actualText).to.include(containsText.toLowerCase()); |
| 28 | + }); |
| 29 | + } |
35 | 30 |
|
36 |
| - return alert; |
37 |
| -}); |
| 31 | + return alert; |
| 32 | + } |
| 33 | +); |
38 | 34 |
|
| 35 | +/** |
| 36 | + * Custom Cypress command to validate browser confirm alerts. |
| 37 | + * @param {Object} options - Options for the command. |
| 38 | + * @param {Function<Cypress.Chainable>} options.confirmTriggerFn - A function that triggers the confirm dialog. |
| 39 | + * This function **must return a Cypress.Chainable**, like `cy.get(...).click()`, |
| 40 | + * so that Cypress can properly wait and chain `.then()` afterward. |
| 41 | + * @example |
| 42 | + * cy.expectBrowserConfirm({ |
| 43 | + * containsText: 'sure to proceed?', |
| 44 | + * proceed: true, |
| 45 | + * confirmTriggerFn: () => { |
| 46 | + * return cy.get('[data-testid="delete"]').click() |
| 47 | + * } |
| 48 | + * }); |
| 49 | + * @example |
| 50 | + * cy.expectBrowserConfirm({ |
| 51 | + * confirmTriggerFn: () => cy.contains('deleted').click() |
| 52 | + * }); |
| 53 | + * @param {string} [options.containsText] - Optional text that the confirm alert should contain. |
| 54 | + * @param {boolean} [options.proceed=true] - Whether to proceed with the confirm (true = OK, false = Cancel). |
| 55 | + */ |
39 | 56 | Cypress.Commands.add(
|
40 |
| - 'listen_for_browser_confirm_alert', |
41 |
| - (containsText, proceed = true) => { |
| 57 | + 'expect_browser_confirm_with_text', |
| 58 | + ({ confirmTriggerFn, containsText, proceed = true }) => { |
| 59 | + let alertTriggered = false; |
42 | 60 | cy.on('window:confirm', (actualText) => {
|
| 61 | + alertTriggered = true; |
43 | 62 | if (containsText) {
|
44 | 63 | expect(actualText.toLowerCase()).to.include(containsText.toLowerCase());
|
45 | 64 | }
|
46 | 65 | return proceed; // true = OK, false = Cancel
|
47 | 66 | });
|
| 67 | + // Fires the event that triggers the confirm dialog |
| 68 | + confirmTriggerFn().then(() => { |
| 69 | + expect(alertTriggered, 'Expected browser confirm alert to be triggered') |
| 70 | + .to.be.true; |
| 71 | + }); |
48 | 72 | }
|
49 | 73 | );
|
0 commit comments