|
| 1 | +import { DeleteResourceModalComponent } from './delete-resource-modal.component'; |
| 2 | +import { CommonModule } from '@angular/common'; |
| 3 | +import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; |
| 4 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 5 | +import { ReactiveFormsModule } from '@angular/forms'; |
| 6 | + |
| 7 | +describe('DeleteResourceModalComponent', () => { |
| 8 | + let component: DeleteResourceModalComponent; |
| 9 | + let fixture: ComponentFixture<DeleteResourceModalComponent>; |
| 10 | + let mockDialog: any; |
| 11 | + |
| 12 | + const resource: any = { metadata: { name: 'TestName' } }; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + await TestBed.configureTestingModule({ |
| 16 | + imports: [CommonModule, ReactiveFormsModule], |
| 17 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 18 | + }) |
| 19 | + .overrideComponent(DeleteResourceModalComponent, { |
| 20 | + set: { |
| 21 | + imports: [CommonModule, ReactiveFormsModule], |
| 22 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 23 | + }, |
| 24 | + }) |
| 25 | + .compileComponents(); |
| 26 | + |
| 27 | + fixture = TestBed.createComponent(DeleteResourceModalComponent); |
| 28 | + component = fixture.componentInstance; |
| 29 | + |
| 30 | + mockDialog = { open: false }; |
| 31 | + (component as any).dialog = () => mockDialog; |
| 32 | + |
| 33 | + component.ngOnInit(); |
| 34 | + fixture.detectChanges(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should create the component', () => { |
| 38 | + expect(component).toBeTruthy(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should initialize the form with "resource" control', () => { |
| 42 | + expect(component.form).toBeDefined(); |
| 43 | + expect(component.form.controls['resource']).toBeDefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should set dialog open and store innerResource', () => { |
| 47 | + component.open(resource); |
| 48 | + expect(mockDialog.open).toBeTruthy(); |
| 49 | + expect(component.innerResource()).toBe(resource); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should set dialog closed when closing', () => { |
| 53 | + mockDialog.open = true; |
| 54 | + component.close(); |
| 55 | + expect(mockDialog.open).toBeFalsy(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should be invalid when empty or mismatched; valid when matches innerResource.name', () => { |
| 59 | + component.open(resource); |
| 60 | + const control = component.form.controls['resource']; |
| 61 | + |
| 62 | + control.setValue(''); |
| 63 | + control.markAsTouched(); |
| 64 | + fixture.detectChanges(); |
| 65 | + expect(control.invalid).toBeTruthy(); |
| 66 | + expect(control.hasError('invalidResource')).toBeTruthy(); |
| 67 | + |
| 68 | + control.setValue('WrongName'); |
| 69 | + fixture.detectChanges(); |
| 70 | + expect(control.invalid).toBeTruthy(); |
| 71 | + expect(control.hasError('invalidResource')).toBeTruthy(); |
| 72 | + |
| 73 | + control.setValue('TestName'); |
| 74 | + fixture.detectChanges(); |
| 75 | + expect(control.valid).toBeTruthy(); |
| 76 | + expect(control.errors).toBeNull(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should emit the resource and close the dialog when deleting resource', () => { |
| 80 | + component.open(resource); |
| 81 | + spyOn(component.resource, 'emit'); |
| 82 | + component.delete(); |
| 83 | + expect(component.resource.emit).toHaveBeenCalledWith(resource); |
| 84 | + expect(mockDialog.open).toBeFalsy(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should set value and marks touched/dirty', () => { |
| 88 | + const control = component.form.controls['resource']; |
| 89 | + spyOn(control, 'setValue'); |
| 90 | + spyOn(control, 'markAsTouched'); |
| 91 | + spyOn(control, 'markAsDirty'); |
| 92 | + |
| 93 | + component.setFormControlValue( |
| 94 | + { target: { value: 'SomeValue' } } as any, |
| 95 | + 'resource', |
| 96 | + ); |
| 97 | + |
| 98 | + expect(control.setValue).toHaveBeenCalledWith('SomeValue'); |
| 99 | + expect(control.markAsTouched).toHaveBeenCalled(); |
| 100 | + expect(control.markAsDirty).toHaveBeenCalled(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should return "Negative" for invalid+touched, else "None"', () => { |
| 104 | + const control = component.form.controls['resource']; |
| 105 | + |
| 106 | + control.setValue(''); |
| 107 | + control.markAsTouched(); |
| 108 | + fixture.detectChanges(); |
| 109 | + expect(component.getValueState('resource')).toBe('Negative'); |
| 110 | + |
| 111 | + component.open(resource); |
| 112 | + control.setValue('TestName'); |
| 113 | + fixture.detectChanges(); |
| 114 | + expect(component.getValueState('resource')).toBe('None'); |
| 115 | + |
| 116 | + control.setValue(''); |
| 117 | + control.markAsUntouched(); |
| 118 | + fixture.detectChanges(); |
| 119 | + expect(component.getValueState('resource')).toBe('None'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should mark the control as touched', () => { |
| 123 | + const control = component.form.controls['resource']; |
| 124 | + spyOn(control, 'markAsTouched'); |
| 125 | + component.onFieldBlur('resource'); |
| 126 | + expect(control.markAsTouched).toHaveBeenCalled(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should render title with resource name in lowercase in the header', () => { |
| 130 | + component.open(resource); |
| 131 | + fixture.detectChanges(); |
| 132 | + const title = fixture.nativeElement.querySelector('ui5-title'); |
| 133 | + expect(title?.textContent?.toLowerCase()).toContain('delete testname'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should render prompt text with resource name and cannot be undone note', () => { |
| 137 | + component.open(resource); |
| 138 | + (component as any).context = () => ({ |
| 139 | + resourceDefinition: { singular: 'resource' }, |
| 140 | + }); |
| 141 | + fixture.detectChanges(); |
| 142 | + const content = fixture.nativeElement.querySelector('section.content'); |
| 143 | + const text = content?.textContent?.toLowerCase() || ''; |
| 144 | + expect(text).toContain('are you sure you want to delete'); |
| 145 | + expect(text).toContain('testname'); |
| 146 | + expect(text).toContain('cannot'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should bind input value to form control and show Negative valueState when invalid and touched', () => { |
| 150 | + component.open(resource); |
| 151 | + fixture.detectChanges(); |
| 152 | + |
| 153 | + const inputEl: HTMLElement & { |
| 154 | + value?: string; |
| 155 | + valueState?: string; |
| 156 | + dispatchEvent?: any; |
| 157 | + } = fixture.nativeElement.querySelector('ui5-input'); |
| 158 | + expect(inputEl).toBeTruthy(); |
| 159 | + |
| 160 | + component.setFormControlValue( |
| 161 | + { target: { value: 'wrong' } } as any, |
| 162 | + 'resource', |
| 163 | + ); |
| 164 | + component.onFieldBlur('resource'); |
| 165 | + fixture.detectChanges(); |
| 166 | + |
| 167 | + expect(component.form.controls['resource'].invalid).toBeTruthy(); |
| 168 | + expect(component.getValueState('resource')).toBe('Negative'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should close dialog when Cancel button clicked', () => { |
| 172 | + component.open(resource); |
| 173 | + mockDialog.open = true; |
| 174 | + fixture.detectChanges(); |
| 175 | + |
| 176 | + const cancelBtn: HTMLElement = fixture.nativeElement.querySelector( |
| 177 | + 'ui5-toolbar-button[design="Transparent"]', |
| 178 | + ); |
| 179 | + expect(cancelBtn).toBeTruthy(); |
| 180 | + |
| 181 | + cancelBtn.dispatchEvent(new Event('click')); |
| 182 | + fixture.detectChanges(); |
| 183 | + expect(mockDialog.open).toBeFalsy(); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should reset control state on close (pristine, untouched, revalidated)', () => { |
| 187 | + component.open(resource); |
| 188 | + const control = component.form.controls['resource']; |
| 189 | + control.setValue('wrong'); |
| 190 | + control.markAsTouched(); |
| 191 | + control.markAsDirty(); |
| 192 | + fixture.detectChanges(); |
| 193 | + expect(control.invalid).toBeTruthy(); |
| 194 | + |
| 195 | + component.close(); |
| 196 | + fixture.detectChanges(); |
| 197 | + |
| 198 | + expect(control.value).toBeNull(); |
| 199 | + expect(control.pristine).toBeTruthy(); |
| 200 | + expect(control.touched).toBeFalsy(); |
| 201 | + expect(control.invalid).toBeTruthy(); |
| 202 | + expect(control.hasError('invalidResource')).toBeTruthy(); |
| 203 | + }); |
| 204 | +}); |
0 commit comments