|
| 1 | +import { CreateWorkspaceDialogContainer } from './CreateWorkspaceDialogContainer'; |
| 2 | +import { useCreateWorkspace, CreateWorkspaceParams } from '../../hooks/useCreateWorkspace'; |
| 3 | +import { useAuthOnboarding } from '../../spaces/onboarding/auth/AuthContextOnboarding'; |
| 4 | + |
| 5 | +describe('CreateWorkspaceDialogContainer', () => { |
| 6 | + let createWorkspacePayload: CreateWorkspaceParams | null = null; |
| 7 | + |
| 8 | + const fakeUseCreateWorkspace: typeof useCreateWorkspace = () => ({ |
| 9 | + createWorkspace: async (data: CreateWorkspaceParams): Promise<void> => { |
| 10 | + createWorkspacePayload = data; |
| 11 | + }, |
| 12 | + isLoading: false, |
| 13 | + }); |
| 14 | + |
| 15 | + const fakeUseAuthOnboarding = (() => ({ |
| 16 | + user: { |
| 17 | + email: 'name@domain.com', |
| 18 | + }, |
| 19 | + })) as typeof useAuthOnboarding; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + createWorkspacePayload = null; |
| 23 | + }); |
| 24 | + |
| 25 | + it('creates a workspace with valid data', () => { |
| 26 | + const setIsOpen = cy.stub(); |
| 27 | + |
| 28 | + cy.mount( |
| 29 | + <CreateWorkspaceDialogContainer |
| 30 | + useCreateWorkspace={fakeUseCreateWorkspace} |
| 31 | + useAuthOnboarding={fakeUseAuthOnboarding} |
| 32 | + isOpen={true} |
| 33 | + setIsOpen={setIsOpen} |
| 34 | + project="test-project" |
| 35 | + />, |
| 36 | + ); |
| 37 | + |
| 38 | + const expectedPayload = { |
| 39 | + name: 'test-workspace', |
| 40 | + displayName: 'Test Workspace Display Name', |
| 41 | + chargingTarget: '12345678-1234-1234-1234-123456789abc', |
| 42 | + chargingTargetType: 'btp', |
| 43 | + members: [ |
| 44 | + { |
| 45 | + name: 'name@domain.com', |
| 46 | + roles: ['admin'], |
| 47 | + kind: 'User', |
| 48 | + }, |
| 49 | + ], |
| 50 | + }; |
| 51 | + |
| 52 | + // Fill in the form (using Shadow DOM selectors) |
| 53 | + cy.get('#name').find('input[id*="inner"]').type('test-workspace'); |
| 54 | + cy.get('#displayName').find('input[id*="inner"]').type('Test Workspace Display Name'); |
| 55 | + |
| 56 | + // Select charging target type |
| 57 | + cy.get('#chargingTargetType').click(); |
| 58 | + cy.contains('BTP').click(); |
| 59 | + |
| 60 | + // Fill charging target |
| 61 | + cy.get('#chargingTarget').find('input[id*="inner"]').type('12345678-1234-1234-1234-123456789abc'); |
| 62 | + |
| 63 | + // Submit the form |
| 64 | + cy.get('ui5-button').contains('Create').click(); |
| 65 | + |
| 66 | + // Verify the hook was called with correct data |
| 67 | + cy.then(() => cy.wrap(createWorkspacePayload).deepEqualJson(expectedPayload)); |
| 68 | + |
| 69 | + // Dialog should close on success |
| 70 | + cy.wrap(setIsOpen).should('have.been.calledWith', false); |
| 71 | + }); |
| 72 | + |
| 73 | + it('validates required fields', () => { |
| 74 | + const setIsOpen = cy.stub(); |
| 75 | + |
| 76 | + cy.mount( |
| 77 | + <CreateWorkspaceDialogContainer |
| 78 | + useCreateWorkspace={fakeUseCreateWorkspace} |
| 79 | + useAuthOnboarding={fakeUseAuthOnboarding} |
| 80 | + isOpen={true} |
| 81 | + setIsOpen={setIsOpen} |
| 82 | + project="test-project" |
| 83 | + />, |
| 84 | + ); |
| 85 | + |
| 86 | + // Try to submit without filling required fields |
| 87 | + cy.get('ui5-button').contains('Create').click(); |
| 88 | + |
| 89 | + // Should show validation errors - check for value-state="Negative" attribute |
| 90 | + cy.get('#name').should('have.attr', 'value-state', 'Negative'); |
| 91 | + |
| 92 | + // Or check if error message exists in DOM (even if hidden by CSS) |
| 93 | + cy.contains('This field is required').should('exist'); |
| 94 | + |
| 95 | + // Dialog should not close |
| 96 | + cy.wrap(setIsOpen).should('not.have.been.called'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('validates charging target format for BTP', () => { |
| 100 | + const setIsOpen = cy.stub(); |
| 101 | + |
| 102 | + cy.mount( |
| 103 | + <CreateWorkspaceDialogContainer |
| 104 | + useCreateWorkspace={fakeUseCreateWorkspace} |
| 105 | + useAuthOnboarding={fakeUseAuthOnboarding} |
| 106 | + isOpen={true} |
| 107 | + setIsOpen={setIsOpen} |
| 108 | + project="test-project" |
| 109 | + />, |
| 110 | + ); |
| 111 | + |
| 112 | + cy.get('#name').find('input[id*="inner"]').type('test-workspace'); |
| 113 | + cy.get('#chargingTargetType').click(); |
| 114 | + cy.contains('BTP').click(); |
| 115 | + |
| 116 | + // Invalid format |
| 117 | + cy.get('#chargingTarget').find('input[id*="inner"]').type('invalid-format'); |
| 118 | + cy.get('ui5-button').contains('Create').click(); |
| 119 | + |
| 120 | + // Should show validation error - check for value-state="Negative" attribute |
| 121 | + cy.get('#chargingTarget').should('have.attr', 'value-state', 'Negative'); |
| 122 | + |
| 123 | + // Dialog should not close |
| 124 | + cy.wrap(setIsOpen).should('not.have.been.called'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should not close dialog when creation fails', () => { |
| 128 | + const failingUseCreateWorkspace: typeof useCreateWorkspace = () => ({ |
| 129 | + createWorkspace: async (): Promise<void> => { |
| 130 | + throw new Error('Creation failed'); // Simulate failure by throwing error |
| 131 | + }, |
| 132 | + isLoading: false, |
| 133 | + }); |
| 134 | + |
| 135 | + const setIsOpen = cy.stub(); |
| 136 | + |
| 137 | + cy.mount( |
| 138 | + <CreateWorkspaceDialogContainer |
| 139 | + useCreateWorkspace={failingUseCreateWorkspace} |
| 140 | + useAuthOnboarding={fakeUseAuthOnboarding} |
| 141 | + isOpen={true} |
| 142 | + setIsOpen={setIsOpen} |
| 143 | + project="test-project" |
| 144 | + />, |
| 145 | + ); |
| 146 | + |
| 147 | + // Fill in the form |
| 148 | + cy.get('#name').find('input[id*="inner"]').type('test-workspace'); |
| 149 | + cy.get('#chargingTargetType').click(); |
| 150 | + cy.contains('BTP').click(); |
| 151 | + cy.get('#chargingTarget').find('input[id*="inner"]').type('12345678-1234-1234-1234-123456789abc'); |
| 152 | + |
| 153 | + // Submit the form |
| 154 | + cy.get('ui5-button').contains('Create').click(); |
| 155 | + |
| 156 | + // Dialog should NOT close on failure |
| 157 | + cy.wrap(setIsOpen).should('not.have.been.called'); |
| 158 | + |
| 159 | + // Dialog should still be visible |
| 160 | + cy.contains('Create').should('be.visible'); |
| 161 | + }); |
| 162 | +}); |
0 commit comments