|
| 1 | +import { k8sNameValidator } from './k8s-name-validator'; |
| 2 | +import { AbstractControl } from '@angular/forms'; |
| 3 | + |
| 4 | +describe('k8sNameValidator', () => { |
| 5 | + const createControl = (value: any): AbstractControl => |
| 6 | + ({ |
| 7 | + value, |
| 8 | + }) as AbstractControl; |
| 9 | + |
| 10 | + it('should return null when control value is null', () => { |
| 11 | + const control = createControl(null); |
| 12 | + const result = k8sNameValidator(control); |
| 13 | + expect(result).toBeNull(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return null when control value is undefined', () => { |
| 17 | + const control = createControl(undefined); |
| 18 | + const result = k8sNameValidator(control); |
| 19 | + expect(result).toBeNull(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return null when control value is empty string', () => { |
| 23 | + const control = createControl(''); |
| 24 | + const result = k8sNameValidator(control); |
| 25 | + expect(result).toBeNull(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should return null for valid k8s name (single character)', () => { |
| 29 | + const control = createControl('a'); |
| 30 | + const result = k8sNameValidator(control); |
| 31 | + expect(result).toBeNull(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should return null for valid k8s name (with numbers and hyphens)', () => { |
| 35 | + const control = createControl('my-app-123'); |
| 36 | + const result = k8sNameValidator(control); |
| 37 | + expect(result).toBeNull(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return null for valid k8s name (maximum length)', () => { |
| 41 | + const control = createControl('a'.repeat(63)); |
| 42 | + const result = k8sNameValidator(control); |
| 43 | + expect(result).toBeNull(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return error for invalid k8s name starting with number', () => { |
| 47 | + const control = createControl('123invalid'); |
| 48 | + const result = k8sNameValidator(control); |
| 49 | + expect(result).toEqual({ k8sNameInvalid: true }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return error for invalid k8s name starting with uppercase', () => { |
| 53 | + const control = createControl('Invalid'); |
| 54 | + const result = k8sNameValidator(control); |
| 55 | + expect(result).toEqual({ k8sNameInvalid: true }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return error for invalid k8s name ending with hyphen', () => { |
| 59 | + const control = createControl('invalid-'); |
| 60 | + const result = k8sNameValidator(control); |
| 61 | + expect(result).toEqual({ k8sNameInvalid: true }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return error for invalid k8s name with uppercase letters', () => { |
| 65 | + const control = createControl('Invalid-Name'); |
| 66 | + const result = k8sNameValidator(control); |
| 67 | + expect(result).toEqual({ k8sNameInvalid: true }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return error for invalid k8s name exceeding maximum length', () => { |
| 71 | + const control = createControl('a'.repeat(64)); |
| 72 | + const result = k8sNameValidator(control); |
| 73 | + expect(result).toEqual({ k8sNameInvalid: true }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return error for invalid k8s name with special characters', () => { |
| 77 | + const control = createControl('invalid@name'); |
| 78 | + const result = k8sNameValidator(control); |
| 79 | + expect(result).toEqual({ k8sNameInvalid: true }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should handle non-string values by converting to string', () => { |
| 83 | + const control = createControl(123); |
| 84 | + const result = k8sNameValidator(control); |
| 85 | + expect(result).toEqual({ k8sNameInvalid: true }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments