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