|
| 1 | +import { |
| 2 | + interceptDeleteDestination, |
| 3 | + mockDeleteDestination, |
| 4 | + mockGetDestination, |
| 5 | + mockGetDestinations, |
| 6 | +} from 'support/intercepts/delivery'; |
| 7 | +import { mockAppendFeatureFlags } from 'support/intercepts/feature-flags'; |
| 8 | +import { ui } from 'support/ui'; |
| 9 | + |
| 10 | +import { destinationFactory } from 'src/factories'; |
| 11 | + |
| 12 | +import type { Destination } from '@linode/api-v4'; |
| 13 | + |
| 14 | +function checkActionMenu(tableAlias: string, mockDestinations: Destination[]) { |
| 15 | + mockDestinations.forEach((destination) => { |
| 16 | + cy.get(tableAlias) |
| 17 | + .find('tbody tr') |
| 18 | + .should('contain', destination.label) |
| 19 | + .then(() => { |
| 20 | + // If the row contains the label, proceed with clicking the action menu |
| 21 | + ui.actionMenu |
| 22 | + .findByTitle(`Action menu for Destination ${destination.label}`) |
| 23 | + .should('be.visible') |
| 24 | + .click(); |
| 25 | + |
| 26 | + // Check that all items are enabled |
| 27 | + ui.actionMenuItem |
| 28 | + .findByTitle('Edit') |
| 29 | + .should('be.visible') |
| 30 | + .should('be.enabled'); |
| 31 | + |
| 32 | + ui.actionMenuItem |
| 33 | + .findByTitle('Delete') |
| 34 | + .should('be.visible') |
| 35 | + .should('be.enabled'); |
| 36 | + }); |
| 37 | + |
| 38 | + // Close the action menu by clicking on Delivery Title of the screen |
| 39 | + cy.get('body').click(0, 0); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +function deleteItem(tableAlias: string, destination: Destination) { |
| 44 | + cy.get(tableAlias) |
| 45 | + .find('tbody tr') |
| 46 | + .should('contain', destination.label) |
| 47 | + .then(() => { |
| 48 | + // If the row contains the label, proceed with clicking the action menu |
| 49 | + ui.actionMenu |
| 50 | + .findByTitle(`Action menu for Destination ${destination.label}`) |
| 51 | + .should('be.visible') |
| 52 | + .click(); |
| 53 | + |
| 54 | + mockDeleteDestination(404); // @TODO remove after API release on prod |
| 55 | + interceptDeleteDestination().as('deleteDestination'); |
| 56 | + |
| 57 | + // Delete destination |
| 58 | + ui.actionMenuItem.findByTitle('Delete').click(); |
| 59 | + |
| 60 | + // Find confirmation modal |
| 61 | + cy.findByText( |
| 62 | + `Are you sure you want to delete "${destination.label}" destination?` |
| 63 | + ); |
| 64 | + ui.button.findByTitle('Delete').click(); |
| 65 | + |
| 66 | + cy.wait('@deleteDestination'); |
| 67 | + |
| 68 | + // Close confirmation modal after failure |
| 69 | + ui.button.findByTitle('Cancel').click(); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function editItemViaActionMenu(tableAlias: string, destination: Destination) { |
| 74 | + cy.get(tableAlias) |
| 75 | + .find('tbody tr') |
| 76 | + .should('contain', destination.label) |
| 77 | + .then(() => { |
| 78 | + // If the row contains the label, proceed with clicking the action menu |
| 79 | + ui.actionMenu |
| 80 | + .findByTitle(`Action menu for Destination ${destination.label}`) |
| 81 | + .should('be.visible') |
| 82 | + .click(); |
| 83 | + |
| 84 | + mockGetDestination(destination); |
| 85 | + // Edit destination redirect |
| 86 | + ui.actionMenuItem.findByTitle('Edit').click(); |
| 87 | + cy.url().should('endWith', `/destinations/${destination.id}/edit`); |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +const mockDestinations: Destination[] = new Array(3) |
| 92 | + .fill(null) |
| 93 | + .map((_item: null, index: number): Destination => { |
| 94 | + return destinationFactory.build({ |
| 95 | + label: `Destination ${index}`, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | +describe('destinations landing checks for non-empty state', () => { |
| 100 | + beforeEach(() => { |
| 101 | + mockAppendFeatureFlags({ |
| 102 | + aclpLogs: { enabled: true, beta: true }, |
| 103 | + }); |
| 104 | + |
| 105 | + // Mock setup to display the Destinations landing page in a non-empty state |
| 106 | + mockGetDestinations(mockDestinations).as('getDestinations'); |
| 107 | + |
| 108 | + // Alias the mockDestinations array |
| 109 | + cy.wrap(mockDestinations).as('mockDestinations'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('checks create destination button is enabled and user can see existing destinations', () => { |
| 113 | + // Login and wait for application to load |
| 114 | + cy.visitWithLogin('/logs/delivery/destinations'); |
| 115 | + cy.wait('@getDestinations'); |
| 116 | + cy.url().should('endWith', '/destinations'); |
| 117 | + |
| 118 | + cy.get('table').should('exist').as('destinationsTable'); |
| 119 | + |
| 120 | + // Assert that Create Destination button is visible and enabled |
| 121 | + ui.button |
| 122 | + .findByTitle('Create Destination') |
| 123 | + .should('be.visible') |
| 124 | + .and('be.enabled'); |
| 125 | + |
| 126 | + // Assert that the correct number of Destinations entries are present in the DestinationsTable |
| 127 | + cy.get('@destinationsTable') |
| 128 | + .find('tbody tr') |
| 129 | + .should('have.length', mockDestinations.length); |
| 130 | + |
| 131 | + checkActionMenu('@destinationsTable', mockDestinations); // For the recovery destination table |
| 132 | + }); |
| 133 | + |
| 134 | + it('checks actions from destination menu actions', () => { |
| 135 | + cy.visitWithLogin('/logs/delivery/destinations'); |
| 136 | + cy.wait('@getDestinations'); |
| 137 | + cy.get('table').should('exist').as('destinationsTable'); |
| 138 | + |
| 139 | + const exampleDestination = mockDestinations[0]; |
| 140 | + deleteItem('@destinationsTable', exampleDestination); |
| 141 | + |
| 142 | + mockGetDestination(exampleDestination).as('getDestination'); |
| 143 | + |
| 144 | + // Redirect to destination edit page via name |
| 145 | + cy.findByText(exampleDestination.label).click(); |
| 146 | + cy.url().should('endWith', `/destinations/${exampleDestination.id}/edit`); |
| 147 | + cy.wait('@getDestination'); |
| 148 | + |
| 149 | + cy.visit('/logs/delivery/destinations'); |
| 150 | + cy.get('table').should('exist').as('destinationsTable'); |
| 151 | + cy.wait('@getDestinations'); |
| 152 | + editItemViaActionMenu('@destinationsTable', exampleDestination); |
| 153 | + }); |
| 154 | +}); |
0 commit comments