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