Skip to content
6 changes: 5 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ module.exports = {
'<rootDir>/src/app/features/files/components',
'<rootDir>/src/app/features/files/pages/file-detail',
'<rootDir>/src/app/features/preprints/',
'<rootDir>/src/app/features/project/',
'<rootDir>/src/app/features/project/addons/',
'<rootDir>/src/app/features/project/overview/',
'<rootDir>/src/app/features/project/registrations',
'<rootDir>/src/app/features/project/settings',
'<rootDir>/src/app/features/project/wiki',
'<rootDir>/src/app/features/registries/',
'<rootDir>/src/app/features/registry/',
'<rootDir>/src/app/features/settings/addons/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('FundingDialogComponent', () => {
providers: [
TranslateServiceMock,
MockProviders(DynamicDialogRef, DestroyRef),
MockProvider(DynamicDialogConfig, { data: null }),
MockProvider(DynamicDialogConfig, { data: { funders: [] } }),
MockProvider(Store, MOCK_STORE),
],
}).compileComponents();
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('FundingDialogComponent', () => {
const awardTitleControl = entry.get('awardTitle');

expect(funderNameControl?.hasError('required')).toBe(true);
expect(awardTitleControl?.hasError('required')).toBe(true);
expect(awardTitleControl?.hasError('required')).toBe(false);

funderNameControl?.setValue('Test Funder');
awardTitleControl?.setValue('Test Award');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,60 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { MOCK_VIEW_ONLY_LINK_COMPONENT_ITEM } from '@shared/mocks';

import { ViewOnlyLinkComponentItem } from '../../models/view-only-components.models';

import { ComponentCheckboxItemComponent } from './component-checkbox-item.component';

describe.skip('ComponentCheckboxItemComponent', () => {
describe('ComponentCheckboxItemComponent', () => {
let component: ComponentCheckboxItemComponent;
let fixture: ComponentFixture<ComponentCheckboxItemComponent>;

const mockItem: ViewOnlyLinkComponentItem = MOCK_VIEW_ONLY_LINK_COMPONENT_ITEM;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ComponentCheckboxItemComponent],
}).compileComponents();

fixture = TestBed.createComponent(ComponentCheckboxItemComponent);
component = fixture.componentInstance;

fixture.componentRef.setInput('item', mockItem);
fixture.detectChanges();
});

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

it('should emit checkboxChange when checkbox is clicked', () => {
jest.spyOn(component.checkboxChange, 'emit');

const checkboxElement = fixture.debugElement.query(By.css('p-checkbox'));
checkboxElement.triggerEventHandler('onChange', {});

expect(component.checkboxChange.emit).toHaveBeenCalled();
});

it('should handle item with parentId', () => {
const itemWithParent = { ...mockItem, parentId: 'parent-123' };
fixture.componentRef.setInput('item', itemWithParent);
fixture.detectChanges();

expect(component.item().parentId).toBe('parent-123');
});

it('should handle item without parentId', () => {
expect(component.item().parentId).toBeNull();
});

it('should handle different item IDs', () => {
const differentItem = { ...mockItem, id: 'different-id' };
fixture.componentRef.setInput('item', differentItem);
fixture.detectChanges();

expect(component.item().id).toBe('different-id');
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
import { MockProvider } from 'ng-mocks';

import { DynamicDialogConfig, DynamicDialogRef } from 'primeng/dynamicdialog';

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

import { CurrentResourceSelectors } from '@osf/shared/stores';
import { MOCK_RESOURCE_INFO, MOCK_RESOURCE_WITH_CHILDREN } from '@shared/mocks';

import { CreateViewLinkDialogComponent } from './create-view-link-dialog.component';

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

describe('CreateViewLinkDialogComponent', () => {
let component: CreateViewLinkDialogComponent;
let fixture: ComponentFixture<CreateViewLinkDialogComponent>;
let dialogRef: jest.Mocked<DynamicDialogRef>;
let dialogConfig: DynamicDialogConfig;

beforeEach(async () => {
dialogRef = {
close: jest.fn(),
} as any;

dialogConfig = {
data: MOCK_RESOURCE_INFO,
} as DynamicDialogConfig;

await TestBed.configureTestingModule({
imports: [CreateViewLinkDialogComponent],
imports: [CreateViewLinkDialogComponent, OSFTestingModule],
providers: [
provideMockStore({
signals: [
{
selector: CurrentResourceSelectors.getResourceWithChildren,
value: MOCK_RESOURCE_WITH_CHILDREN,
},
{
selector: CurrentResourceSelectors.isResourceWithChildrenLoading,
value: false,
},
],
}),
MockProvider(DynamicDialogRef, dialogRef),
MockProvider(DynamicDialogConfig, dialogConfig),
],
}).compileComponents();

fixture = TestBed.createComponent(CreateViewLinkDialogComponent);
Expand All @@ -19,4 +55,146 @@ describe('CreateViewLinkDialogComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('should have invalid form when linkName is empty', () => {
expect(component.linkName.invalid).toBe(true);
});

it('should have invalid form when linkName contains only whitespace', () => {
component.linkName.setValue(' ');
expect(component.linkName.invalid).toBe(true);
});

it('should have valid form when linkName has content', () => {
component.linkName.setValue('Test Link');
expect(component.linkName.valid).toBe(true);
});

it('should mark current resource as checked and disabled', () => {
const currentResourceItem = component.componentsList().find((item) => item.id === 'project-123');
expect(currentResourceItem?.checked).toBe(true);
expect(currentResourceItem?.disabled).toBe(true);
expect(currentResourceItem?.isCurrentResource).toBe(true);
});

it('should uncheck children when parent is unchecked', () => {
const parentItem = component.componentsList().find((item) => item.id === 'component-1');

component.onCheckboxChange({ ...parentItem!, checked: true });
fixture.detectChanges();

component.onCheckboxChange({ ...parentItem!, checked: false });
fixture.detectChanges();

const updatedChildItem = component.componentsList().find((item) => item.id === 'component-3');
expect(updatedChildItem?.checked).toBe(false);
});

it('should handle items without parent correctly', () => {
const rootItem = component.componentsList().find((item) => item.id === 'project-123');
expect(rootItem?.disabled).toBe(true);
});

it('should add link and close dialog when form is valid', () => {
component.linkName.setValue('Test Link');
component.anonymous.set(false);

component.addLink();

expect(dialogRef.close).toHaveBeenCalledWith({
attributes: {
name: 'Test Link',
anonymous: false,
},
nodes: [{ id: 'project-123', type: 'nodes' }],
});
});

it('should add link with relationships when additional components are selected', () => {
const result = component['buildLinkData'](
['project-123', 'component-1', 'component-2'],
'project-123',
'Test Link',
false
);

expect(result).toEqual({
attributes: {
name: 'Test Link',
anonymous: false,
},
nodes: [{ id: 'project-123', type: 'nodes' }],
relationships: {
nodes: {
data: [
{ id: 'component-1', type: 'nodes' },
{ id: 'component-2', type: 'nodes' },
],
},
},
});
});

it('should build correct link data with only root project', () => {
component.linkName.setValue('Test Link');
component.anonymous.set(true);

const result = component['buildLinkData'](['project-123'], 'project-123', 'Test Link', true);

expect(result).toEqual({
attributes: {
name: 'Test Link',
anonymous: true,
},
nodes: [{ id: 'project-123', type: 'nodes' }],
});
});

it('should build correct link data with root project and components', () => {
component.linkName.setValue('Test Link');
component.anonymous.set(false);

const result = component['buildLinkData'](
['project-123', 'component-1', 'component-2'],
'project-123',
'Test Link',
false
);

expect(result).toEqual({
attributes: {
name: 'Test Link',
anonymous: false,
},
nodes: [{ id: 'project-123', type: 'nodes' }],
relationships: {
nodes: {
data: [
{ id: 'component-1', type: 'nodes' },
{ id: 'component-2', type: 'nodes' },
],
},
},
});
});

it('should build correct link data without root project', () => {
const result = component['buildLinkData'](['component-1', 'component-2'], 'project-123', 'Test Link', true);

expect(result).toEqual({
attributes: {
name: 'Test Link',
anonymous: true,
},
nodes: [],
relationships: {
nodes: {
data: [
{ id: 'component-1', type: 'nodes' },
{ id: 'component-2', type: 'nodes' },
],
},
},
});
});
});
Loading