|
| 1 | +import { mount } from 'cypress/react'; |
| 2 | +import { ThemeProvider } from '@mui/material/styles'; |
| 3 | +import React from 'react'; |
| 4 | +import globalTheme from 'modules/theme'; |
| 5 | +import BillingProfileDropdown from './BillingProfileDropdown'; |
| 6 | + |
| 7 | +interface BillingProfile { |
| 8 | + id: string; |
| 9 | + profileName?: string; |
| 10 | + [key: string]: any; |
| 11 | +} |
| 12 | + |
| 13 | +const mockBillingProfiles: BillingProfile[] = [ |
| 14 | + { |
| 15 | + id: 'profile-1', |
| 16 | + profileName: 'Default Profile', |
| 17 | + }, |
| 18 | + { |
| 19 | + id: 'profile-2', |
| 20 | + profileName: 'Alternative Profile', |
| 21 | + }, |
| 22 | + { |
| 23 | + id: 'profile-3', |
| 24 | + profileName: 'Test Profile', |
| 25 | + }, |
| 26 | +]; |
| 27 | + |
| 28 | +const setUp = (overrideProps = {}) => { |
| 29 | + const props = { |
| 30 | + billingProfiles: mockBillingProfiles, |
| 31 | + selectedBillingProfile: mockBillingProfiles[0], |
| 32 | + onSelectedItem: cy.stub(), |
| 33 | + ...overrideProps, |
| 34 | + }; |
| 35 | + |
| 36 | + mount( |
| 37 | + <ThemeProvider theme={globalTheme}> |
| 38 | + <BillingProfileDropdown {...props} /> |
| 39 | + </ThemeProvider>, |
| 40 | + ); |
| 41 | + |
| 42 | + return props; |
| 43 | +}; |
| 44 | + |
| 45 | +describe('BillingProfileDropdown Component', () => { |
| 46 | + // No need for beforeEach since we create fresh stubs in each test |
| 47 | + |
| 48 | + describe('Rendering', () => { |
| 49 | + it('should render the billing profile label by default', () => { |
| 50 | + setUp(); |
| 51 | + |
| 52 | + cy.contains('Billing Profile').should('be.visible'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should not render label when showLabel is false', () => { |
| 56 | + setUp({ showLabel: false }); |
| 57 | + |
| 58 | + cy.contains('Billing Profile').should('not.exist'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should render dropdown with correct initial value', () => { |
| 62 | + setUp(); |
| 63 | + |
| 64 | + cy.get('[data-cy="billingProfile"]').should('contain', 'Default Profile'); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('Profile Selection', () => { |
| 69 | + it('should display all available billing profiles', () => { |
| 70 | + setUp(); |
| 71 | + |
| 72 | + cy.get('[data-cy="billingProfile"]').click(); |
| 73 | + cy.contains('Default Profile').should('exist'); |
| 74 | + cy.contains('Alternative Profile').should('exist'); |
| 75 | + cy.contains('Test Profile').should('exist'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should call onSelectedItem when profile is selected', () => { |
| 79 | + const props = setUp(); |
| 80 | + |
| 81 | + cy.get('[data-cy="billingProfile"]').click(); |
| 82 | + cy.contains('Alternative Profile').click(); |
| 83 | + |
| 84 | + cy.wrap(props.onSelectedItem).should('have.been.calledOnce'); |
| 85 | + cy.wrap(props.onSelectedItem).should('have.been.calledWith', mockBillingProfiles[1]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should filter out profiles without profileName', () => { |
| 89 | + const profilesWithUndefined = [ |
| 90 | + ...mockBillingProfiles, |
| 91 | + { id: 'profile-4' }, // Missing profileName |
| 92 | + { id: 'profile-5', profileName: undefined }, |
| 93 | + ]; |
| 94 | + |
| 95 | + setUp({ billingProfiles: profilesWithUndefined }); |
| 96 | + |
| 97 | + cy.get('[data-cy="billingProfile"]').click(); |
| 98 | + cy.contains('Default Profile').should('exist'); |
| 99 | + cy.contains('Alternative Profile').should('exist'); |
| 100 | + cy.contains('Test Profile').should('exist'); |
| 101 | + // Profiles without profileName should not appear |
| 102 | + cy.get('[data-cy="menuItem-undefined"]').should('not.exist'); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('Disabled State', () => { |
| 107 | + it('should be disabled when only one profile is available', () => { |
| 108 | + setUp({ billingProfiles: [mockBillingProfiles[0]] }); |
| 109 | + |
| 110 | + cy.get('[data-cy="billingProfile"]').should('have.class', 'Mui-disabled'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should be disabled when disabled prop is true', () => { |
| 114 | + setUp({ disabled: true }); |
| 115 | + |
| 116 | + cy.get('[data-cy="billingProfile"]').should('have.class', 'Mui-disabled'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should be enabled when multiple profiles available and not explicitly disabled', () => { |
| 120 | + setUp(); |
| 121 | + |
| 122 | + cy.get('[data-cy="billingProfile"]').should('not.have.attr', 'aria-disabled'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('Empty State', () => { |
| 127 | + it('should handle empty billing profiles array', () => { |
| 128 | + setUp({ billingProfiles: [], selectedBillingProfile: null }); |
| 129 | + |
| 130 | + cy.get('[data-cy="billingProfile"]').should('have.value', ''); |
| 131 | + cy.get('[data-cy="billingProfile"]').should('have.class', 'Mui-disabled'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle null selectedBillingProfile', () => { |
| 135 | + setUp({ selectedBillingProfile: null }); |
| 136 | + |
| 137 | + cy.get('[data-cy="billingProfile"]').should('have.value', ''); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe('Styling and Props', () => { |
| 142 | + it('should apply custom sx props', () => { |
| 143 | + const customSx = { height: '3rem', marginTop: '16px', backgroundColor: 'red' }; |
| 144 | + setUp({ sx: customSx }); |
| 145 | + |
| 146 | + cy.get('[data-cy="billingProfile"]').should('exist'); |
| 147 | + // Note: Testing exact styles is complex in Cypress, but we can verify the component renders |
| 148 | + }); |
| 149 | + |
| 150 | + it('should handle duplicate profile names correctly', () => { |
| 151 | + const duplicateProfiles = [ |
| 152 | + { id: 'profile-1', profileName: 'Same Name' }, |
| 153 | + { id: 'profile-2', profileName: 'Same Name' }, |
| 154 | + { id: 'profile-3', profileName: 'Different Name' }, |
| 155 | + ]; |
| 156 | + |
| 157 | + setUp({ billingProfiles: duplicateProfiles }); |
| 158 | + |
| 159 | + cy.get('[data-cy="billingProfile"]').click(); |
| 160 | + // Should only show unique profile names |
| 161 | + cy.contains('Same Name').should('exist'); |
| 162 | + cy.contains('Different Name').should('exist'); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('Integration', () => { |
| 167 | + it('should work with real-world profile data structure', () => { |
| 168 | + it('should call onSelectedItem with the correct profile when selection changes', () => { |
| 169 | + const realWorldProfiles = [ |
| 170 | + { |
| 171 | + id: 'bp-12345', |
| 172 | + profileName: 'Production Environment', |
| 173 | + billingAccountId: 'ba-prod-001', |
| 174 | + description: 'Production billing profile for live workloads', |
| 175 | + }, |
| 176 | + { |
| 177 | + id: 'bp-67890', |
| 178 | + profileName: 'Development Environment', |
| 179 | + billingAccountId: 'ba-dev-001', |
| 180 | + description: 'Development billing profile for testing', |
| 181 | + }, |
| 182 | + ]; |
| 183 | + |
| 184 | + const props = setUp({ |
| 185 | + billingProfiles: realWorldProfiles, |
| 186 | + selectedBillingProfile: realWorldProfiles[0], |
| 187 | + }); |
| 188 | + |
| 189 | + cy.get('[data-cy="billingProfile"]').should('contain', 'Production Environment'); |
| 190 | + |
| 191 | + cy.get('[data-cy="billingProfile"]').click(); |
| 192 | + cy.contains('Development Environment').click(); |
| 193 | + |
| 194 | + cy.wrap(props.onSelectedItem).should('have.been.calledOnce'); |
| 195 | + cy.wrap(props.onSelectedItem.getCall(0).args[0]) |
| 196 | + .should('have.property', 'id', 'bp-67890') |
| 197 | + .should('have.property', 'profileName', 'Development Environment'); |
| 198 | + }); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments