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
7 changes: 7 additions & 0 deletions src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ export const routes: Routes = [
'@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: () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { MockProvider } from 'ng-mocks';

import { PLATFORM_ID } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';

import { SocialShareService } from '@osf/shared/services/social-share.service';

import { PreprintDownloadRedirectComponent } from './preprint-download-redirect.component';

import { provideOSFCore } from '@testing/osf.testing.provider';
import { ActivatedRouteMockBuilder } from '@testing/providers/route-provider.mock';

const MOCK_ID = 'test-preprint-id';
const MOCK_DOWNLOAD_URL = 'https://osf.io/download/test-preprint-id';

describe('PreprintDownloadRedirectComponent', () => {
let locationReplaceMock: jest.Mock;

beforeEach(() => {
locationReplaceMock = jest.fn();
Object.defineProperty(window, 'location', {
value: { replace: locationReplaceMock },
writable: true,
configurable: true,
});
});

function setup(overrides: { id?: string | null; isBrowser?: boolean } = {}) {
const { id = MOCK_ID, isBrowser = true } = overrides;

const mockRoute = ActivatedRouteMockBuilder.create()
.withParams(id ? { id } : {})
.build();

const mockSocialShareService = {
createDownloadUrl: jest.fn().mockReturnValue(MOCK_DOWNLOAD_URL),
};

TestBed.configureTestingModule({
imports: [PreprintDownloadRedirectComponent],
providers: [
provideOSFCore(),
MockProvider(ActivatedRoute, mockRoute),
MockProvider(SocialShareService, mockSocialShareService),
MockProvider(PLATFORM_ID, isBrowser ? 'browser' : 'server'),
],
});

const fixture = TestBed.createComponent(PreprintDownloadRedirectComponent);
return { fixture, component: fixture.componentInstance, mockSocialShareService };
}

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

it('should render download message', () => {
const { fixture } = setup();
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('p').textContent).toContain('preprints.downloadRedirect.message');
});

it('should redirect to download URL when id is present in browser', () => {
const { mockSocialShareService } = setup({ id: MOCK_ID });
expect(mockSocialShareService.createDownloadUrl).toHaveBeenCalledWith(MOCK_ID);
expect(locationReplaceMock).toHaveBeenCalledWith(MOCK_DOWNLOAD_URL);
});

it('should not redirect when id is missing', () => {
const { mockSocialShareService } = setup({ id: null });
expect(mockSocialShareService.createDownloadUrl).not.toHaveBeenCalled();
expect(locationReplaceMock).not.toHaveBeenCalled();
});

it('should not redirect when not in browser', () => {
const { mockSocialShareService } = setup({ isBrowser: false });
expect(mockSocialShareService.createDownloadUrl).not.toHaveBeenCalled();
expect(locationReplaceMock).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { TranslatePipe } from '@ngx-translate/core';

import { isPlatformBrowser } from '@angular/common';
import { ChangeDetectionStrategy, Component, inject, PLATFORM_ID } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { SocialShareService } from '@osf/shared/services/social-share.service';

@Component({
selector: 'osf-preprint-download-redirect',
template: `<p>{{ 'preprints.downloadRedirect.message' | translate }}</p>`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [TranslatePipe],
})
export class PreprintDownloadRedirectComponent {
private readonly route = inject(ActivatedRoute);
private readonly socialShareService = inject(SocialShareService);
private readonly isBrowser = isPlatformBrowser(inject(PLATFORM_ID));

constructor() {
const id = this.route.snapshot.paramMap.get('id') ?? '';

if (!id || !this.isBrowser) {
return;
}

const url = this.socialShareService.createDownloadUrl(id);
window.location.replace(url);
}
}
3 changes: 3 additions & 0 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,9 @@
"singularCapitalized": "Thesis"
}
},
"downloadRedirect": {
"message": "Your download will begin shortly."
},
"details": {
"reasonForWithdrawal": "Reason for withdrawal",
"originalPublicationDate": "Original Publication Date",
Expand Down