Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/app/core/components/breadcrumb/breadcrumb.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import { of } from 'rxjs';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';

import { ProviderSelectors } from '@core/store/provider';
import { InstitutionsAdminSelectors } from '@osf/features/admin-institutions/store';
import { InstitutionsSearchSelectors } from '@shared/stores/institutions-search';

import { BreadcrumbComponent } from './breadcrumb.component';

import { provideMockStore } from '@testing/providers/store-provider.mock';

describe('Component: Breadcrumb', () => {
let component: BreadcrumbComponent;
let fixture: ComponentFixture<BreadcrumbComponent>;
Expand All @@ -17,26 +23,53 @@ describe('Component: Breadcrumb', () => {
};

const mockActivatedRoute = {
root: {
snapshot: {
url: [],
params: {},
data: {},
firstChild: null,
},
},
snapshot: {
data: { skipBreadcrumbs: false },
url: [],
params: {},
},
firstChild: null,
};

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [BreadcrumbComponent],
providers: [MockProvider(Router, mockRouter), { provide: ActivatedRoute, useValue: mockActivatedRoute }],
providers: [
MockProvider(Router, mockRouter),
{ provide: ActivatedRoute, useValue: mockActivatedRoute },
provideMockStore({
signals: [
{ selector: ProviderSelectors.getCurrentProvider, value: null },
{ selector: InstitutionsSearchSelectors.getInstitution, value: null },
{ selector: InstitutionsAdminSelectors.getInstitution, value: null },
],
}),
],
}).compileComponents();

fixture = TestBed.createComponent(BreadcrumbComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create and parse URL correctly', () => {
it('should create', () => {
expect(component).toBeTruthy();
expect(component['url']()).toBe('/test/path');
expect(component['parsedUrl']()).toEqual(['test', 'path']);
});

it('should show breadcrumb when skipBreadcrumbs is false', () => {
expect(component.showBreadcrumb()).toBe(true);
});

it('should build breadcrumbs from route', () => {
expect(component.breadcrumbs()).toBeDefined();
expect(Array.isArray(component.breadcrumbs())).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';

import { RequestAccessErrorDialogComponent } from './request-access-error-dialog.component';

import { DynamicDialogRefMock } from '@testing/mocks/dynamic-dialog-ref.mock';
import { OSFTestingModule } from '@testing/osf.testing.module';

describe('RequestAccessErrorDialogComponent', () => {
let component: RequestAccessErrorDialogComponent;
let fixture: ComponentFixture<RequestAccessErrorDialogComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RequestAccessErrorDialogComponent],
imports: [RequestAccessErrorDialogComponent, OSFTestingModule],
providers: [DynamicDialogRefMock],
}).compileComponents();

fixture = TestBed.createComponent(RequestAccessErrorDialogComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('CollectionsDiscoverComponent', () => {
beforeEach(async () => {
toastServiceMock = ToastServiceMockBuilder.create().build();
mockCustomDialogService = CustomDialogServiceMockBuilder.create().build();
mockRoute = ActivatedRouteMockBuilder.create().withParams({ id: 'provider-1' }).build();
mockRoute = ActivatedRouteMockBuilder.create().withParams({ providerId: 'provider-1' }).build();

await TestBed.configureTestingModule({
imports: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { of } from 'rxjs';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';

import { SetDefaultFilterValue } from '@osf/shared/stores/global-search';
import { FetchInstitutionById, InstitutionsSearchSelectors } from '@osf/shared/stores/institutions-search';
import { InstitutionsSearchSelectors } from '@osf/shared/stores/institutions-search';
import { GlobalSearchComponent, LoadingSpinnerComponent } from '@shared/components';
import { MOCK_INSTITUTION } from '@shared/mocks';

Expand Down Expand Up @@ -49,31 +48,25 @@ describe('Component: Institutions Search', () => {

store = TestBed.inject(Store) as jest.Mocked<Store>;
store.dispatch = jest.fn().mockReturnValue(of(undefined));

fixture.detectChanges();
});

it('should create', () => {
fixture.detectChanges();
expect(component).toBeTruthy();
});

it('should fetch institution and set default filter value on ngOnInit when institution-id is provided', () => {
activatedRouteMock.snapshot!.params = { 'institution-id': MOCK_INSTITUTION.id };
it('should fetch institution and set default filter value on ngOnInit when institutionId is provided', () => {
activatedRouteMock.snapshot!.params = { institutionId: MOCK_INSTITUTION.id };

store.dispatch.mockReturnValue(of(undefined));

component.ngOnInit();
fixture.detectChanges();

expect(store.dispatch).toHaveBeenCalledWith(new FetchInstitutionById(MOCK_INSTITUTION.id));
expect(store.dispatch).toHaveBeenCalledWith(
new SetDefaultFilterValue('affiliation,isContainedBy.affiliation', MOCK_INSTITUTION.iris.join(','))
);
expect(store.dispatch).toHaveBeenCalled();
});

it('should not fetch institution on ngOnInit when institution-id is not provided', () => {
it('should not fetch institution on ngOnInit when institutionId is not provided', () => {
activatedRouteMock.snapshot!.params = {};

component.ngOnInit();
fixture.detectChanges();

expect(store.dispatch).not.toHaveBeenCalled();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import { MockComponents } from 'ng-mocks';
import { MockComponents, MockProvider } from 'ng-mocks';

import { of } from 'rxjs';

import { ComponentFixture, TestBed } from '@angular/core/testing';

import { EducationHistoryComponent, EmploymentHistoryComponent } from '@osf/shared/components';
import { IS_MEDIUM } from '@osf/shared/helpers';
import { SocialModel, UserModel } from '@osf/shared/models';
import { EducationHistoryComponent, EmploymentHistoryComponent } from '@shared/components';
import { MOCK_USER } from '@shared/mocks';

import { ProfileInformationComponent } from './profile-information.component';

import { MOCK_EDUCATION, MOCK_EMPLOYMENT } from '@testing/mocks/user-employment-education.mock';
import { OSFTestingModule } from '@testing/osf.testing.module';

describe('ProfileInformationComponent', () => {
let component: ProfileInformationComponent;
let fixture: ComponentFixture<ProfileInformationComponent>;

const mockUser: UserModel = MOCK_USER;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
ProfileInformationComponent,
...MockComponents(EmploymentHistoryComponent, EducationHistoryComponent),
OSFTestingModule,
...MockComponents(EmploymentHistoryComponent, EducationHistoryComponent),
],
providers: [MockProvider(IS_MEDIUM, of(false))],
}).compileComponents();

fixture = TestBed.createComponent(ProfileInformationComponent);
Expand All @@ -29,4 +38,145 @@ describe('ProfileInformationComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('should initialize with default inputs', () => {
expect(component.currentUser()).toBeUndefined();
expect(component.showEdit()).toBe(false);
});

it('should accept user input', () => {
fixture.componentRef.setInput('currentUser', mockUser);
fixture.detectChanges();
expect(component.currentUser()).toEqual(mockUser);
});

it('should accept showEdit input', () => {
fixture.componentRef.setInput('showEdit', true);
fixture.detectChanges();
expect(component.showEdit()).toBe(true);
});

it('should return true when user has employment', () => {
fixture.componentRef.setInput('currentUser', {
...mockUser,
employment: MOCK_EMPLOYMENT,
education: [],
});
fixture.detectChanges();
expect(component.isEmploymentAndEducationVisible()).toBeTruthy();
});

it('should return true when user has education', () => {
fixture.componentRef.setInput('currentUser', {
...mockUser,
employment: [],
education: MOCK_EDUCATION,
});
fixture.detectChanges();
expect(component.isEmploymentAndEducationVisible()).toBeTruthy();
});

it('should return true when user has both employment and education', () => {
fixture.componentRef.setInput('currentUser', mockUser);
fixture.detectChanges();
expect(component.isEmploymentAndEducationVisible()).toBeTruthy();
});

it('should return falsy when user has neither employment nor education', () => {
fixture.componentRef.setInput('currentUser', {
...mockUser,
employment: [],
education: [],
});
fixture.detectChanges();
expect(component.isEmploymentAndEducationVisible()).toBeFalsy();
});

it('should return falsy when currentUser is null', () => {
fixture.componentRef.setInput('currentUser', null);
fixture.detectChanges();
expect(component.isEmploymentAndEducationVisible()).toBeFalsy();
});

it('should map user social data to view models', () => {
fixture.componentRef.setInput('currentUser', mockUser);
fixture.detectChanges();

const socials = component.userSocials();
expect(socials).toBeDefined();
expect(socials.length).toBeGreaterThan(0);
});

it('should include GitHub social link when present', () => {
fixture.componentRef.setInput('currentUser', mockUser);
fixture.detectChanges();

const socials = component.userSocials();
const github = socials.find((s) => s.icon.includes('github'));
expect(github).toBeDefined();
expect(github?.url).toContain('github.com');
});

it('should include Twitter social link when present', () => {
fixture.componentRef.setInput('currentUser', mockUser);
fixture.detectChanges();

const socials = component.userSocials();
const twitter = socials.find((s) => s.icon.includes('x.svg'));
expect(twitter).toBeDefined();
expect(twitter?.url).toContain('x.com');
});

it('should include LinkedIn social link when present', () => {
fixture.componentRef.setInput('currentUser', mockUser);
fixture.detectChanges();

const socials = component.userSocials();
const linkedin = socials.find((s) => s.icon.includes('linkedin'));
expect(linkedin).toBeDefined();
expect(linkedin?.url).toContain('linkedin.com');
});

it('should return empty array when user has no social data', () => {
fixture.componentRef.setInput('currentUser', {
...mockUser,
social: {} as SocialModel,
});
fixture.detectChanges();

const socials = component.userSocials();
expect(socials).toEqual([]);
});

it('should return empty array when currentUser is null', () => {
fixture.componentRef.setInput('currentUser', null);
fixture.detectChanges();

const socials = component.userSocials();
expect(socials).toEqual([]);
});

it('should not include profileWebsites in social links', () => {
fixture.componentRef.setInput('currentUser', mockUser);
fixture.detectChanges();

const socials = component.userSocials();
const websites = socials.filter((s) => s.alt === 'settings.profileSettings.social.labels.profileWebsites');
expect(websites.length).toBe(0);
});

it('should emit editProfile event when called', (done) => {
component.editProfile.subscribe(() => {
expect(true).toBe(true);
done();
});

component.toProfileSettings();
});

it('should emit editProfile event on button click', () => {
jest.spyOn(component.editProfile, 'emit');
component.toProfileSettings();
expect(component.editProfile.emit).toHaveBeenCalled();
});
});
Loading
Loading