|
| 1 | +import { CreateGitRepositoryDialog } from './CreateGitRepositoryDialog'; |
| 2 | +import { CreateGitRepositoryParams } from '../../hooks/useCreateGitRepository'; |
| 3 | + |
| 4 | +describe('CreateGitRepositoryDialog', () => { |
| 5 | + let capturedData: CreateGitRepositoryParams | null = null; |
| 6 | + |
| 7 | + const fakeUseCreateGitRepository = () => ({ |
| 8 | + createGitRepository: async (data: CreateGitRepositoryParams): Promise<void> => { |
| 9 | + capturedData = data; |
| 10 | + }, |
| 11 | + isLoading: false, |
| 12 | + }); |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + capturedData = null; |
| 16 | + }); |
| 17 | + |
| 18 | + it('creates a git repository with valid data', () => { |
| 19 | + const onClose = cy.stub(); |
| 20 | + |
| 21 | + cy.mount( |
| 22 | + <CreateGitRepositoryDialog isOpen={true} useCreateGitRepository={fakeUseCreateGitRepository} onClose={onClose} />, |
| 23 | + ); |
| 24 | + |
| 25 | + const expectedPayload = { |
| 26 | + namespace: 'default', |
| 27 | + name: 'test-repo', |
| 28 | + interval: '5m0s', |
| 29 | + url: 'https://github.com/test/repo', |
| 30 | + branch: 'develop', |
| 31 | + secretRef: '', |
| 32 | + }; |
| 33 | + |
| 34 | + // Fill in the form |
| 35 | + cy.get('[name="name"]').typeIntoUi5Input('test-repo'); |
| 36 | + cy.get('[name="interval"]').find('input').clear().type('5m0s'); |
| 37 | + |
| 38 | + cy.get('[name="url"]').typeIntoUi5Input('https://github.com/test/repo'); |
| 39 | + cy.get('[name="branch"]').find('input').clear().type('develop'); |
| 40 | + |
| 41 | + // Submit the form |
| 42 | + cy.get('ui5-button').contains('Create').click(); |
| 43 | + |
| 44 | + // Verify the hook was called with correct data |
| 45 | + cy.then(() => cy.wrap(capturedData).deepEqualJson(expectedPayload)); |
| 46 | + |
| 47 | + // Dialog should close on success |
| 48 | + cy.wrap(onClose).should('have.been.called'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('includes secretRef when provided', () => { |
| 52 | + const onClose = cy.stub(); |
| 53 | + |
| 54 | + cy.mount( |
| 55 | + <CreateGitRepositoryDialog isOpen={true} useCreateGitRepository={fakeUseCreateGitRepository} onClose={onClose} />, |
| 56 | + ); |
| 57 | + |
| 58 | + const expectedPayload = { |
| 59 | + namespace: 'default', |
| 60 | + name: 'test-repo', |
| 61 | + interval: '1m0s', |
| 62 | + url: 'https://github.com/test/repo', |
| 63 | + branch: 'main', |
| 64 | + secretRef: 'my-git-secret', |
| 65 | + }; |
| 66 | + |
| 67 | + // Fill in the form |
| 68 | + cy.get('[name="name"]').typeIntoUi5Input('test-repo'); |
| 69 | + cy.get('[name="url"]').typeIntoUi5Input('https://github.com/test/repo'); |
| 70 | + cy.get('[name="secretRef"]').typeIntoUi5Input('my-git-secret'); |
| 71 | + |
| 72 | + // Submit the form |
| 73 | + cy.get('ui5-button').contains('Create').click(); |
| 74 | + |
| 75 | + // Verify the hook was called with correct data |
| 76 | + cy.then(() => cy.wrap(capturedData).deepEqualJson(expectedPayload)); |
| 77 | + |
| 78 | + // Dialog should close on success |
| 79 | + cy.wrap(onClose).should('have.been.called'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('validates required fields', () => { |
| 83 | + const onClose = cy.stub(); |
| 84 | + |
| 85 | + cy.mount( |
| 86 | + <CreateGitRepositoryDialog isOpen={true} useCreateGitRepository={fakeUseCreateGitRepository} onClose={onClose} />, |
| 87 | + ); |
| 88 | + |
| 89 | + // Try to submit without filling required fields |
| 90 | + cy.get('ui5-button').contains('Create').click(); |
| 91 | + |
| 92 | + // Should show validation errors |
| 93 | + cy.get('[name="name"]').should('have.attr', 'value-state', 'Negative'); |
| 94 | + cy.contains('This field is required').should('exist'); |
| 95 | + |
| 96 | + // Dialog should not close |
| 97 | + cy.wrap(onClose).should('not.have.been.called'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('validates URL format', () => { |
| 101 | + const onClose = cy.stub(); |
| 102 | + |
| 103 | + cy.mount( |
| 104 | + <CreateGitRepositoryDialog isOpen={true} useCreateGitRepository={fakeUseCreateGitRepository} onClose={onClose} />, |
| 105 | + ); |
| 106 | + |
| 107 | + cy.get('[name="name"]').typeIntoUi5Input('test-repo'); |
| 108 | + cy.get('[name="interval"]').find('input').clear().type('1m0s'); |
| 109 | + cy.get('[name="branch"]').find('input').clear().type('main'); |
| 110 | + |
| 111 | + // Test 1: Invalid string |
| 112 | + cy.get('[name="url"]').find('input').clear().type('not-a-valid-url'); |
| 113 | + cy.get('ui5-button').contains('Create').click(); |
| 114 | + cy.get('[name="url"]').should('have.attr', 'value-state', 'Negative'); |
| 115 | + cy.contains('Must be a valid HTTPS URL').should('exist'); |
| 116 | + |
| 117 | + // Test 2: HTTP protocol (should fail if we require HTTPS) |
| 118 | + cy.get('[name="url"]').find('input').clear().type('http://github.com/test/repo'); |
| 119 | + cy.get('ui5-button').contains('Create').click(); |
| 120 | + cy.get('[name="url"]').should('have.attr', 'value-state', 'Negative'); |
| 121 | + cy.contains('Must be a valid HTTPS URL').should('exist'); |
| 122 | + |
| 123 | + // Test 3: Valid HTTPS URL (should pass) |
| 124 | + cy.get('[name="url"]').find('input').clear().type('https://github.com/test/repo'); |
| 125 | + cy.get('ui5-button').contains('Create').click(); |
| 126 | + |
| 127 | + // Dialog should close on success |
| 128 | + cy.wrap(onClose).should('have.been.called'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('closes dialog when cancel is clicked', () => { |
| 132 | + const onClose = cy.stub(); |
| 133 | + |
| 134 | + cy.mount( |
| 135 | + <CreateGitRepositoryDialog isOpen={true} useCreateGitRepository={fakeUseCreateGitRepository} onClose={onClose} />, |
| 136 | + ); |
| 137 | + |
| 138 | + // Fill in some data |
| 139 | + cy.get('[name="name"]').typeIntoUi5Input('test-repo'); |
| 140 | + |
| 141 | + // Click cancel |
| 142 | + cy.get('ui5-button').contains('Cancel').click(); |
| 143 | + |
| 144 | + // Dialog should close without calling onSuccess |
| 145 | + cy.wrap(onClose).should('have.been.called'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('uses default values for interval and branch', () => { |
| 149 | + const onClose = cy.stub(); |
| 150 | + |
| 151 | + cy.mount( |
| 152 | + <CreateGitRepositoryDialog isOpen={true} useCreateGitRepository={fakeUseCreateGitRepository} onClose={onClose} />, |
| 153 | + ); |
| 154 | + |
| 155 | + // Check default values |
| 156 | + cy.get('[name="interval"]').find('input').should('have.value', '1m0s'); |
| 157 | + cy.get('[name="branch"]').find('input').should('have.value', 'main'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should not close dialog when creation fails', () => { |
| 161 | + const failingUseCreateGitRepository = () => ({ |
| 162 | + createGitRepository: async (): Promise<void> => { |
| 163 | + throw new Error('Creation failed'); |
| 164 | + }, |
| 165 | + isLoading: false, |
| 166 | + }); |
| 167 | + |
| 168 | + const onClose = cy.stub(); |
| 169 | + |
| 170 | + cy.mount( |
| 171 | + <CreateGitRepositoryDialog |
| 172 | + isOpen={true} |
| 173 | + useCreateGitRepository={failingUseCreateGitRepository} |
| 174 | + onClose={onClose} |
| 175 | + />, |
| 176 | + ); |
| 177 | + |
| 178 | + // Fill in the form |
| 179 | + cy.get('[name="name"]').typeIntoUi5Input('test-repo'); |
| 180 | + cy.get('[name="interval"]').find('input').clear().type('1m0s'); |
| 181 | + cy.get('[name="url"]').find('input').type('https://github.com/test/repo'); |
| 182 | + cy.get('[name="branch"]').find('input').clear().type('main'); |
| 183 | + |
| 184 | + // Submit the form |
| 185 | + cy.get('ui5-button').contains('Create').click(); |
| 186 | + |
| 187 | + // Dialog should NOT close on failure |
| 188 | + cy.wrap(onClose).should('not.have.been.called'); |
| 189 | + |
| 190 | + // Dialog should still be visible |
| 191 | + cy.contains('Create Git Repository').should('be.visible'); |
| 192 | + }); |
| 193 | +}); |
0 commit comments