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
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { MockProvider } from 'ng-mocks';

import { firstValueFrom, of } from 'rxjs';

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

import { CollectionsMapper } from '@osf/shared/mappers/collections';
import { ReviewActionsMapper } from '@osf/shared/mappers/review-actions.mapper';
import { CollectionSubmissionPayload } from '@osf/shared/models/collections/collection-submission-payload.model';
import { CollectionSubmissionPayloadJsonApi } from '@osf/shared/models/collections/collection-submission-payload-json-api.model';
import { ReviewActionPayloadJsonApi } from '@osf/shared/models/review-action/review-action-payload-json-api.model';
import { JsonApiService } from '@osf/shared/services/json-api.service';

import { MOCK_API_URL, provideMockEnvironment } from '@testing/providers/environment.provider.mock';
import { JsonApiServiceMock, JsonApiServiceMockType } from '@testing/providers/json-api.service.mock';

import { RemoveCollectionSubmissionPayload } from '../models/remove-collection-submission-payload.model';

import { AddToCollectionService } from './add-to-collection.service';

describe('AddToCollectionService', () => {
let service: AddToCollectionService;
let jsonApiService: JsonApiServiceMockType;

const submissionPayload: CollectionSubmissionPayload = {
collectionId: 'collection-1',
projectId: 'project-1',
userId: 'user-1',
};

const removePayload: RemoveCollectionSubmissionPayload = {
projectId: 'project-1',
collectionId: 'collection-1',
comment: 'Removing from collection',
};

function setup() {
jsonApiService = JsonApiServiceMock.simple();

TestBed.configureTestingModule({
providers: [AddToCollectionService, MockProvider(JsonApiService, jsonApiService), provideMockEnvironment()],
});

service = TestBed.inject(AddToCollectionService);
}

it('should expose apiUrl from environment', () => {
setup();

expect(service.apiUrl).toBe(MOCK_API_URL);
});

it('createCollectionSubmission should map payload and post to collection submissions', async () => {
setup();
const mappedPayload = {
data: {
type: 'collection-submissions',
attributes: { guid: 'project-1' },
relationships: {
collection: { data: { id: 'collection-1', type: 'collections' } },
creator: { data: { type: 'users', id: 'user-1' } },
},
},
} as CollectionSubmissionPayloadJsonApi;
const mapperSpy = vi.spyOn(CollectionsMapper, 'toCollectionSubmissionRequest').mockReturnValue(mappedPayload);
jsonApiService.post.mockReturnValue(of(undefined));

const response = await firstValueFrom(service.createCollectionSubmission(submissionPayload));

expect(mapperSpy).toHaveBeenCalledWith(submissionPayload);
expect(jsonApiService.post).toHaveBeenCalledWith(
`${MOCK_API_URL}/collections/${submissionPayload.collectionId}/collection_submissions/`,
mappedPayload
);
expect(response).toBeUndefined();
});

it('removeCollectionSubmission should map review action and post to collection submission actions', async () => {
setup();
const mappedParams = {
data: {
type: 'collection_submission_actions',
attributes: {
trigger: 'remove',
comment: removePayload.comment,
},
relationships: {
target: {
data: {
type: 'collection-submissions',
id: `${removePayload.projectId}-${removePayload.collectionId}`,
},
},
},
},
} as ReviewActionPayloadJsonApi<string, string>;
const mapperSpy = vi.spyOn(ReviewActionsMapper, 'toReviewActionPayloadJsonApi').mockReturnValue(mappedParams);
jsonApiService.post.mockReturnValue(of(undefined));

const response = await firstValueFrom(service.removeCollectionSubmission(removePayload));

expect(mapperSpy).toHaveBeenCalledWith(
{
action: 'remove',
targetId: `${removePayload.projectId}-${removePayload.collectionId}`,
comment: removePayload.comment,
},
'collection_submission_actions',
'collection-submissions'
);
expect(jsonApiService.post).toHaveBeenCalledWith(`${MOCK_API_URL}/collection_submission_actions/`, mappedParams);
expect(response).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { map, Observable } from 'rxjs';
import { Observable } from 'rxjs';

import { inject, Injectable } from '@angular/core';

import { ENVIRONMENT } from '@core/provider/environment.provider';
import { CollectionsMapper } from '@osf/shared/mappers/collections';
import { LicensesMapper } from '@osf/shared/mappers/licenses.mapper';
import { ReviewActionsMapper } from '@osf/shared/mappers/review-actions.mapper';
import { ReviewActionPayload } from '@osf/shared/models/review-action/review-action-payload.model';
import { JsonApiService } from '@osf/shared/services/json-api.service';
import { CollectionSubmissionPayload } from '@shared/models/collections/collection-submission-payload.model';
import { LicenseModel } from '@shared/models/license/license.model';
import { LicensesResponseJsonApi } from '@shared/models/license/licenses-json-api.model';

import { RemoveCollectionSubmissionPayload } from '../models/remove-collection-submission-payload.model';

Expand All @@ -25,15 +22,6 @@ export class AddToCollectionService {
return `${this.environment.apiDomainUrl}/v2`;
}

fetchCollectionLicenses(providerId: string): Observable<LicenseModel[]> {
return this.jsonApiService
.get<LicensesResponseJsonApi>(`${this.apiUrl}/providers/collections/${providerId}/licenses/`, {
'page[size]': 100,
sort: 'name',
})
.pipe(map((licenses) => LicensesMapper.fromLicensesResponse(licenses)));
}

createCollectionSubmission(payload: CollectionSubmissionPayload): Observable<void> {
const collectionId = payload.collectionId;
const metadata = CollectionsMapper.toCollectionSubmissionRequest(payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { catchError } from 'rxjs/operators';
import { inject, Injectable } from '@angular/core';

import { AddToCollectionService } from '@osf/features/collections/services/add-to-collection.service';
import { ResourceType } from '@osf/shared/enums/resource-type.enum';
import { handleSectionError } from '@osf/shared/helpers/state-error.handler';
import { CollectionsService } from '@osf/shared/services/collections.service';
import { LicensesService } from '@osf/shared/services/licenses.service';

import {
ClearAddToCollectionState,
Expand All @@ -26,6 +28,7 @@ import { ADD_TO_COLLECTION_DEFAULTS, AddToCollectionStateModel } from './add-to-
export class AddToCollectionState {
addToCollectionService = inject(AddToCollectionService);
collectionsService = inject(CollectionsService);
licensesService = inject(LicensesService);

@Action(GetCollectionLicenses)
getCollectionLicenses(ctx: StateContext<AddToCollectionStateModel>, action: GetCollectionLicenses) {
Expand All @@ -37,7 +40,7 @@ export class AddToCollectionState {
},
});

return this.addToCollectionService.fetchCollectionLicenses(action.providerId).pipe(
return this.licensesService.getLicenses(ResourceType.Collection, action.providerId).pipe(
tap((licenses) => {
ctx.patchState({
collectionLicenses: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
<div class="flex flex-column gap-4">
@if (licensesLoading()) {
<osf-loading-spinner></osf-loading-spinner>
<osf-loading-spinner />
} @else {
<div class="license-content">
<h3 class="mb-3">{{ 'project.metadata.license.dialog.chooseLicense.label' | translate }}</h3>
<h3>{{ 'project.metadata.license.dialog.chooseLicense.label' | translate }}</h3>

<osf-license
#licenseComponent
[licenses]="licenses()"
[selectedLicenseId]="selectedLicenseId()"
[selectedLicenseOptions]="selectedLicenseOptions()"
[isSubmitting]="isSubmitting()"
[showInternalButtons]="false"
[fullWidthSelect]="true"
[appendTo]="'body'"
(selectLicense)="onSelectLicense($event)"
(createLicense)="onCreateLicense($event)"
/>
</div>
<osf-license
appendTo="body"
[licenses]="licenses()"
[selectedLicenseId]="selectedLicenseId()"
[selectedLicenseOptions]="initialLicenseOptions"
[showInternalButtons]="false"
(selectLicense)="selectedLicenseId.set($event.id)"
(licenseStateChange)="licenseState.set($event)"
/>
}

<div class="flex justify-content-end gap-2">
<p-button severity="info" [label]="'common.buttons.cancel' | translate" (onClick)="cancel()" />
<p-button
[label]="'common.buttons.save' | translate"
(onClick)="save()"
[disabled]="isInvalid"
[loading]="isSubmitting()"
/>
<p-button [label]="'common.buttons.save' | translate" [disabled]="isSaveDisabled()" (onClick)="save()" />
</div>
</div>
Loading
Loading