Skip to content

Commit c21760b

Browse files
committed
test(tests): created tests for all account settings components
1 parent e7f4958 commit c21760b

File tree

28 files changed

+509
-62
lines changed

28 files changed

+509
-62
lines changed

jest.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ module.exports = {
3434
'<rootDir>/src/app/features/registry/',
3535
'<rootDir>/src/app/features/project/',
3636
'<rootDir>/src/app/features/registries/',
37-
'<rootDir>/src/app/features/settings/',
3837
'<rootDir>/src/app/shared/',
3938
],
4039
};

src/app/features/settings/account-settings/components/add-email/add-email.component.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { provideStore } from '@ngxs/store';
1+
import { provideStore, Store } from '@ngxs/store';
22

33
import { TranslatePipe } from '@ngx-translate/core';
44
import { MockPipe, MockProviders } from 'ng-mocks';
@@ -9,31 +9,47 @@ import { provideHttpClient } from '@angular/common/http';
99
import { provideHttpClientTesting } from '@angular/common/http/testing';
1010
import { ComponentFixture, TestBed } from '@angular/core/testing';
1111

12+
import { TranslateServiceMock } from '@shared/mocks';
13+
import { ToastService } from '@shared/services';
14+
1215
import { AccountSettingsState } from '../../store';
1316

1417
import { AddEmailComponent } from './add-email.component';
1518

1619
describe('AddEmailComponent', () => {
1720
let component: AddEmailComponent;
1821
let fixture: ComponentFixture<AddEmailComponent>;
22+
let store: Store;
1923

2024
beforeEach(async () => {
2125
await TestBed.configureTestingModule({
2226
imports: [AddEmailComponent, MockPipe(TranslatePipe)],
2327
providers: [
2428
provideStore([AccountSettingsState]),
29+
MockProviders(DynamicDialogRef, ToastService),
30+
TranslateServiceMock,
2531
provideHttpClient(),
2632
provideHttpClientTesting(),
27-
MockProviders(DynamicDialogRef),
2833
],
2934
}).compileComponents();
3035

3136
fixture = TestBed.createComponent(AddEmailComponent);
3237
component = fixture.componentInstance;
38+
39+
store = TestBed.inject(Store);
40+
3341
fixture.detectChanges();
3442
});
3543

3644
it('should create', () => {
3745
expect(component).toBeTruthy();
3846
});
47+
48+
it('should not call action addEmail when email is invalid', () => {
49+
const actionSpy = jest.spyOn(store, 'dispatch');
50+
51+
component.addEmail();
52+
53+
expect(actionSpy).not.toHaveBeenCalled();
54+
});
3955
});
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import { provideStore } from '@ngxs/store';
1+
import { provideStore, Store } from '@ngxs/store';
22

33
import { TranslatePipe } from '@ngx-translate/core';
4-
import { MockPipe } from 'ng-mocks';
4+
import { MockPipe, MockProviders } from 'ng-mocks';
5+
6+
import { DynamicDialogRef } from 'primeng/dynamicdialog';
57

68
import { provideHttpClient } from '@angular/common/http';
79
import { provideHttpClientTesting } from '@angular/common/http/testing';
810
import { ComponentFixture, TestBed } from '@angular/core/testing';
911

1012
import { UserState } from '@osf/core/store/user';
13+
import { MOCK_INSTITUTION } from '@shared/mocks/institution.mock';
14+
import { CustomConfirmationService, ToastService } from '@shared/services';
1115

1216
import { AccountSettingsState } from '../../store';
1317

@@ -16,19 +20,53 @@ import { AffiliatedInstitutionsComponent } from './affiliated-institutions.compo
1620
describe('AffiliatedInstitutionsComponent', () => {
1721
let component: AffiliatedInstitutionsComponent;
1822
let fixture: ComponentFixture<AffiliatedInstitutionsComponent>;
23+
let confirmationService: CustomConfirmationService;
24+
let store: Store;
1925

2026
beforeEach(async () => {
2127
await TestBed.configureTestingModule({
2228
imports: [AffiliatedInstitutionsComponent, MockPipe(TranslatePipe)],
23-
providers: [provideStore([AccountSettingsState, UserState]), provideHttpClient(), provideHttpClientTesting()],
29+
providers: [
30+
provideStore([AccountSettingsState, UserState]),
31+
MockProviders(CustomConfirmationService, ToastService, DynamicDialogRef),
32+
provideHttpClient(),
33+
provideHttpClientTesting(),
34+
],
2435
}).compileComponents();
2536

2637
fixture = TestBed.createComponent(AffiliatedInstitutionsComponent);
2738
component = fixture.componentInstance;
39+
40+
confirmationService = TestBed.inject(CustomConfirmationService);
41+
store = TestBed.inject(Store);
42+
2843
fixture.detectChanges();
2944
});
3045

3146
it('should create', () => {
3247
expect(component).toBeTruthy();
3348
});
49+
50+
it('should call deleteInstitution on confirmation', () => {
51+
jest.spyOn(confirmationService, 'confirmDelete').mockImplementation(({ onConfirm }) => {
52+
onConfirm();
53+
});
54+
55+
component.deleteInstitution(MOCK_INSTITUTION);
56+
57+
expect(confirmationService.confirmDelete).toHaveBeenCalled();
58+
});
59+
60+
it('should not dispatch delete when user cancels confirmation', () => {
61+
const dispatchSpy = jest.spyOn(store, 'dispatch');
62+
63+
jest.spyOn(confirmationService, 'confirmDelete').mockImplementation(() => {
64+
// Simulate cancelling the confirmation
65+
});
66+
67+
component.deleteInstitution(MOCK_INSTITUTION);
68+
69+
expect(confirmationService.confirmDelete).toHaveBeenCalled();
70+
expect(dispatchSpy).not.toHaveBeenCalled();
71+
});
3472
});

