|
| 1 | +import { provideZonelessChangeDetection, signal, WritableSignal } from '@angular/core'; |
| 2 | +import { provideHttpClient } from '@angular/common/http'; |
| 3 | +import { provideHttpClientTesting } from '@angular/common/http/testing'; |
| 4 | +import { provideRouter } from '@angular/router'; |
| 5 | +import { provideNoopAnimations } from '@angular/platform-browser/animations'; |
| 6 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 7 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 8 | + |
| 9 | +import { ServiceKeysComponent } from './service-keys.component'; |
| 10 | +import { ServiceCatalogDataService, ServiceKeyView } from '../../../services/endpoint-data/service-catalog-data.service'; |
| 11 | + |
| 12 | +// SignalSource triple with a static value — null instance ⇒ the component |
| 13 | +// builds without firing the offering/bindable fetch. |
| 14 | +function source<T>(value: T) { |
| 15 | + return { |
| 16 | + value: signal(value).asReadonly(), |
| 17 | + isLoading: signal(false).asReadonly(), |
| 18 | + error: signal(null).asReadonly(), |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +const spinner = (el: HTMLElement) => el.querySelector('.animate-spin'); |
| 23 | + |
| 24 | +describe('ServiceKeysComponent — busy-state spinners', () => { |
| 25 | + let component: ServiceKeysComponent; |
| 26 | + let fixture: ComponentFixture<ServiceKeysComponent>; |
| 27 | + let keysVal: WritableSignal<ServiceKeyView[]>; |
| 28 | + let keysLoading: WritableSignal<boolean>; |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + keysVal = signal<ServiceKeyView[]>([]); |
| 32 | + keysLoading = signal(false); |
| 33 | + |
| 34 | + await TestBed.configureTestingModule({ |
| 35 | + imports: [ServiceKeysComponent], |
| 36 | + providers: [ |
| 37 | + provideZonelessChangeDetection(), |
| 38 | + provideHttpClient(), |
| 39 | + provideHttpClientTesting(), |
| 40 | + provideRouter([]), |
| 41 | + provideNoopAnimations(), |
| 42 | + { |
| 43 | + provide: ServiceCatalogDataService, |
| 44 | + useValue: { |
| 45 | + serviceInstance: () => source(null), |
| 46 | + // Backed by the writable signals above so tests can drive the |
| 47 | + // list value + loading flag through the component's source. |
| 48 | + serviceKeysForInstance: () => ({ |
| 49 | + value: keysVal.asReadonly(), |
| 50 | + isLoading: keysLoading.asReadonly(), |
| 51 | + error: signal(null).asReadonly(), |
| 52 | + }), |
| 53 | + }, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }).compileComponents(); |
| 57 | + |
| 58 | + fixture = TestBed.createComponent(ServiceKeysComponent); |
| 59 | + component = fixture.componentInstance; |
| 60 | + fixture.detectChanges(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('no spinner when idle', () => { |
| 64 | + expect(spinner(fixture.nativeElement)).toBeNull(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('shows a spinner while the key list is loading (GET)', () => { |
| 68 | + keysLoading.set(true); |
| 69 | + fixture.detectChanges(); |
| 70 | + expect(spinner(fixture.nativeElement)).not.toBeNull(); |
| 71 | + |
| 72 | + keysLoading.set(false); |
| 73 | + fixture.detectChanges(); |
| 74 | + expect(spinner(fixture.nativeElement)).toBeNull(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('shows a spinner in the create form while creating', () => { |
| 78 | + component.isAdding.set(true); |
| 79 | + fixture.detectChanges(); |
| 80 | + expect(spinner(fixture.nativeElement)).toBeNull(); |
| 81 | + |
| 82 | + component.creating.set(true); |
| 83 | + fixture.detectChanges(); |
| 84 | + expect(spinner(fixture.nativeElement)).not.toBeNull(); |
| 85 | + |
| 86 | + component.creating.set(false); |
| 87 | + fixture.detectChanges(); |
| 88 | + expect(spinner(fixture.nativeElement)).toBeNull(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('shows a spinner while a key\'s credentials are loading (GET)', () => { |
| 92 | + keysVal.set([{ guid: 'k1', name: 'key-one', createdAt: '' }]); |
| 93 | + fixture.detectChanges(); |
| 94 | + expect(spinner(fixture.nativeElement)).toBeNull(); |
| 95 | + |
| 96 | + // Expanding triggers the lazy credential GET; the request stays pending |
| 97 | + // under HttpTestingController, so credsLoading('k1') stays true. |
| 98 | + component.toggleOpen('k1'); |
| 99 | + fixture.detectChanges(); |
| 100 | + expect(spinner(fixture.nativeElement)).not.toBeNull(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('shows a spinner on the delete action while deleting', () => { |
| 104 | + keysVal.set([{ guid: 'k1', name: 'key-one', createdAt: '' }]); |
| 105 | + fixture.detectChanges(); |
| 106 | + expect(spinner(fixture.nativeElement)).toBeNull(); |
| 107 | + |
| 108 | + // Fire-and-forget: the DELETE stays pending, rowStatus('k1') === 'busy'. |
| 109 | + void component.deleteKey('k1'); |
| 110 | + fixture.detectChanges(); |
| 111 | + expect(spinner(fixture.nativeElement)).not.toBeNull(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments