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
196 changes: 196 additions & 0 deletions src/app/features/moderation/mappers/registry-moderation.mapper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import { RegistrationReviewStates } from '@osf/shared/enums/registration-review-states.enum';
import { RevisionReviewStates } from '@osf/shared/enums/revision-review-states.enum';

import { RegistryDataJsonApi, RegistryResponseJsonApi } from '../models';

import { RegistryModerationMapper } from './registry-moderation.mapper';

describe('RegistryModerationMapper', () => {
const createMockResponse = (overrides: Partial<RegistryDataJsonApi> = {}): RegistryDataJsonApi => ({
id: 'registry-1',
attributes: {
title: 'Test Registry',
reviews_state: RegistrationReviewStates.Accepted,
revision_state: RevisionReviewStates.Approved,
public: true,
embargoed: false,
embargo_end_date: null,
} as any,
embeds: {
schema_responses: {
data: [],
},
},
...overrides,
});

describe('fromResponse', () => {
it('should map basic fields correctly', () => {
const response = createMockResponse();
const result = RegistryModerationMapper.fromResponse(response);

expect(result.id).toBe('registry-1');
expect(result.title).toBe('Test Registry');
expect(result.reviewsState).toBe(RegistrationReviewStates.Accepted);
expect(result.revisionStatus).toBe(RevisionReviewStates.Approved);
expect(result.public).toBe(true);
expect(result.embargoed).toBe(false);
expect(result.embargoEndDate).toBeNull();
expect(result.actions).toEqual([]);
});

it('should extract revisionId from schema response with pending_moderation state', () => {
const response = createMockResponse({
embeds: {
schema_responses: {
data: [
{ id: 'approved-response', attributes: { reviews_state: 'approved' } },
{ id: 'pending-response', attributes: { reviews_state: 'pending_moderation' } },
],
},
},
});

const result = RegistryModerationMapper.fromResponse(response);
expect(result.revisionId).toBe('pending-response');
});

it('should return null revisionId when no pending_moderation response exists', () => {
const response = createMockResponse({
embeds: {
schema_responses: {
data: [{ id: 'approved-response', attributes: { reviews_state: 'approved' } }],
},
},
});

const result = RegistryModerationMapper.fromResponse(response);
expect(result.revisionId).toBeNull();
});

it('should return null revisionId when schema_responses data is empty', () => {
const response = createMockResponse({
embeds: {
schema_responses: {
data: [],
},
},
});

const result = RegistryModerationMapper.fromResponse(response);
expect(result.revisionId).toBeNull();
});

it('should handle missing embeds gracefully', () => {
const response = createMockResponse({
embeds: undefined as any,
});

const result = RegistryModerationMapper.fromResponse(response);
expect(result.revisionId).toBeNull();
});

it('should select the first pending_moderation response when multiple exist', () => {
const response = createMockResponse({
embeds: {
schema_responses: {
data: [
{ id: 'first-pending', attributes: { reviews_state: 'pending_moderation' } },
{ id: 'second-pending', attributes: { reviews_state: 'pending_moderation' } },
],
},
},
});

const result = RegistryModerationMapper.fromResponse(response);
expect(result.revisionId).toBe('first-pending');
});
});

describe('fromResponseWithPagination', () => {
it('should map paginated response correctly', () => {
const response: RegistryResponseJsonApi = {
data: [createMockResponse(), createMockResponse({ id: 'registry-2' })],
meta: { total: 10, per_page: 2, version: '2.0' },
links: {},
};

const result = RegistryModerationMapper.fromResponseWithPagination(response);

expect(result.data).toHaveLength(2);
expect(result.totalCount).toBe(10);
expect(result.pageSize).toBe(2);
expect(result.data[0].id).toBe('registry-1');
expect(result.data[1].id).toBe('registry-2');
});
});

describe('fromActionResponse', () => {
it('should map action response correctly', () => {
const response = {
id: 'action-1',
attributes: {
auto: false,
comment: 'Test comment',
date_created: '2023-01-01T00:00:00Z',
date_modified: '2023-01-02T00:00:00Z',
from_state: 'pending',
to_state: 'accepted',
trigger: 'accept_submission',
visible: true,
},
embeds: {
creator: {
data: {
id: 'user-1',
attributes: {
full_name: 'Test User',
given_name: 'Test',
family_name: 'User',
middle_names: '',
suffix: '',
date_registered: '2023-01-01',
active: true,
timezone: 'UTC',
locale: 'en_US',
social: {},
employment: [],
education: [],
},
},
meta: { version: '2.0' },
},
},
};

const result = RegistryModerationMapper.fromActionResponse(response as any);

expect(result.id).toBe('action-1');
expect(result.fromState).toBe('pending');
expect(result.toState).toBe('accepted');
expect(result.comment).toBe('Test comment');
expect(result.trigger).toBe('accept_submission');
expect(result.creator?.name).toBe('Test User');
});

it('should handle missing creator', () => {
const response = {
id: 'action-1',
attributes: {
auto: false,
comment: '',
date_created: '2023-01-01T00:00:00Z',
date_modified: '2023-01-02T00:00:00Z',
from_state: 'pending',
to_state: 'accepted',
trigger: 'accept_submission',
visible: true,
},
embeds: {},
};

const result = RegistryModerationMapper.fromActionResponse(response as any);
expect(result.creator).toBeNull();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RevisionReviewStates } from '@osf/shared/enums/revision-review-states.enum';
import { UserMapper } from '@osf/shared/mappers/user';
import { PaginatedData } from '@osf/shared/models/paginated-data.model';
import { replaceBadEncodedChars } from '@shared/helpers/format-bad-encoding.helper';
Expand All @@ -21,7 +22,10 @@ export class RegistryModerationMapper {
embargoed: response.attributes.embargoed,
embargoEndDate: response.attributes.embargo_end_date,
actions: [],
revisionId: response.embeds?.schema_responses?.data?.[0]?.id || null,
revisionId:
response.embeds?.schema_responses?.data?.find(
(sr) => sr.attributes?.reviews_state === RevisionReviewStates.RevisionPendingModeration
)?.id || null,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ export interface RegistryDataJsonApi {
embeds: RegistryEmbedsJsonApi;
}

export interface SchemaResponseEmbedItem {
id: string;
attributes: {
reviews_state: string;
};
}

export interface RegistryEmbedsJsonApi {
schema_responses: {
data: { id: string }[];
data: SchemaResponseEmbedItem[];
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
class="w-full"
></textarea>
</div>
} @else if (isUpdateAwaitingAdminApproval) {
<p-message severity="info" class="w-full">
{{ 'moderation.makeDecision.awaitingAdminApproval' | translate }}
</p-message>
} @else {
<div class="flex flex-column gap-2">
<div class="flex gap-2">
Expand Down Expand Up @@ -123,14 +127,16 @@
[disabled]="isSubmitting()"
class="btn-full-width"
/>
<p-button
[label]="'moderation.makeDecision.submitDecision' | translate"
(click)="handleSubmission()"
severity="success"
[disabled]="!requestForm.valid || isSubmitting()"
[loading]="isSubmitting()"
class="btn-full-width"
/>
@if (!isUpdateAwaitingAdminApproval) {
<p-button
[label]="'moderation.makeDecision.submitDecision' | translate"
(click)="handleSubmission()"
severity="success"
[disabled]="!requestForm.valid || isSubmitting()"
[loading]="isSubmitting()"
class="btn-full-width"
/>
}
</div>
</form>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('RegistryMakeDecisionComponent', () => {
mockDialogConfig.data.registry = {
...mockRegistry,
reviewsState: RegistrationReviewStates.Pending,
revisionStatus: RevisionReviewStates.RevisionInProgress,
revisionState: RevisionReviewStates.RevisionInProgress,
};
fixture = TestBed.createComponent(RegistryMakeDecisionComponent);
component = fixture.componentInstance;
Expand Down Expand Up @@ -266,27 +266,6 @@ describe('RegistryMakeDecisionComponent', () => {
expect(commentControl?.hasError('maxlength')).toBe(true);
});

it('should update comment validators when action changes to reject', () => {
const commentControl = component.requestForm.get(ModerationDecisionFormControls.Comment);
component.requestForm.patchValue({
[ModerationDecisionFormControls.Action]: ReviewActionTrigger.RejectSubmission,
});

expect(commentControl?.hasError('required')).toBe(true);
});

it('should clear comment validators when action changes to accept', () => {
const commentControl = component.requestForm.get(ModerationDecisionFormControls.Comment);
component.requestForm.patchValue({
[ModerationDecisionFormControls.Action]: ReviewActionTrigger.RejectSubmission,
});
component.requestForm.patchValue({
[ModerationDecisionFormControls.Action]: ReviewActionTrigger.AcceptSubmission,
});

expect(commentControl?.hasError('required')).toBe(false);
});

it('should initialize with correct constants', () => {
expect(component.decisionCommentLimit).toBeDefined();
expect(component.INPUT_VALIDATION_MESSAGES).toBeDefined();
Expand All @@ -295,6 +274,50 @@ describe('RegistryMakeDecisionComponent', () => {
expect(component.ModerationDecisionFormControls).toBeDefined();
});

it('should compute isUpdateAwaitingAdminApproval as true when revision state is unapproved', () => {
mockDialogConfig.data.registry = { ...mockRegistry, revisionState: RevisionReviewStates.Unapproved };
fixture = TestBed.createComponent(RegistryMakeDecisionComponent);
component = fixture.componentInstance;
fixture.detectChanges();

expect(component.isUpdateAwaitingAdminApproval).toBe(true);
});

it('should compute isUpdateAwaitingAdminApproval as true when revision state is in progress', () => {
mockDialogConfig.data.registry = { ...mockRegistry, revisionState: RevisionReviewStates.RevisionInProgress };
fixture = TestBed.createComponent(RegistryMakeDecisionComponent);
component = fixture.componentInstance;
fixture.detectChanges();

expect(component.isUpdateAwaitingAdminApproval).toBe(true);
});

it('should compute isUpdateAwaitingAdminApproval as false when revision state is approved', () => {
expect(component.isUpdateAwaitingAdminApproval).toBe(false);
});

it('should compute isUpdateAwaitingAdminApproval as false when revision state is pending moderation', () => {
mockDialogConfig.data.registry = {
...mockRegistry,
revisionState: RevisionReviewStates.RevisionPendingModeration,
};
fixture = TestBed.createComponent(RegistryMakeDecisionComponent);
component = fixture.componentInstance;
fixture.detectChanges();

expect(component.isUpdateAwaitingAdminApproval).toBe(false);
});

it('should hide submit button when isUpdateAwaitingAdminApproval is true', () => {
mockDialogConfig.data.registry = { ...mockRegistry, revisionState: RevisionReviewStates.Unapproved };
fixture = TestBed.createComponent(RegistryMakeDecisionComponent);
component = fixture.componentInstance;
fixture.detectChanges();

const submitButton = fixture.nativeElement.querySelector('p-button[severity="success"]');
expect(submitButton).toBeNull();
});

it('should set embargoEndDate from registry', () => {
const testDate = '2024-12-31';
mockDialogConfig.data.registry = { ...mockRegistry, embargoEndDate: testDate };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ export class RegistryMakeDecisionComponent {
return this.registry.reviewsState === RegistrationReviewStates.PendingWithdraw;
}

get isUpdateAwaitingAdminApproval(): boolean {
return (
this.registry.revisionState === RevisionReviewStates.Unapproved ||
this.registry.revisionState === RevisionReviewStates.RevisionInProgress
);
}

get canWithdraw(): boolean {
return (
this.registry.reviewsState === RegistrationReviewStates.Accepted &&
Expand Down Expand Up @@ -137,7 +144,10 @@ export class RegistryMakeDecisionComponent {
},
!!revisionId
)
.pipe(tap(() => this.dialogRef.close(this.requestForm.value)))
.pipe(
takeUntilDestroyed(this.destroyRef),
tap(() => this.dialogRef.close(this.requestForm.value))
)
.subscribe();
}

Expand Down
Loading