|
| 1 | +import { provideHttpClient } from '@angular/common/http'; |
| 2 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { FormsModule } from '@angular/forms'; |
| 4 | +import { MatSnackBarModule } from '@angular/material/snack-bar'; |
| 5 | +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 6 | +import { provideRouter, Router } from '@angular/router'; |
| 7 | +import { IPasswordStrengthMeterService } from 'angular-password-strength-meter'; |
| 8 | +import { Angulartics2Module } from 'angulartics2'; |
| 9 | +import { SelfhostedService } from 'src/app/services/selfhosted.service'; |
| 10 | +import { SetupComponent } from './setup.component'; |
| 11 | + |
| 12 | +type SetupComponentTestable = SetupComponent & { |
| 13 | + email: ReturnType<typeof import('@angular/core').signal<string>>; |
| 14 | + password: ReturnType<typeof import('@angular/core').signal<string>>; |
| 15 | + submitting: ReturnType<typeof import('@angular/core').signal<boolean>>; |
| 16 | +}; |
| 17 | + |
| 18 | +describe('SetupComponent', () => { |
| 19 | + let component: SetupComponent; |
| 20 | + let fixture: ComponentFixture<SetupComponent>; |
| 21 | + let selfhostedService: SelfhostedService; |
| 22 | + let router: Router; |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + await TestBed.configureTestingModule({ |
| 26 | + imports: [FormsModule, MatSnackBarModule, SetupComponent, BrowserAnimationsModule, Angulartics2Module.forRoot()], |
| 27 | + providers: [provideHttpClient(), provideRouter([]), { provide: IPasswordStrengthMeterService, useValue: {} }], |
| 28 | + }).compileComponents(); |
| 29 | + }); |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + fixture = TestBed.createComponent(SetupComponent); |
| 33 | + component = fixture.componentInstance; |
| 34 | + selfhostedService = TestBed.inject(SelfhostedService); |
| 35 | + router = TestBed.inject(Router); |
| 36 | + fixture.detectChanges(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should create', () => { |
| 40 | + expect(component).toBeTruthy(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should update email signal on change', () => { |
| 44 | + const testable = component as SetupComponentTestable; |
| 45 | + component.onEmailChange('[email protected]'); |
| 46 | + expect(testable.email()).toBe('[email protected]'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should update password signal on change', () => { |
| 50 | + const testable = component as SetupComponentTestable; |
| 51 | + component.onPasswordChange('SecurePass123'); |
| 52 | + expect(testable.password()).toBe('SecurePass123'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should not submit if email is empty', async () => { |
| 56 | + const testable = component as SetupComponentTestable; |
| 57 | + const fakeCreateInitialUser = vi.spyOn(selfhostedService, 'createInitialUser'); |
| 58 | + |
| 59 | + testable.email.set(''); |
| 60 | + testable.password.set('SecurePass123'); |
| 61 | + |
| 62 | + await component.createAdminAccount(); |
| 63 | + expect(fakeCreateInitialUser).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should not submit if password is empty', async () => { |
| 67 | + const testable = component as SetupComponentTestable; |
| 68 | + const fakeCreateInitialUser = vi.spyOn(selfhostedService, 'createInitialUser'); |
| 69 | + |
| 70 | + testable.email.set('[email protected]'); |
| 71 | + testable.password.set(''); |
| 72 | + |
| 73 | + await component.createAdminAccount(); |
| 74 | + expect(fakeCreateInitialUser).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should create admin account and navigate to login on success', async () => { |
| 78 | + const testable = component as SetupComponentTestable; |
| 79 | + const fakeCreateInitialUser = vi.spyOn(selfhostedService, 'createInitialUser').mockResolvedValue({ success: true }); |
| 80 | + const fakeNavigate = vi.spyOn(router, 'navigate').mockResolvedValue(true); |
| 81 | + |
| 82 | + testable.email.set('[email protected]'); |
| 83 | + testable.password.set('SecurePass123'); |
| 84 | + |
| 85 | + await component.createAdminAccount(); |
| 86 | + |
| 87 | + expect(fakeCreateInitialUser).toHaveBeenCalledWith({ |
| 88 | + |
| 89 | + password: 'SecurePass123', |
| 90 | + }); |
| 91 | + expect(testable.submitting()).toBe(false); |
| 92 | + expect(fakeNavigate).toHaveBeenCalledWith(['/login']); |
| 93 | + }); |
| 94 | +}); |
0 commit comments