Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/app/app.routes.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ export const serverRoutes: ServerRoute[] = [
path: 'preprints/:providerId',
renderMode: RenderMode.Server,
},
{
path: 'preprints',
renderMode: RenderMode.Server,
},
{
path: 'registries/my-registrations',
renderMode: RenderMode.Client,
Expand All @@ -145,6 +149,10 @@ export const serverRoutes: ServerRoute[] = [
path: 'registries/:providerId',
renderMode: RenderMode.Server,
},
{
path: 'registries',
renderMode: RenderMode.Server,
},
{
path: 'institutions',
renderMode: RenderMode.Server,
Expand Down
21 changes: 0 additions & 21 deletions src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,6 @@ export const routes: Routes = [
path: 'preprints',
loadChildren: () => import('./features/preprints/preprints.routes').then((mod) => mod.preprintsRoutes),
},
{
path: 'preprints/:providerId/:id/pending-moderation',
loadComponent: () =>
import('@osf/features/preprints/pages/preprint-pending-moderation/preprint-pending-moderation.component').then(
(mod) => mod.PreprintPendingModerationComponent
),
},
{
path: 'preprints/:providerId/:id/download',
loadComponent: () =>
import('@osf/features/preprints/pages/preprint-download-redirect/preprint-download-redirect.component').then(
(c) => c.PreprintDownloadRedirectComponent
),
},
{
path: 'preprints/:providerId/:id',
loadComponent: () =>
import('@osf/features/preprints/pages/preprint-details/preprint-details.component').then(
(c) => c.PreprintDetailsComponent
),
},
{
path: 'registries',
loadChildren: () => import('./features/registries/registries.routes').then((mod) => mod.registriesRoutes),
Expand Down
272 changes: 143 additions & 129 deletions src/app/core/components/nav-menu/nav-menu.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,190 +1,204 @@
import { MockComponent, MockProvider } from 'ng-mocks';
import { MockProvider } from 'ng-mocks';

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

import { CustomMenuItem } from '@core/models/custom-menu-item.model';
import { AuthService } from '@core/services/auth.service';
import { ProviderSelectors } from '@osf/core/store/provider/provider.selectors';
import { UserSelectors } from '@osf/core/store/user/user.selectors';
import { IconComponent } from '@osf/shared/components/icon/icon.component';
import { ProviderSelectors } from '@core/store/provider';
import { UserSelectors } from '@core/store/user';
import { ViewOnlyLinkHelperService } from '@osf/shared/services/view-only-link-helper.service';
import { CurrentResourceSelectors } from '@osf/shared/stores/current-resource';

import { MOCK_USER } from '@testing/mocks/data.mock';
import { provideOSFCore } from '@testing/osf.testing.provider';
import { AuthServiceMock, AuthServiceMockType } from '@testing/providers/auth-service.mock';
import { ActivatedRouteMockBuilder } from '@testing/providers/route-provider.mock';
import { RouterMockBuilder } from '@testing/providers/router-provider.mock';
import { provideMockStore } from '@testing/providers/store-provider.mock';
import { RouterMockBuilder, RouterMockType } from '@testing/providers/router-provider.mock';
import { BaseSetupOverrides, mergeSignalOverrides, provideMockStore } from '@testing/providers/store-provider.mock';
import { ViewOnlyLinkHelperMock } from '@testing/providers/view-only-link-helper.mock';

import { NavMenuComponent } from './nav-menu.component';

interface SetupOverrides extends BaseSetupOverrides {
routerUrl?: string;
nestedRouteParams?: Record<string, string>;
queryParams?: Record<string, string>;
}

describe('NavMenuComponent', () => {
let component: NavMenuComponent;
let fixture: ComponentFixture<NavMenuComponent>;
let mockAuthService: AuthServiceMockType;

beforeEach(() => {
Object.defineProperty(window, 'open', {
writable: true,
value: vi.fn(),
});
mockAuthService = AuthServiceMock.simple();
let authServiceMock: AuthServiceMockType;
let routerMock: RouterMockType;

function findMenuItem(items: CustomMenuItem[] | undefined, id: string): CustomMenuItem | undefined {
if (!items) {
return undefined;
}

for (const item of items) {
if (item.id === id) {
return item;
}

const nested = findMenuItem(item.items, id);
if (nested) {
return nested;
}
}

return undefined;
}

function setup(overrides: SetupOverrides = {}) {
const routeBuilder = ActivatedRouteMockBuilder.create().withQueryParams(overrides.queryParams ?? {});

if (overrides.nestedRouteParams) {
routeBuilder.withFirstChild((featureRoute) => {
featureRoute.withFirstChild((detailsRoute) => {
detailsRoute.withParams(overrides.nestedRouteParams ?? {});
});
});
} else if (overrides.routeParams) {
routeBuilder.withParams(overrides.routeParams);
}

if (overrides.hasParent === false) {
routeBuilder.withNoParent();
}

const routeMock = routeBuilder.build();
routerMock = RouterMockBuilder.create()
.withUrl(overrides.routerUrl ?? '/')
.build();
authServiceMock = AuthServiceMock.simple();

const defaultSignals = [
{ selector: UserSelectors.isAuthenticated, value: true },
{ selector: UserSelectors.getCanViewReviews, value: false },
{ selector: CurrentResourceSelectors.getCurrentResource, value: null },
{ selector: ProviderSelectors.getCurrentProvider, value: null },
];
const signals = mergeSignalOverrides(defaultSignals, overrides.selectorOverrides);

TestBed.configureTestingModule({
imports: [NavMenuComponent, MockComponent(IconComponent)],
imports: [NavMenuComponent],
providers: [
provideOSFCore(),
provideMockStore({
signals: [
{ selector: UserSelectors.isAuthenticated, value: signal(false) },
{ selector: UserSelectors.getCurrentUser, value: signal(MOCK_USER) },
{ selector: UserSelectors.getCanViewReviews, value: signal(false) },
{ selector: ProviderSelectors.getCurrentProvider, value: signal(null) },
{ selector: CurrentResourceSelectors.getCurrentResource, value: signal(null) },
],
}),
{
provide: Router,
useValue: {
...RouterMockBuilder.create().withUrl('/test').build(),
serializeUrl: vi.fn(() => '/test'),
parseUrl: vi.fn(() => ({})),
isActive: vi.fn(() => false),
},
},
{
provide: ActivatedRoute,
useValue: ActivatedRouteMockBuilder.create().build(),
},
MockProvider(AuthService, mockAuthService),
provideRouter([]),
MockProvider(ActivatedRoute, routeMock),
MockProvider(Router, routerMock),
MockProvider(AuthService, authServiceMock),
MockProvider(ViewOnlyLinkHelperService, ViewOnlyLinkHelperMock.simple()),
provideMockStore({ signals }),
],
});

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

return { fixture, component };
}

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

it('should open external links in new tab for support and donate items', () => {
const openSpy = vi.spyOn(window, 'open');
const supportItem: CustomMenuItem = { id: 'support', url: 'https://support.example.com' };
const donateItem: CustomMenuItem = { id: 'donate', url: 'https://donate.example.com' };

component.goToLink(supportItem);
expect(openSpy).toHaveBeenCalledWith('https://support.example.com', '_blank');
it('should detect preprint details from nested route params', () => {
setup({
routerUrl: '/preprints/socarxiv/2cpmr_v3',
nestedRouteParams: { providerId: 'socarxiv', id: '2cpmr_v3' },
});

component.goToLink(donateItem);
expect(openSpy).toHaveBeenCalledWith('https://donate.example.com', '_blank');
expect(component.isPreprintRoute()).toBe(true);
expect(component.currentResourceId()).toBe('2cpmr_v3');
});

it('should navigate to sign in for sign-in item', () => {
const signInItem: CustomMenuItem = { id: 'sign-in' };
it('should not treat preprint provider overview as preprint details', () => {
setup({
routerUrl: '/preprints/socarxiv',
nestedRouteParams: { providerId: 'socarxiv' },
});

component.goToLink(signInItem);
expect(mockAuthService.navigateToSignIn).toHaveBeenCalled();
expect(component.isPreprintRoute()).toBe(false);
expect(component.currentResourceId()).toBeUndefined();
});

it('should logout for log-out item', () => {
const logOutItem: CustomMenuItem = { id: 'log-out' };
it('should show preprint details menu using nested providerId when provider store is empty', () => {
setup({
routerUrl: '/preprints/socarxiv/2cpmr_v3',
nestedRouteParams: { providerId: 'socarxiv', id: '2cpmr_v3' },
});

const detailsItem = findMenuItem(component.mainMenuItems(), 'preprints-details');

component.goToLink(logOutItem);
expect(mockAuthService.logout).toHaveBeenCalled();
expect(detailsItem?.visible).toBe(true);
expect(detailsItem?.items?.[0]?.routerLink).toEqual(['preprints', 'socarxiv', '2cpmr_v3']);
});

it('should emit closeMenu for items without children', () => {
const emitSpy = vi.spyOn(component.closeMenu, 'emit');
const menuItem: CustomMenuItem = { id: 'test-item' };
it('should use resourceId from query params when route id is missing', () => {
setup({
routerUrl: '/collections/provider-1',
queryParams: { resourceId: 'query-resource-1' },
});

component.goToLink(menuItem);
expect(emitSpy).toHaveBeenCalled();
expect(component.currentResourceId()).toBe('query-resource-1');
});

it('should not emit closeMenu for items with children', () => {
const emitSpy = vi.spyOn(component.closeMenu, 'emit');
const menuItemWithChildren: CustomMenuItem = {
id: 'test-item',
items: [{ id: 'child-item' }],
};

component.goToLink(menuItemWithChildren);
expect(emitSpy).not.toHaveBeenCalled();
});
it('should emit closeMenu for leaf items', () => {
setup();
const closeMenu = vi.fn();
component.closeMenu.subscribe(closeMenu);

it('should return false for items without items array', () => {
const menuItem: CustomMenuItem = { id: 'test-item' };
expect(component.hasVisibleChildren(menuItem)).toBe(false);
});
component.goToLink({ id: 'home', label: 'Home' });

it('should return false for items with empty items array', () => {
const menuItem: CustomMenuItem = { id: 'test-item', items: [] };
expect(component.hasVisibleChildren(menuItem)).toBe(false);
expect(closeMenu).toHaveBeenCalledTimes(1);
});

it('should return false for items with all invisible children', () => {
const menuItem: CustomMenuItem = {
id: 'test-item',
items: [
{ id: 'child1', visible: false },
{ id: 'child2', visible: false },
],
};
expect(component.hasVisibleChildren(menuItem)).toBe(false);
});
it('should not emit closeMenu for items with children', () => {
setup();
const closeMenu = vi.fn();
component.closeMenu.subscribe(closeMenu);

component.goToLink({
id: 'preprints',
label: 'Preprints',
items: [{ id: 'child', label: 'Child', visible: true }],
});

it('should return true for items with at least one visible child', () => {
const menuItem: CustomMenuItem = {
id: 'test-item',
items: [
{ id: 'child1', visible: false },
{ id: 'child2', visible: true },
],
};
expect(component.hasVisibleChildren(menuItem)).toBe(true);
expect(closeMenu).not.toHaveBeenCalled();
});

it('should return false for items with children that have visible property undefined', () => {
const menuItem: CustomMenuItem = {
id: 'test-item',
items: [{ id: 'child1' }],
};
expect(component.hasVisibleChildren(menuItem)).toBe(false);
});
it('should navigate to sign in', () => {
setup();

it('should have mainMenuItems computed property', () => {
expect(component.mainMenuItems).toBeDefined();
expect(typeof component.mainMenuItems).toBe('function');
});
component.goToLink({ id: 'sign-in', label: 'Sign in' });

it('should have currentRoute computed property', () => {
expect(component.currentRoute).toBeDefined();
expect(authServiceMock.navigateToSignIn).toHaveBeenCalledTimes(1);
});

it('should have currentResourceId computed property', () => {
expect(component.currentResourceId).toBeDefined();
expect(typeof component.currentResourceId).toBe('function');
});
it('should log out', () => {
setup();

it('should have isCollectionsRoute computed property', () => {
expect(component.isCollectionsRoute).toBeDefined();
expect(typeof component.isCollectionsRoute).toBe('function');
});
component.goToLink({ id: 'log-out', label: 'Log out' });

it('should have isPreprintRoute computed property', () => {
expect(component.isPreprintRoute).toBeDefined();
expect(typeof component.isPreprintRoute).toBe('function');
expect(authServiceMock.logout).toHaveBeenCalledTimes(1);
});

it('should emit closeMenu event', () => {
const emitSpy = vi.spyOn(component.closeMenu, 'emit');

component.closeMenu.emit();
it('should report visible children', () => {
setup();

expect(emitSpy).toHaveBeenCalled();
expect(
component.hasVisibleChildren({
id: 'parent',
items: [
{ id: 'hidden', visible: false },
{ id: 'shown', visible: true },
],
})
).toBe(true);
expect(component.hasVisibleChildren({ id: 'leaf' })).toBe(false);
});
});
Loading
Loading