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
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ module.exports = {
'<rootDir>/src/app/app.routes.ts',
'<rootDir>/src/app/features/files/components',
'<rootDir>/src/app/features/files/pages/file-detail',
'<rootDir>/src/app/features/preprints/',
'<rootDir>/src/app/features/project/addons/',
'<rootDir>/src/app/features/project/overview/',
'<rootDir>/src/app/features/project/registrations',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { MockProvider } from 'ng-mocks';

import { PaginatorState } from 'primeng/paginator';

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormControl } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';

import { InstitutionsSelectors } from '@osf/shared/stores/institutions';
Expand Down Expand Up @@ -54,69 +53,38 @@ describe.skip('Component: Institutions List', () => {
expect(component).toBeTruthy();
});

it('should update currentPage, first, and call updateQueryParams when page is provided', () => {
const paginatorEvent: PaginatorState = {
page: 1,
first: 20,
rows: 10,
pageCount: 5,
};

component.onPageChange(paginatorEvent);

expect(component.currentPage()).toBe(2);
expect(component.first()).toBe(20);
expect(routerMock.navigate).toHaveBeenCalledWith([], {
relativeTo: expect.any(Object),
queryParams: {
page: '2',
size: '10',
},
queryParamsHandling: 'merge',
});
it('should initialize with correct default values', () => {
expect(component.classes).toBe('flex-1 flex flex-column w-full');
expect(component.searchControl).toBeInstanceOf(FormControl);
expect(component.searchControl.value).toBe('');
});

it('should return institutions from store', () => {
const institutions = component.institutions();
expect(institutions).toBe(mockInstitutions);
});

it('should return loading state from store', () => {
const loading = component.institutionsLoading();
expect(loading).toBe(false);
});

it('should set currentPage to 1 when page is not provided', () => {
const paginatorEvent: PaginatorState = {
page: undefined,
first: 0,
rows: 20,
pageCount: 3,
};

component.onPageChange(paginatorEvent);

expect(component.currentPage()).toBe(1);
expect(component.first()).toBe(0);
expect(routerMock.navigate).toHaveBeenCalledWith([], {
relativeTo: expect.any(Object),
queryParams: {
page: '1',
size: '20',
},
queryParamsHandling: 'merge',
});
it('should handle search control value changes', () => {
const searchValue = 'test search';
component.searchControl.setValue(searchValue);

expect(component.searchControl.value).toBe(searchValue);
});

it('should handle first being undefined', () => {
const paginatorEvent: PaginatorState = {
page: 2,
first: undefined,
rows: 15,
pageCount: 4,
};

component.onPageChange(paginatorEvent);

expect(component.currentPage()).toBe(2);
expect(component.first()).toBe(0);
expect(routerMock.navigate).toHaveBeenCalledWith([], {
relativeTo: expect.any(Object),
queryParams: {
page: '2',
size: '15',
},
queryParamsHandling: 'merge',
});
it('should handle empty search', () => {
component.searchControl.setValue('');

expect(component.searchControl.value).toBe('');
});

it('should handle null search value', () => {
component.searchControl.setValue(null);

expect(component.searchControl.value).toBe(null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ describe('AdvisoryBoardComponent', () => {
let component: AdvisoryBoardComponent;
let fixture: ComponentFixture<AdvisoryBoardComponent>;

const mockHtmlContent =
'<div class="advisory-content"><h2>Advisory Board</h2><p>This is advisory board content.</p></div>';

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AdvisoryBoardComponent],
Expand All @@ -19,4 +22,71 @@ describe('AdvisoryBoardComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('should have default input values', () => {
expect(component.htmlContent()).toBeNull();
expect(component.brand()).toBeUndefined();
expect(component.isLandingPage()).toBe(false);
});

it('should not render section when htmlContent is null', () => {
fixture.detectChanges();

const compiled = fixture.nativeElement;
const section = compiled.querySelector('section');

expect(section).toBeNull();
});

it('should not render section when htmlContent is undefined', () => {
fixture.componentRef.setInput('htmlContent', undefined);
fixture.detectChanges();

const compiled = fixture.nativeElement;
const section = compiled.querySelector('section');

expect(section).toBeNull();
});

it('should render section when htmlContent is provided', () => {
fixture.componentRef.setInput('htmlContent', mockHtmlContent);
fixture.detectChanges();

const compiled = fixture.nativeElement;
const section = compiled.querySelector('section');

expect(section).toBeTruthy();
expect(section.innerHTML).toBe(mockHtmlContent);
});

it('should apply correct CSS classes when isLandingPage is false', () => {
fixture.componentRef.setInput('htmlContent', mockHtmlContent);
fixture.componentRef.setInput('isLandingPage', false);
fixture.detectChanges();

const compiled = fixture.nativeElement;
const section = compiled.querySelector('section');

expect(section).toBeTruthy();
expect(section.classList.contains('osf-preprint-service')).toBe(false);
expect(section.classList.contains('preprints-advisory-board-section')).toBe(true);
expect(section.classList.contains('pt-3')).toBe(true);
expect(section.classList.contains('pb-5')).toBe(true);
expect(section.classList.contains('px-3')).toBe(true);
expect(section.classList.contains('flex')).toBe(true);
expect(section.classList.contains('flex-column')).toBe(true);
});

it('should apply correct CSS classes when isLandingPage is true', () => {
fixture.componentRef.setInput('htmlContent', mockHtmlContent);
fixture.componentRef.setInput('isLandingPage', true);
fixture.detectChanges();

const compiled = fixture.nativeElement;
const section = compiled.querySelector('section');

expect(section).toBeTruthy();
expect(section.classList.contains('osf-preprint-service')).toBe(true);
expect(section.classList.contains('preprints-advisory-board-section')).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,34 +1,143 @@
import { TranslatePipe } from '@ngx-translate/core';
import { MockPipe } from 'ng-mocks';

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

import { TranslateServiceMock } from '@shared/mocks';
import { ResourceType } from '@shared/enums';
import { SubjectModel } from '@shared/models';

import { BrowseBySubjectsComponent } from './browse-by-subjects.component';

import { SUBJECTS_MOCK } from '@testing/mocks/subject.mock';
import { OSFTestingModule } from '@testing/osf.testing.module';

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

const mockSubjects: SubjectModel[] = SUBJECTS_MOCK;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [BrowseBySubjectsComponent, MockPipe(TranslatePipe)],
providers: [provideRouter([]), TranslateServiceMock],
imports: [BrowseBySubjectsComponent, OSFTestingModule],
}).compileComponents();

fixture = TestBed.createComponent(BrowseBySubjectsComponent);
component = fixture.componentInstance;
});

it('should create', () => {
fixture.componentRef.setInput('subjects', []);
fixture.componentRef.setInput('areSubjectsLoading', false);
fixture.componentRef.setInput('isProviderLoading', false);

fixture.detectChanges();
expect(component).toBeTruthy();
});

it('should create', () => {
expect(component).toBeTruthy();
it('should have default input values', () => {
fixture.componentRef.setInput('subjects', []);
fixture.componentRef.setInput('areSubjectsLoading', false);
fixture.componentRef.setInput('isProviderLoading', false);
fixture.detectChanges();

expect(component.subjects()).toEqual([]);
expect(component.areSubjectsLoading()).toBe(false);
expect(component.isProviderLoading()).toBe(false);
expect(component.isLandingPage()).toBe(false);
});

it('should display title', () => {
fixture.componentRef.setInput('subjects', []);
fixture.componentRef.setInput('areSubjectsLoading', false);
fixture.componentRef.setInput('isProviderLoading', false);
fixture.detectChanges();

const compiled = fixture.nativeElement;
const title = compiled.querySelector('h2');

expect(title).toBeTruthy();
expect(title.textContent).toBe('preprints.browseBySubjects.title');
});

it('should display correct subject names in buttons', () => {
fixture.componentRef.setInput('subjects', mockSubjects);
fixture.componentRef.setInput('areSubjectsLoading', false);
fixture.componentRef.setInput('isProviderLoading', false);
fixture.detectChanges();

const compiled = fixture.nativeElement;
const buttons = compiled.querySelectorAll('p-button');

expect(buttons[0].getAttribute('ng-reflect-label')).toBe('Mathematics');
expect(buttons[1].getAttribute('ng-reflect-label')).toBe('Physics');
});

it('should compute linksToSearchPageForSubject correctly', () => {
fixture.componentRef.setInput('subjects', mockSubjects);
fixture.componentRef.setInput('areSubjectsLoading', false);
fixture.componentRef.setInput('isProviderLoading', false);
fixture.detectChanges();

const links = component.linksToSearchPageForSubject();

expect(links).toHaveLength(2);
expect(links[0]).toEqual({
tab: ResourceType.Preprint,
filter_subject: 'https://example.com/subjects/mathematics',
});
expect(links[1]).toEqual({
tab: ResourceType.Preprint,
filter_subject: 'https://example.com/subjects/physics',
});
});

it('should set correct routerLink for non-landing page', () => {
fixture.componentRef.setInput('subjects', mockSubjects);
fixture.componentRef.setInput('areSubjectsLoading', false);
fixture.componentRef.setInput('isProviderLoading', false);
fixture.componentRef.setInput('isLandingPage', false);
fixture.detectChanges();

const compiled = fixture.nativeElement;
const buttons = compiled.querySelectorAll('p-button');

expect(buttons[0].getAttribute('ng-reflect-router-link')).toBe('discover');
});

it('should set correct routerLink for landing page', () => {
fixture.componentRef.setInput('subjects', mockSubjects);
fixture.componentRef.setInput('areSubjectsLoading', false);
fixture.componentRef.setInput('isProviderLoading', false);
fixture.componentRef.setInput('isLandingPage', true);
fixture.detectChanges();

const compiled = fixture.nativeElement;
const buttons = compiled.querySelectorAll('p-button');

expect(buttons[0].getAttribute('ng-reflect-router-link')).toBe('/search');
});

it('should handle subjects without iri', () => {
const subjectsWithoutIri: SubjectModel[] = [
{
id: 'subject-1',
name: 'Physics',
iri: undefined,
children: [],
parent: null,
expanded: false,
},
];

fixture.componentRef.setInput('subjects', subjectsWithoutIri);
fixture.componentRef.setInput('areSubjectsLoading', false);
fixture.componentRef.setInput('isProviderLoading', false);
fixture.detectChanges();

const links = component.linksToSearchPageForSubject();

expect(links).toHaveLength(1);
expect(links[0]).toEqual({
tab: ResourceType.Preprint,
filter_subject: undefined,
});
});
});
Loading