src/app/features/settings/account-settings/components/cancel-deactivation/cancel-deactivation.component.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ describe('CancelDeactivationComponent', () => {
3636
it('should create', () => {
3737
expect(component).toBeTruthy();
3838
});
39+
40+
it('should close the dialog with true when cancelDeactivation is called', () => {
41+
const dialogRef = TestBed.inject(DynamicDialogRef);
42+
jest.spyOn(dialogRef, 'close');
43+
component.cancelDeactivation();
44+
expect(dialogRef.close).toHaveBeenCalledWith(true);
45+
});
3946
});
Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
import { provideStore } from '@ngxs/store';
1+
import { provideStore, Store } from '@ngxs/store';
22

3-
import { TranslatePipe, TranslateService } from '@ngx-translate/core';
3+
import { TranslatePipe } from '@ngx-translate/core';
44
import { MockPipe, MockProvider } from 'ng-mocks';
55

6-
import { provideHttpClient } from '@angular/common/http';
6+
import { of, throwError } from 'rxjs';
7+
8+
import { HttpErrorResponse, provideHttpClient } from '@angular/common/http';
79
import { provideHttpClientTesting } from '@angular/common/http/testing';
810
import { ComponentFixture, TestBed } from '@angular/core/testing';
911

12+
import { TranslateServiceMock } from '@shared/mocks';
13+
import { LoaderService, ToastService } from '@shared/services';
14+
1015
import { AccountSettingsState } from '../../store';
1116

1217
import { ChangePasswordComponent } from './change-password.component';
1318

1419
describe('ChangePasswordComponent', () => {
1520
let component: ChangePasswordComponent;
1621
let fixture: ComponentFixture<ChangePasswordComponent>;
22+
let store: Store;
1723

1824
beforeEach(async () => {
1925
await TestBed.configureTestingModule({
@@ -22,16 +28,47 @@ describe('ChangePasswordComponent', () => {
2228
provideStore([AccountSettingsState]),
2329
provideHttpClient(),
2430
provideHttpClientTesting(),
25-
MockProvider(TranslateService),
31+
TranslateServiceMock,
32+
MockProvider(LoaderService),
33+
MockProvider(ToastService),
2634
],
2735
}).compileComponents();
2836

2937
fixture = TestBed.createComponent(ChangePasswordComponent);
3038
component = fixture.componentInstance;
39+
store = TestBed.inject(Store);
40+
3141
fixture.detectChanges();
3242
});
3343

3444
it('should create', () => {
3545
expect(component).toBeTruthy();
3646
});
47+
48+
it('should change password when form is valid', () => {
49+
component.passwordForm.setValue({
50+
oldPassword: 'Oldpass1!',
51+
newPassword: 'Newpass1!',
52+
confirmPassword: 'Newpass1!',
53+
});
54+
const dispatchSpy = jest.spyOn(store, 'dispatch').mockReturnValue(of());
55+
component.changePassword();
56+
expect(dispatchSpy).toHaveBeenCalled();
57+
});
58+
59+
it('should display error message when backend returns error', () => {
60+
component.passwordForm.setValue({
61+
oldPassword: 'Oldpass1!',
62+
newPassword: 'Newpass1!',
63+
confirmPassword: 'Newpass1!',
64+
});
65+
const errorDetail = 'Current password is incorrect';
66+
const httpError = new HttpErrorResponse({
67+
status: 400,
68+
error: { errors: [{ detail: errorDetail }] },
69+
});
70+
jest.spyOn(store, 'dispatch').mockReturnValue(throwError(() => httpError));
71+
component.changePassword();
72+
expect(component['errorMessage']()).toBe(errorDetail);
73+
});
3774
});

src/app/features/settings/account-settings/components/confirmation-sent-dialog/confirmation-sent-dialog.component.spec.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
import { provideStore } from '@ngxs/store';
2-
31
import { TranslatePipe } from '@ngx-translate/core';
42
import { MockPipe, MockProviders } from 'ng-mocks';
53

6-
import { DynamicDialogRef } from 'primeng/dynamicdialog';
4+
import { DynamicDialogConfig, DynamicDialogRef } from 'primeng/dynamicdialog';
75

86
import { provideHttpClient } from '@angular/common/http';
97
import { provideHttpClientTesting } from '@angular/common/http/testing';
108
import { ComponentFixture, TestBed } from '@angular/core/testing';
119

12-
import { AccountSettingsState } from '@osf/features/settings/account-settings/store';
13-
1410
import { ConfirmationSentDialogComponent } from './confirmation-sent-dialog.component';
1511

1612
describe('ConfirmationSentDialogComponent', () => {
@@ -21,10 +17,9 @@ describe('ConfirmationSentDialogComponent', () => {
2117
await TestBed.configureTestingModule({
2218
imports: [ConfirmationSentDialogComponent, MockPipe(TranslatePipe)],
2319
providers: [
24-
provideStore([AccountSettingsState]),
20+
MockProviders(DynamicDialogRef, DynamicDialogConfig),
2521
provideHttpClient(),
2622
provideHttpClientTesting(),
27-
MockProviders(DynamicDialogRef),
2823
],
2924
}).compileComponents();
3025

0 commit comments

Comments
 (0)