diff --git a/src/app/features/registries/constants/registrations-tabs.ts b/src/app/features/registries/constants/registrations-tabs.ts index accf00f00..067163eec 100644 --- a/src/app/features/registries/constants/registrations-tabs.ts +++ b/src/app/features/registries/constants/registrations-tabs.ts @@ -1,8 +1,8 @@ -import { TabOption } from '@osf/shared/models/tab-option.model'; +import { CustomOption } from '@osf/shared/models/select-option.model'; import { RegistrationTab } from '../enums'; -export const REGISTRATIONS_TABS: TabOption[] = [ +export const REGISTRATIONS_TABS: CustomOption[] = [ { label: 'common.labels.drafts', value: RegistrationTab.Drafts, diff --git a/src/app/features/registries/enums/registration-tab.enum.ts b/src/app/features/registries/enums/registration-tab.enum.ts index 67eeac498..c7270c341 100644 --- a/src/app/features/registries/enums/registration-tab.enum.ts +++ b/src/app/features/registries/enums/registration-tab.enum.ts @@ -1,4 +1,4 @@ export enum RegistrationTab { - Drafts, - Submitted, + Drafts = 'drafts', + Submitted = 'submitted', } diff --git a/src/app/features/registries/pages/draft-registration-custom-step/draft-registration-custom-step.component.spec.ts b/src/app/features/registries/pages/draft-registration-custom-step/draft-registration-custom-step.component.spec.ts index 0534020da..3c07dc4bb 100644 --- a/src/app/features/registries/pages/draft-registration-custom-step/draft-registration-custom-step.component.spec.ts +++ b/src/app/features/registries/pages/draft-registration-custom-step/draft-registration-custom-step.component.spec.ts @@ -1,51 +1,44 @@ -import { Store } from '@ngxs/store'; - -import { MockComponent, MockProvider } from 'ng-mocks'; - -import { of } from 'rxjs'; +import { MockComponent, ngMocks } from 'ng-mocks'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ActivatedRoute, Router } from '@angular/router'; -import { DraftRegistrationAttributesJsonApi } from '@osf/shared/models/registration/registration-json-api.model'; +import { RegistriesSelectors } from '@osf/features/registries/store'; import { CustomStepComponent } from '../../components/custom-step/custom-step.component'; -import { RegistriesSelectors, UpdateDraft } from '../../store'; import { DraftRegistrationCustomStepComponent } from './draft-registration-custom-step.component'; +import { MOCK_REGISTRIES_PAGE } from '@testing/mocks/registries.mock'; import { OSFTestingModule } from '@testing/osf.testing.module'; import { ActivatedRouteMockBuilder } from '@testing/providers/route-provider.mock'; import { RouterMockBuilder } from '@testing/providers/router-provider.mock'; import { provideMockStore } from '@testing/providers/store-provider.mock'; +const MOCK_DRAFT = { id: 'draft-1', providerId: 'prov-1', branchedFrom: { id: 'node-1', filesLink: '/files' } }; +const MOCK_STEPS_DATA = { 'question-1': 'answer-1' }; + describe('DraftRegistrationCustomStepComponent', () => { let component: DraftRegistrationCustomStepComponent; let fixture: ComponentFixture; - let store: Store; - let mockRouter: ReturnType; let mockActivatedRoute: ReturnType; - - const mockStepsData = { stepKey: { field: 'value' } }; - const mockDraftRegistration = { - id: 'draft-1', - providerId: 'prov-1', - branchedFrom: { id: 'proj-1', filesLink: '/project/proj-1/files/' }, - }; + let mockRouter: ReturnType; beforeEach(async () => { - mockRouter = RouterMockBuilder.create().build(); - mockActivatedRoute = ActivatedRouteMockBuilder.create().withParams({ id: 'draft-1' }).build(); + mockActivatedRoute = ActivatedRouteMockBuilder.create().withParams({ id: 'draft-1', step: '1' }).build(); + mockRouter = RouterMockBuilder.create().withUrl('/registries/prov-1/draft/draft-1/custom').build(); await TestBed.configureTestingModule({ imports: [DraftRegistrationCustomStepComponent, OSFTestingModule, MockComponent(CustomStepComponent)], providers: [ - MockProvider(Router, mockRouter), - MockProvider(ActivatedRoute, mockActivatedRoute), + { provide: ActivatedRoute, useValue: mockActivatedRoute }, + { provide: Router, useValue: mockRouter }, provideMockStore({ signals: [ - { selector: RegistriesSelectors.getStepsData, value: mockStepsData }, - { selector: RegistriesSelectors.getDraftRegistration, value: mockDraftRegistration }, + { selector: RegistriesSelectors.getStepsData, value: MOCK_STEPS_DATA }, + { selector: RegistriesSelectors.getDraftRegistration, value: MOCK_DRAFT }, + { selector: RegistriesSelectors.getPagesSchema, value: [MOCK_REGISTRIES_PAGE] }, + { selector: RegistriesSelectors.getStepsState, value: { 1: { invalid: false } } }, ], }), ], @@ -53,8 +46,6 @@ describe('DraftRegistrationCustomStepComponent', () => { fixture = TestBed.createComponent(DraftRegistrationCustomStepComponent); component = fixture.componentInstance; - store = TestBed.inject(Store); - (store.dispatch as jest.Mock).mockReturnValue(of(void 0)); fixture.detectChanges(); }); @@ -62,81 +53,83 @@ describe('DraftRegistrationCustomStepComponent', () => { expect(component).toBeTruthy(); }); - it('should return stepsData and draftRegistration from store', () => { - expect(component.stepsData()).toEqual(mockStepsData); - expect(component.draftRegistration()).toEqual(mockDraftRegistration); + it('should compute inputs from draft registration', () => { + expect(component.filesLink()).toBe('/files'); + expect(component.provider()).toBe('prov-1'); + expect(component.projectId()).toBe('node-1'); }); - it('should compute filesLink from draftRegistration branchedFrom', () => { - expect(component.filesLink()).toBe('/project/proj-1/files/'); - }); + it('should dispatch updateDraft on onUpdateAction', () => { + const actionsMock = { updateDraft: jest.fn() } as any; + Object.defineProperty(component, 'actions', { value: actionsMock }); - it('should compute provider from draftRegistration providerId', () => { - expect(component.provider()).toBe('prov-1'); + component.onUpdateAction({ a: 1 } as any); + expect(actionsMock.updateDraft).toHaveBeenCalledWith('draft-1', { registration_responses: { a: 1 } }); }); - it('should compute projectId from draftRegistration branchedFrom id', () => { - expect(component.projectId()).toBe('proj-1'); + it('should navigate back to metadata on onBack', () => { + const navigateSpy = jest.spyOn(TestBed.inject(Router), 'navigate'); + component.onBack(); + expect(navigateSpy).toHaveBeenCalledWith(['../', 'metadata'], { relativeTo: TestBed.inject(ActivatedRoute) }); }); - it('should dispatch UpdateDraft with id and registration_responses payload on onUpdateAction', () => { - const attributes: Partial = { - registration_responses: { field1: 'value1' }, - }; - (store.dispatch as jest.Mock).mockClear(); - - component.onUpdateAction(attributes); - - expect(store.dispatch).toHaveBeenCalledWith(expect.any(UpdateDraft)); - const call = (store.dispatch as jest.Mock).mock.calls.find((c) => c[0] instanceof UpdateDraft); - expect(call[0].draftId).toBe('draft-1'); - expect(call[0].attributes).toEqual({ registration_responses: { registration_responses: { field1: 'value1' } } }); + it('should navigate to review on onNext', () => { + const navigateSpy = jest.spyOn(TestBed.inject(Router), 'navigate'); + component.onNext(); + expect(navigateSpy).toHaveBeenCalledWith(['../', 'review'], { relativeTo: TestBed.inject(ActivatedRoute) }); }); - it('should navigate to ../metadata on onBack', () => { - component.onBack(); - expect(mockRouter.navigate).toHaveBeenCalledWith( - ['../', 'metadata'], - expect.objectContaining({ relativeTo: expect.anything() }) - ); + it('should pass stepsData to custom step component', () => { + const customStep = ngMocks.find(CustomStepComponent).componentInstance; + expect(customStep.stepsData).toEqual(MOCK_STEPS_DATA); }); - it('should navigate to ../review on onNext', () => { - component.onNext(); - expect(mockRouter.navigate).toHaveBeenCalledWith( - ['../', 'review'], - expect.objectContaining({ relativeTo: expect.anything() }) - ); + it('should pass filesLink, projectId, and provider to custom step component', () => { + const customStep = ngMocks.find(CustomStepComponent).componentInstance; + expect(customStep.filesLink).toBe('/files'); + expect(customStep.projectId).toBe('node-1'); + expect(customStep.provider).toBe('prov-1'); }); -}); -describe('DraftRegistrationCustomStepComponent when no draft registration', () => { - let component: DraftRegistrationCustomStepComponent; - let fixture: ComponentFixture; + it('should return empty strings when draftRegistration is null', async () => { + TestBed.resetTestingModule(); + mockActivatedRoute = ActivatedRouteMockBuilder.create().withParams({ id: 'draft-1', step: '1' }).build(); + mockRouter = RouterMockBuilder.create().withUrl('/registries/prov-1/draft/draft-1/custom').build(); - beforeEach(async () => { await TestBed.configureTestingModule({ imports: [DraftRegistrationCustomStepComponent, OSFTestingModule, MockComponent(CustomStepComponent)], providers: [ - MockProvider(Router, RouterMockBuilder.create().build()), - MockProvider(ActivatedRoute, ActivatedRouteMockBuilder.create().withParams({ id: 'draft-1' }).build()), + { provide: ActivatedRoute, useValue: mockActivatedRoute }, + { provide: Router, useValue: mockRouter }, provideMockStore({ signals: [ { selector: RegistriesSelectors.getStepsData, value: {} }, { selector: RegistriesSelectors.getDraftRegistration, value: null }, + { selector: RegistriesSelectors.getPagesSchema, value: [MOCK_REGISTRIES_PAGE] }, + { selector: RegistriesSelectors.getStepsState, value: {} }, ], }), ], }).compileComponents(); - fixture = TestBed.createComponent(DraftRegistrationCustomStepComponent); - component = fixture.componentInstance; - fixture.detectChanges(); + const nullFixture = TestBed.createComponent(DraftRegistrationCustomStepComponent); + const nullComponent = nullFixture.componentInstance; + nullFixture.detectChanges(); + + expect(nullComponent.filesLink()).toBe(''); + expect(nullComponent.provider()).toBe(''); + expect(nullComponent.projectId()).toBe(''); }); - it('should compute empty filesLink provider and projectId', () => { - expect(component.filesLink()).toBe(''); - expect(component.provider()).toBe(''); - expect(component.projectId()).toBe(''); + it('should wrap attributes in registration_responses on update', () => { + const actionsMock = { updateDraft: jest.fn() } as any; + Object.defineProperty(component, 'actions', { value: actionsMock }); + + const attributes = { field1: 'value1', field2: ['a', 'b'] }; + component.onUpdateAction(attributes as any); + + expect(actionsMock.updateDraft).toHaveBeenCalledWith('draft-1', { + registration_responses: { field1: 'value1', field2: ['a', 'b'] }, + }); }); }); diff --git a/src/app/features/registries/pages/justification/justification.component.spec.ts b/src/app/features/registries/pages/justification/justification.component.spec.ts index ddbee0e57..3efa9f79c 100644 --- a/src/app/features/registries/pages/justification/justification.component.spec.ts +++ b/src/app/features/registries/pages/justification/justification.component.spec.ts @@ -1,87 +1,239 @@ +import { Store } from '@ngxs/store'; + import { MockComponents, MockProvider } from 'ng-mocks'; +import { of } from 'rxjs'; + import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { ActivatedRoute, Router } from '@angular/router'; +import { ActivatedRoute, NavigationEnd, Router } from '@angular/router'; -import { RegistriesSelectors } from '@osf/features/registries/store'; import { StepperComponent } from '@osf/shared/components/stepper/stepper.component'; import { SubHeaderComponent } from '@osf/shared/components/sub-header/sub-header.component'; +import { RevisionReviewStates } from '@osf/shared/enums/revision-review-states.enum'; +import { PageSchema } from '@osf/shared/models/registration/page-schema.model'; +import { SchemaResponse } from '@osf/shared/models/registration/schema-response.model'; import { LoaderService } from '@osf/shared/services/loader.service'; +import { RegistriesSelectors } from '../../store'; + import { JustificationComponent } from './justification.component'; +import { createMockSchemaResponse } from '@testing/mocks/schema-response.mock'; import { OSFTestingModule } from '@testing/osf.testing.module'; -import { RouterMockBuilder } from '@testing/providers/router-provider.mock'; +import { RouterMockBuilder, RouterMockType } from '@testing/providers/router-provider.mock'; import { provideMockStore } from '@testing/providers/store-provider.mock'; +const MOCK_SCHEMA_RESPONSE = createMockSchemaResponse('resp-1', RevisionReviewStates.RevisionInProgress); + +const MOCK_PAGES: PageSchema[] = [ + { id: 'page-1', title: 'Page One', questions: [{ id: 'q1', displayText: 'Q1', required: true, responseKey: 'q1' }] }, + { id: 'page-2', title: 'Page Two', questions: [{ id: 'q2', displayText: 'Q2', required: false, responseKey: 'q2' }] }, +]; + +function buildActivatedRoute(params: Record = {}) { + return { + snapshot: { firstChild: { params } }, + firstChild: { snapshot: { params } }, + } as unknown as ActivatedRoute; +} + describe('JustificationComponent', () => { let component: JustificationComponent; let fixture: ComponentFixture; - let mockActivatedRoute: Partial; - let mockRouter: ReturnType; - - beforeEach(async () => { - mockActivatedRoute = { - snapshot: { - firstChild: { params: { id: 'rev-1', step: '0' } } as any, - } as any, - firstChild: { snapshot: { params: { id: 'rev-1', step: '0' } } } as any, - } as Partial; - mockRouter = RouterMockBuilder.create().withUrl('/registries/revisions/rev-1/justification').build(); - - await TestBed.configureTestingModule({ + let mockRouter: RouterMockType; + let routerBuilder: RouterMockBuilder; + let loaderService: jest.Mocked; + let actionsMock: { + getSchemaBlocks: jest.Mock; + clearState: jest.Mock; + getSchemaResponse: jest.Mock; + updateStepState: jest.Mock; + }; + + function setup( + options: { + routeParams?: Record; + routerUrl?: string; + schemaResponse?: SchemaResponse | null; + pages?: PageSchema[]; + stepsState?: Record; + revisionData?: Record; + } = {} + ) { + const { + routeParams = { id: 'rev-1' }, + routerUrl = '/registries/revisions/rev-1/justification', + schemaResponse = MOCK_SCHEMA_RESPONSE, + pages = MOCK_PAGES, + stepsState = {}, + revisionData = MOCK_SCHEMA_RESPONSE.revisionResponses, + } = options; + + fixture?.destroy(); + TestBed.resetTestingModule(); + + routerBuilder = RouterMockBuilder.create().withUrl(routerUrl); + mockRouter = routerBuilder.build(); + loaderService = { show: jest.fn(), hide: jest.fn() } as unknown as jest.Mocked; + + TestBed.configureTestingModule({ imports: [JustificationComponent, OSFTestingModule, ...MockComponents(StepperComponent, SubHeaderComponent)], providers: [ - MockProvider(ActivatedRoute, mockActivatedRoute), - MockProvider(Router, mockRouter), - MockProvider(LoaderService, { show: jest.fn(), hide: jest.fn() }), + { provide: ActivatedRoute, useValue: buildActivatedRoute(routeParams) }, + { provide: Router, useValue: mockRouter }, + MockProvider(LoaderService, loaderService), provideMockStore({ signals: [ - { selector: RegistriesSelectors.getPagesSchema, value: [] }, - { selector: RegistriesSelectors.getStepsState, value: { 0: { invalid: false, touched: false } } }, - { - selector: RegistriesSelectors.getSchemaResponse, - value: { - registrationSchemaId: 'schema-1', - revisionJustification: 'Reason', - reviewsState: 'revision_in_progress', - }, - }, - { selector: RegistriesSelectors.getSchemaResponseRevisionData, value: {} }, + { selector: RegistriesSelectors.getSchemaResponse, value: schemaResponse }, + { selector: RegistriesSelectors.getPagesSchema, value: pages }, + { selector: RegistriesSelectors.getStepsState, value: stepsState }, + { selector: RegistriesSelectors.getSchemaResponseRevisionData, value: revisionData }, ], }), ], - }).compileComponents(); + }); fixture = TestBed.createComponent(JustificationComponent); component = fixture.componentInstance; + + actionsMock = { + getSchemaBlocks: jest.fn().mockReturnValue(of({})), + clearState: jest.fn().mockReturnValue(of({})), + getSchemaResponse: jest.fn().mockReturnValue(of({})), + updateStepState: jest.fn().mockReturnValue(of({})), + }; + Object.defineProperty(component, 'actions', { value: actionsMock, writable: true }); + fixture.detectChanges(); - }); + } + + beforeEach(() => setup()); + + afterEach(() => fixture?.destroy()); it('should create', () => { expect(component).toBeTruthy(); }); - it('should compute steps with justification and review', () => { + it('should extract revisionId from route params', () => { + setup({ routeParams: { id: 'rev-42' } }); + expect(component.revisionId).toBe('rev-42'); + }); + + it('should default revisionId to empty string when no id param', () => { + setup({ routeParams: {} }); + expect(component.revisionId).toBe(''); + }); + + it('should build justification as first and review as last step with custom steps in between', () => { + const steps = component.steps(); + expect(steps.length).toBe(4); + expect(steps[0]).toEqual(expect.objectContaining({ index: 0, value: 'justification', routeLink: 'justification' })); + expect(steps[1]).toEqual(expect.objectContaining({ index: 1, label: 'Page One', value: 'page-1', routeLink: '1' })); + expect(steps[2]).toEqual(expect.objectContaining({ index: 2, label: 'Page Two', value: 'page-2', routeLink: '2' })); + expect(steps[3]).toEqual( + expect.objectContaining({ index: 3, value: 'review', routeLink: 'review', invalid: false }) + ); + }); + + it('should mark justification step as invalid when revisionJustification is empty', () => { + setup({ schemaResponse: { ...MOCK_SCHEMA_RESPONSE, revisionJustification: '' } }); + const step = component.steps()[0]; + expect(step.invalid).toBe(true); + expect(step.touched).toBe(false); + }); + + it('should disable steps when reviewsState is not RevisionInProgress', () => { + setup({ schemaResponse: createMockSchemaResponse('resp-1', RevisionReviewStates.Approved) }); + const steps = component.steps(); + expect(steps[0].disabled).toBe(true); + expect(steps[1].disabled).toBe(true); + }); + + it('should apply stepsState invalid/touched to custom steps', () => { + setup({ stepsState: { 1: { invalid: true, touched: true }, 2: { invalid: false, touched: false } } }); + const steps = component.steps(); + expect(steps[1]).toEqual(expect.objectContaining({ invalid: true, touched: true })); + expect(steps[2]).toEqual(expect.objectContaining({ invalid: false, touched: false })); + }); + + it('should handle null schemaResponse gracefully', () => { + setup({ schemaResponse: null }); + const step = component.steps()[0]; + expect(step.invalid).toBe(true); + expect(step.disabled).toBe(true); + }); + + it('should produce only justification and review when no pages', () => { + setup({ pages: [] }); const steps = component.steps(); expect(steps.length).toBe(2); expect(steps[0].value).toBe('justification'); - expect(steps[1].value).toBe('review'); + expect(steps[1]).toEqual(expect.objectContaining({ index: 1, value: 'review' })); + }); + + it('should initialize currentStepIndex from route step param', () => { + setup({ routeParams: { id: 'rev-1', step: '2' } }); + expect(component.currentStepIndex()).toBe(2); + }); + + it('should default currentStepIndex to 0 when no step param', () => { + expect(component.currentStepIndex()).toBe(0); + }); + + it('should return the step at currentStepIndex', () => { + component.currentStepIndex.set(0); + expect(component.currentStep().value).toBe('justification'); + }); + + it('should update currentStepIndex and navigate on stepChange', () => { + component.stepChange({ index: 1, label: 'Page One', value: 'page-1' } as any); + + expect(component.currentStepIndex()).toBe(1); + expect(mockRouter.navigate).toHaveBeenCalledWith(['/registries/revisions/rev-1/', '1']); + }); + + it('should navigate to review route for last step', () => { + const reviewIndex = component.steps().length - 1; + component.stepChange({ index: reviewIndex, label: 'Review', value: 'review' } as any); + + expect(mockRouter.navigate).toHaveBeenCalledWith(['/registries/revisions/rev-1/', 'review']); + }); + + it('should update currentStepIndex on NavigationEnd', () => { + setup({ routeParams: { id: 'rev-1', step: '2' }, routerUrl: '/registries/revisions/rev-1/2' }); + + routerBuilder.emit(new NavigationEnd(1, '/test', '/test')); + + expect(component.currentStepIndex()).toBe(2); + }); + + it('should show loader on init', () => { + expect(loaderService.show).toHaveBeenCalled(); + }); + + it('should dispatch FetchSchemaResponse when not already loaded', () => { + setup({ schemaResponse: null }); + const store = TestBed.inject(Store); + expect(store.dispatch).toHaveBeenCalled(); }); - it('should navigate on stepChange', () => { - const navSpy = jest.spyOn(TestBed.inject(Router), 'navigate'); - component.stepChange({ index: 1, routeLink: '1', value: 'p1', label: 'Page 1' } as any); - expect(navSpy).toHaveBeenCalledWith(['/registries/revisions/rev-1/', 'review']); + it('should not dispatch FetchSchemaResponse when already loaded', () => { + const store = TestBed.inject(Store); + expect(store.dispatch).not.toHaveBeenCalled(); }); - it('should clear state on destroy', () => { - const actionsMock = { - clearState: jest.fn(), - getSchemaBlocks: jest.fn().mockReturnValue({ pipe: () => ({ subscribe: () => {} }) }), - } as any; - Object.defineProperty(component as any, 'actions', { value: actionsMock }); - fixture.destroy(); + it('should dispatch clearState on destroy', () => { + component.ngOnDestroy(); expect(actionsMock.clearState).toHaveBeenCalled(); }); + + it('should detect review page from URL', () => { + setup({ routerUrl: '/registries/revisions/rev-1/review' }); + expect(component['isReviewPage']).toBe(true); + }); + + it('should return false for isReviewPage when not on review', () => { + expect(component['isReviewPage']).toBe(false); + }); }); diff --git a/src/app/features/registries/pages/justification/justification.component.ts b/src/app/features/registries/pages/justification/justification.component.ts index e196e9038..610dfd668 100644 --- a/src/app/features/registries/pages/justification/justification.component.ts +++ b/src/app/features/registries/pages/justification/justification.component.ts @@ -2,7 +2,7 @@ import { createDispatchMap, select } from '@ngxs/store'; import { TranslatePipe, TranslateService } from '@ngx-translate/core'; -import { filter, tap } from 'rxjs'; +import { filter } from 'rxjs'; import { ChangeDetectionStrategy, @@ -12,7 +12,6 @@ import { effect, inject, OnDestroy, - Signal, signal, untracked, } from '@angular/core'; @@ -33,21 +32,14 @@ import { ClearState, FetchSchemaBlocks, FetchSchemaResponse, RegistriesSelectors templateUrl: './justification.component.html', styleUrl: './justification.component.scss', changeDetection: ChangeDetectionStrategy.OnPush, - providers: [TranslateService], }) export class JustificationComponent implements OnDestroy { private readonly route = inject(ActivatedRoute); private readonly router = inject(Router); private readonly destroyRef = inject(DestroyRef); - private readonly loaderService = inject(LoaderService); private readonly translateService = inject(TranslateService); - readonly pages = select(RegistriesSelectors.getPagesSchema); - readonly stepsState = select(RegistriesSelectors.getStepsState); - readonly schemaResponse = select(RegistriesSelectors.getSchemaResponse); - readonly schemaResponseRevisionData = select(RegistriesSelectors.getSchemaResponseRevisionData); - private readonly actions = createDispatchMap({ getSchemaBlocks: FetchSchemaBlocks, clearState: ClearState, @@ -55,61 +47,79 @@ export class JustificationComponent implements OnDestroy { updateStepState: UpdateStepState, }); + readonly pages = select(RegistriesSelectors.getPagesSchema); + readonly stepsState = select(RegistriesSelectors.getStepsState); + readonly schemaResponse = select(RegistriesSelectors.getSchemaResponse); + readonly schemaResponseRevisionData = select(RegistriesSelectors.getSchemaResponseRevisionData); + + readonly revisionId = this.route.snapshot.firstChild?.params['id'] || ''; + get isReviewPage(): boolean { return this.router.url.includes('/review'); } - reviewStep!: StepOption; - justificationStep!: StepOption; - revisionId = this.route.snapshot.firstChild?.params['id'] || ''; + readonly steps = computed(() => { + const response = this.schemaResponse(); + const isJustificationValid = !!response?.revisionJustification; + const isDisabled = response?.reviewsState !== RevisionReviewStates.RevisionInProgress; + const stepState = this.stepsState(); + const pages = this.pages(); - steps: Signal = computed(() => { - const isJustificationValid = !!this.schemaResponse()?.revisionJustification; - this.justificationStep = { + const justificationStep: StepOption = { index: 0, value: 'justification', label: this.translateService.instant('registries.justification.step'), invalid: !isJustificationValid, touched: isJustificationValid, routeLink: 'justification', - disabled: this.schemaResponse()?.reviewsState !== RevisionReviewStates.RevisionInProgress, + disabled: isDisabled, }; - this.reviewStep = { - index: 1, + const customSteps: StepOption[] = pages.map((page, index) => ({ + index: index + 1, + label: page.title, + value: page.id, + routeLink: `${index + 1}`, + invalid: stepState?.[index + 1]?.invalid || false, + touched: stepState?.[index + 1]?.touched || false, + disabled: isDisabled, + })); + + const reviewStep: StepOption = { + index: customSteps.length + 1, value: 'review', label: this.translateService.instant('registries.review.step'), invalid: false, routeLink: 'review', }; - const stepState = this.stepsState(); - const customSteps = this.pages().map((page, index) => { - return { - index: index + 1, - label: page.title, - value: page.id, - routeLink: `${index + 1}`, - invalid: stepState?.[index + 1]?.invalid || false, - touched: stepState?.[index + 1]?.touched || false, - disabled: this.schemaResponse()?.reviewsState !== RevisionReviewStates.RevisionInProgress, - }; - }); - return [ - { ...this.justificationStep }, - ...customSteps, - { ...this.reviewStep, index: customSteps.length + 1, invalid: false }, - ]; + + return [justificationStep, ...customSteps, reviewStep]; }); currentStepIndex = signal( this.route.snapshot.firstChild?.params['step'] ? +this.route.snapshot.firstChild?.params['step'] : 0 ); - currentStep = computed(() => { - return this.steps()[this.currentStepIndex()]; - }); + currentStep = computed(() => this.steps()[this.currentStepIndex()]); constructor() { + this.initRouterListener(); + this.initDataFetching(); + this.initReviewPageSync(); + this.initStepValidation(); + } + + ngOnDestroy(): void { + this.actions.clearState(); + } + + stepChange(step: StepOption): void { + this.currentStepIndex.set(step.index); + const pageLink = this.steps()[step.index].routeLink; + this.router.navigate([`/registries/revisions/${this.revisionId}/`, pageLink]); + } + + private initRouterListener(): void { this.router.events .pipe( takeUntilDestroyed(this.destroyRef), @@ -120,47 +130,56 @@ export class JustificationComponent implements OnDestroy { if (step) { this.currentStepIndex.set(+step); } else if (this.isReviewPage) { - const reviewStepIndex = this.pages().length + 1; - this.currentStepIndex.set(reviewStepIndex); + this.currentStepIndex.set(this.pages().length + 1); } else { this.currentStepIndex.set(0); } }); + } + private initDataFetching(): void { this.loaderService.show(); + if (!this.schemaResponse()) { this.actions.getSchemaResponse(this.revisionId); } effect(() => { const registrationSchemaId = this.schemaResponse()?.registrationSchemaId; + if (registrationSchemaId) { - this.actions - .getSchemaBlocks(registrationSchemaId) - .pipe(tap(() => this.loaderService.hide())) - .subscribe(); + this.actions.getSchemaBlocks(registrationSchemaId).subscribe(() => this.loaderService.hide()); } }); + } + private initReviewPageSync(): void { effect(() => { const reviewStepIndex = this.pages().length + 1; + if (this.isReviewPage) { this.currentStepIndex.set(reviewStepIndex); } }); + } + private initStepValidation(): void { effect(() => { + const currentIndex = this.currentStepIndex(); + const pages = this.pages(); + const revisionData = this.schemaResponseRevisionData(); const stepState = untracked(() => this.stepsState()); - if (this.currentStepIndex() > 0) { + if (currentIndex > 0) { this.actions.updateStepState('0', true, stepState?.[0]?.touched || false); } - if (this.pages().length && this.currentStepIndex() > 0 && this.schemaResponseRevisionData()) { - for (let i = 1; i < this.currentStepIndex(); i++) { - const pageStep = this.pages()[i - 1]; + + if (pages.length && currentIndex > 0 && revisionData) { + for (let i = 1; i < currentIndex; i++) { + const pageStep = pages[i - 1]; const isStepInvalid = pageStep?.questions?.some((question) => { - const questionData = this.schemaResponseRevisionData()[question.responseKey!]; + const questionData = revisionData[question.responseKey!]; return question.required && (Array.isArray(questionData) ? !questionData.length : !questionData); }) || false; this.actions.updateStepState(i.toString(), isStepInvalid, stepState?.[i]?.touched || false); @@ -168,14 +187,4 @@ export class JustificationComponent implements OnDestroy { } }); } - - stepChange(step: StepOption): void { - this.currentStepIndex.set(step.index); - const pageLink = this.steps()[step.index].routeLink; - this.router.navigate([`/registries/revisions/${this.revisionId}/`, pageLink]); - } - - ngOnDestroy(): void { - this.actions.clearState(); - } } diff --git a/src/app/features/registries/pages/my-registrations-redirect/my-registrations-redirect.component.spec.ts b/src/app/features/registries/pages/my-registrations-redirect/my-registrations-redirect.component.spec.ts index d4f40e53f..b82eba951 100644 --- a/src/app/features/registries/pages/my-registrations-redirect/my-registrations-redirect.component.spec.ts +++ b/src/app/features/registries/pages/my-registrations-redirect/my-registrations-redirect.component.spec.ts @@ -28,24 +28,10 @@ describe('MyRegistrationsRedirectComponent', () => { expect(component).toBeTruthy(); }); - it('should be an instance of MyRegistrationsRedirectComponent', () => { - expect(component).toBeInstanceOf(MyRegistrationsRedirectComponent); - }); - it('should navigate to /my-registrations on component creation', () => { expect(router.navigate).toHaveBeenCalledWith(['/my-registrations'], { queryParamsHandling: 'preserve', replaceUrl: true, }); }); - - it('should preserve query parameters during navigation', () => { - const navigationOptions = router.navigate.mock.calls[0][1]; - expect(navigationOptions?.queryParamsHandling).toBe('preserve'); - }); - - it('should replace the current URL in browser history', () => { - const navigationOptions = router.navigate.mock.calls[0][1]; - expect(navigationOptions?.replaceUrl).toBe(true); - }); }); diff --git a/src/app/features/registries/pages/my-registrations/my-registrations.component.html b/src/app/features/registries/pages/my-registrations/my-registrations.component.html index d9197ccaa..45a6b4e5f 100644 --- a/src/app/features/registries/pages/my-registrations/my-registrations.component.html +++ b/src/app/features/registries/pages/my-registrations/my-registrations.component.html @@ -10,7 +10,7 @@
- + @if (!isMobile()) { @for (tab of tabOptions; track tab.value) { diff --git a/src/app/features/registries/pages/my-registrations/my-registrations.component.spec.ts b/src/app/features/registries/pages/my-registrations/my-registrations.component.spec.ts index 5f1c0f4e4..7ec894828 100644 --- a/src/app/features/registries/pages/my-registrations/my-registrations.component.spec.ts +++ b/src/app/features/registries/pages/my-registrations/my-registrations.component.spec.ts @@ -98,7 +98,7 @@ describe('MyRegistrationsComponent', () => { getDraftRegistrations: jest.fn(), getSubmittedRegistrations: jest.fn(), deleteDraft: jest.fn(), - } as any; + }; Object.defineProperty(component, 'actions', { value: actionsMock }); const navigateSpy = jest.spyOn(mockRouter, 'navigate'); @@ -119,7 +119,7 @@ describe('MyRegistrationsComponent', () => { getDraftRegistrations: jest.fn(), getSubmittedRegistrations: jest.fn(), deleteDraft: jest.fn(), - } as any; + }; Object.defineProperty(component, 'actions', { value: actionsMock }); const navigateSpy = jest.spyOn(mockRouter, 'navigate'); @@ -135,16 +135,32 @@ describe('MyRegistrationsComponent', () => { }); }); - it('should not process tab change if tab is not a number', () => { + it('should not process tab change if tab value is invalid', () => { const actionsMock = { getDraftRegistrations: jest.fn(), getSubmittedRegistrations: jest.fn(), deleteDraft: jest.fn(), - } as any; + }; Object.defineProperty(component, 'actions', { value: actionsMock }); const initialTab = component.selectedTab(); - component.onTabChange('invalid' as any); + component.onTabChange('invalid'); + + expect(component.selectedTab()).toBe(initialTab); + expect(actionsMock.getDraftRegistrations).not.toHaveBeenCalled(); + expect(actionsMock.getSubmittedRegistrations).not.toHaveBeenCalled(); + }); + + it('should not process tab change if tab is not a string', () => { + const actionsMock = { + getDraftRegistrations: jest.fn(), + getSubmittedRegistrations: jest.fn(), + deleteDraft: jest.fn(), + }; + Object.defineProperty(component, 'actions', { value: actionsMock }); + const initialTab = component.selectedTab(); + + component.onTabChange(0); expect(component.selectedTab()).toBe(initialTab); expect(actionsMock.getDraftRegistrations).not.toHaveBeenCalled(); @@ -158,17 +174,17 @@ describe('MyRegistrationsComponent', () => { }); it('should handle drafts pagination', () => { - const actionsMock = { getDraftRegistrations: jest.fn() } as any; + const actionsMock = { getDraftRegistrations: jest.fn() }; Object.defineProperty(component, 'actions', { value: actionsMock }); - component.onDraftsPageChange({ page: 2, first: 20 } as any); + component.onDraftsPageChange({ page: 2, first: 20 }); expect(actionsMock.getDraftRegistrations).toHaveBeenCalledWith(3); expect(component.draftFirst).toBe(20); }); it('should handle submitted pagination', () => { - const actionsMock = { getSubmittedRegistrations: jest.fn() } as any; + const actionsMock = { getSubmittedRegistrations: jest.fn() }; Object.defineProperty(component, 'actions', { value: actionsMock }); - component.onSubmittedPageChange({ page: 1, first: 10 } as any); + component.onSubmittedPageChange({ page: 1, first: 10 }); expect(actionsMock.getSubmittedRegistrations).toHaveBeenCalledWith(2); expect(component.submittedFirst).toBe(10); }); @@ -178,7 +194,7 @@ describe('MyRegistrationsComponent', () => { getDraftRegistrations: jest.fn(), getSubmittedRegistrations: jest.fn(), deleteDraft: jest.fn(() => of({})), - } as any; + }; Object.defineProperty(component, 'actions', { value: actionsMock }); customConfirmationService.confirmDelete.mockImplementation(({ onConfirm }) => { onConfirm(); @@ -201,7 +217,7 @@ describe('MyRegistrationsComponent', () => { getDraftRegistrations: jest.fn(), getSubmittedRegistrations: jest.fn(), deleteDraft: jest.fn(), - } as any; + }; Object.defineProperty(component, 'actions', { value: actionsMock }); customConfirmationService.confirmDelete.mockImplementation(() => {}); @@ -219,7 +235,7 @@ describe('MyRegistrationsComponent', () => { getDraftRegistrations: jest.fn(), getSubmittedRegistrations: jest.fn(), deleteDraft: jest.fn(), - } as any; + }; Object.defineProperty(component, 'actions', { value: actionsMock }); component.onTabChange(RegistrationTab.Drafts); @@ -233,7 +249,7 @@ describe('MyRegistrationsComponent', () => { getDraftRegistrations: jest.fn(), getSubmittedRegistrations: jest.fn(), deleteDraft: jest.fn(), - } as any; + }; Object.defineProperty(component, 'actions', { value: actionsMock }); component.onTabChange(RegistrationTab.Submitted); diff --git a/src/app/features/registries/pages/my-registrations/my-registrations.component.ts b/src/app/features/registries/pages/my-registrations/my-registrations.component.ts index 106db16e9..95179b7b7 100644 --- a/src/app/features/registries/pages/my-registrations/my-registrations.component.ts +++ b/src/app/features/registries/pages/my-registrations/my-registrations.component.ts @@ -10,7 +10,6 @@ import { TabsModule } from 'primeng/tabs'; import { NgTemplateOutlet } from '@angular/common'; import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core'; import { toSignal } from '@angular/core/rxjs-interop'; -import { FormsModule } from '@angular/forms'; import { ActivatedRoute, Router, RouterLink } from '@angular/router'; import { ENVIRONMENT } from '@core/provider/environment.provider'; @@ -30,17 +29,16 @@ import { DeleteDraft, FetchDraftRegistrations, FetchSubmittedRegistrations, Regi @Component({ selector: 'osf-my-registrations', imports: [ - SubHeaderComponent, - TranslatePipe, - TabsModule, - FormsModule, - SelectComponent, - RegistrationCardComponent, - CustomPaginatorComponent, - Skeleton, Button, + Skeleton, + TabsModule, RouterLink, NgTemplateOutlet, + CustomPaginatorComponent, + RegistrationCardComponent, + SelectComponent, + SubHeaderComponent, + TranslatePipe, ], templateUrl: './my-registrations.component.html', styleUrl: './my-registrations.component.scss', @@ -82,26 +80,28 @@ export class MyRegistrationsComponent { constructor() { const initialTab = this.route.snapshot.queryParams['tab']; - const selectedTab = initialTab == 'drafts' ? RegistrationTab.Drafts : RegistrationTab.Submitted; + const selectedTab = initialTab === RegistrationTab.Drafts ? RegistrationTab.Drafts : RegistrationTab.Submitted; this.onTabChange(selectedTab); } onTabChange(tab: Primitive): void { - if (typeof tab !== 'number') { + if (typeof tab !== 'string' || !Object.values(RegistrationTab).includes(tab as RegistrationTab)) { return; } - this.selectedTab.set(tab); - this.loadTabData(tab); + const validTab = tab as RegistrationTab; + + this.selectedTab.set(validTab); + this.loadTabData(validTab); this.router.navigate([], { relativeTo: this.route, - queryParams: { tab: tab === RegistrationTab.Drafts ? 'drafts' : 'submitted' }, + queryParams: { tab }, queryParamsHandling: 'merge', }); } - private loadTabData(tab: number): void { + private loadTabData(tab: RegistrationTab): void { if (tab === RegistrationTab.Drafts) { this.draftFirst = 0; this.actions.getDraftRegistrations(); diff --git a/src/app/features/registries/pages/registries-landing/registries-landing.component.spec.ts b/src/app/features/registries/pages/registries-landing/registries-landing.component.spec.ts index cf4780553..b23fca833 100644 --- a/src/app/features/registries/pages/registries-landing/registries-landing.component.spec.ts +++ b/src/app/features/registries/pages/registries-landing/registries-landing.component.spec.ts @@ -8,7 +8,6 @@ import { LoadingSpinnerComponent } from '@osf/shared/components/loading-spinner/ import { ResourceCardComponent } from '@osf/shared/components/resource-card/resource-card.component'; import { SearchInputComponent } from '@osf/shared/components/search-input/search-input.component'; import { SubHeaderComponent } from '@osf/shared/components/sub-header/sub-header.component'; -import { RegistrationProviderSelectors } from '@osf/shared/stores/registration-provider'; import { RegistryServicesComponent } from '../../components/registry-services/registry-services.component'; import { RegistriesSelectors } from '../../store'; @@ -44,8 +43,6 @@ describe('RegistriesLandingComponent', () => { { provide: Router, useValue: mockRouter }, provideMockStore({ signals: [ - { selector: RegistrationProviderSelectors.getBrandedProvider, value: null }, - { selector: RegistrationProviderSelectors.isBrandedProviderLoading, value: false }, { selector: RegistriesSelectors.getRegistries, value: [] }, { selector: RegistriesSelectors.isRegistriesLoading, value: false }, ], diff --git a/src/app/features/registries/pages/registries-landing/registries-landing.component.ts b/src/app/features/registries/pages/registries-landing/registries-landing.component.ts index 1aa9c22c8..917caf2fc 100644 --- a/src/app/features/registries/pages/registries-landing/registries-landing.component.ts +++ b/src/app/features/registries/pages/registries-landing/registries-landing.component.ts @@ -18,11 +18,7 @@ import { SearchInputComponent } from '@osf/shared/components/search-input/search import { SubHeaderComponent } from '@osf/shared/components/sub-header/sub-header.component'; import { ResourceType } from '@osf/shared/enums/resource-type.enum'; import { normalizeQuotes } from '@osf/shared/helpers/normalize-quotes'; -import { - ClearRegistryProvider, - GetRegistryProvider, - RegistrationProviderSelectors, -} from '@osf/shared/stores/registration-provider'; +import { ClearRegistryProvider, GetRegistryProvider } from '@osf/shared/stores/registration-provider'; import { RegistryServicesComponent } from '../../components/registry-services/registry-services.component'; import { GetRegistries, RegistriesSelectors } from '../../store'; @@ -44,10 +40,9 @@ import { GetRegistries, RegistriesSelectors } from '../../store'; changeDetection: ChangeDetectionStrategy.OnPush, }) export class RegistriesLandingComponent implements OnInit, OnDestroy { - private router = inject(Router); + private readonly router = inject(Router); private readonly environment = inject(ENVIRONMENT); - private readonly platformId = inject(PLATFORM_ID); - private readonly isBrowser = isPlatformBrowser(this.platformId); + private readonly isBrowser = isPlatformBrowser(inject(PLATFORM_ID)); private actions = createDispatchMap({ getRegistries: GetRegistries, @@ -56,8 +51,6 @@ export class RegistriesLandingComponent implements OnInit, OnDestroy { clearRegistryProvider: ClearRegistryProvider, }); - provider = select(RegistrationProviderSelectors.getBrandedProvider); - isProviderLoading = select(RegistrationProviderSelectors.isBrandedProviderLoading); registries = select(RegistriesSelectors.getRegistries); isRegistriesLoading = select(RegistriesSelectors.isRegistriesLoading); diff --git a/src/app/features/registries/pages/registries-provider-search/registries-provider-search.component.spec.ts b/src/app/features/registries/pages/registries-provider-search/registries-provider-search.component.spec.ts index 6498fed94..f29b7f8c7 100644 --- a/src/app/features/registries/pages/registries-provider-search/registries-provider-search.component.spec.ts +++ b/src/app/features/registries/pages/registries-provider-search/registries-provider-search.component.spec.ts @@ -1,11 +1,14 @@ -import { MockComponents, MockProvider } from 'ng-mocks'; +import { MockComponents } from 'ng-mocks'; +import { of } from 'rxjs'; + +import { PLATFORM_ID } from '@angular/core'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { ActivatedRoute } from '@angular/router'; +import { ActivatedRoute, Params } from '@angular/router'; import { RegistryProviderHeroComponent } from '@osf/features/registries/components/registry-provider-hero/registry-provider-hero.component'; import { GlobalSearchComponent } from '@osf/shared/components/global-search/global-search.component'; -import { CustomDialogService } from '@osf/shared/services/custom-dialog.service'; +import { ResourceType } from '@osf/shared/enums/resource-type.enum'; import { RegistrationProviderSelectors } from '@osf/shared/stores/registration-provider'; import { RegistriesProviderSearchComponent } from './registries-provider-search.component'; @@ -14,54 +17,100 @@ import { OSFTestingModule } from '@testing/osf.testing.module'; import { ActivatedRouteMockBuilder } from '@testing/providers/route-provider.mock'; import { provideMockStore } from '@testing/providers/store-provider.mock'; +const MOCK_PROVIDER = { iri: 'http://iri/provider', name: 'Test Provider' }; + describe('RegistriesProviderSearchComponent', () => { let component: RegistriesProviderSearchComponent; let fixture: ComponentFixture; + let actionsMock: { + getProvider: jest.Mock; + setDefaultFilterValue: jest.Mock; + setResourceType: jest.Mock; + clearCurrentProvider: jest.Mock; + clearRegistryProvider: jest.Mock; + }; + + function setup(options: { params?: Params; platformId?: string } = {}) { + const { params = { providerId: 'osf' }, platformId = 'browser' } = options; - beforeEach(async () => { - const routeMock = ActivatedRouteMockBuilder.create().withParams({ name: 'osf' }).build(); + fixture?.destroy(); + TestBed.resetTestingModule(); - await TestBed.configureTestingModule({ + TestBed.configureTestingModule({ imports: [ RegistriesProviderSearchComponent, OSFTestingModule, ...MockComponents(GlobalSearchComponent, RegistryProviderHeroComponent), ], providers: [ - { provide: ActivatedRoute, useValue: routeMock }, - MockProvider(CustomDialogService, { open: jest.fn() }), + { provide: ActivatedRoute, useValue: ActivatedRouteMockBuilder.create().withParams(params).build() }, + { provide: PLATFORM_ID, useValue: platformId }, provideMockStore({ signals: [ - { selector: RegistrationProviderSelectors.getBrandedProvider, value: { iri: 'http://iri/provider' } }, + { selector: RegistrationProviderSelectors.getBrandedProvider, value: MOCK_PROVIDER }, { selector: RegistrationProviderSelectors.isBrandedProviderLoading, value: false }, ], }), ], - }).compileComponents(); + }); fixture = TestBed.createComponent(RegistriesProviderSearchComponent); component = fixture.componentInstance; - }); - it('should create', () => { + actionsMock = { + getProvider: jest.fn().mockReturnValue(of({})), + setDefaultFilterValue: jest.fn().mockReturnValue(of({})), + setResourceType: jest.fn().mockReturnValue(of({})), + clearCurrentProvider: jest.fn().mockReturnValue(of({})), + clearRegistryProvider: jest.fn().mockReturnValue(of({})), + }; + Object.defineProperty(component, 'actions', { value: actionsMock, writable: true }); + fixture.detectChanges(); + } + + beforeEach(() => setup()); + + afterEach(() => fixture?.destroy()); + + it('should create', () => { expect(component).toBeTruthy(); }); - it('should clear providers on destroy', () => { - fixture.detectChanges(); + it('should initialize searchControl with empty string', () => { + expect(component.searchControl.value).toBe(''); + }); - const actionsMock = { - getProvider: jest.fn(), - setDefaultFilterValue: jest.fn(), - setResourceType: jest.fn(), - clearCurrentProvider: jest.fn(), - clearRegistryProvider: jest.fn(), - } as any; - Object.defineProperty(component as any, 'actions', { value: actionsMock }); + it('should initialize defaultSearchFiltersInitialized as false before ngOnInit completes', () => { + setup({ params: {} }); + expect(component.defaultSearchFiltersInitialized()).toBe(false); + }); + + it('should fetch provider and set filters on init when providerId exists', () => { + expect(actionsMock.getProvider).toHaveBeenCalledWith('osf'); + expect(actionsMock.setDefaultFilterValue).toHaveBeenCalledWith('publisher', MOCK_PROVIDER.iri); + expect(actionsMock.setResourceType).toHaveBeenCalledWith(ResourceType.Registration); + expect(component.defaultSearchFiltersInitialized()).toBe(true); + }); - fixture.destroy(); + it('should not fetch provider when providerId is missing', () => { + setup({ params: {} }); + expect(actionsMock.getProvider).not.toHaveBeenCalled(); + expect(actionsMock.setDefaultFilterValue).not.toHaveBeenCalled(); + expect(actionsMock.setResourceType).not.toHaveBeenCalled(); + expect(component.defaultSearchFiltersInitialized()).toBe(false); + }); + + it('should clear providers on destroy in browser', () => { + component.ngOnDestroy(); expect(actionsMock.clearCurrentProvider).toHaveBeenCalled(); expect(actionsMock.clearRegistryProvider).toHaveBeenCalled(); }); + + it('should not clear providers on destroy on server', () => { + setup({ platformId: 'server' }); + component.ngOnDestroy(); + expect(actionsMock.clearCurrentProvider).not.toHaveBeenCalled(); + expect(actionsMock.clearRegistryProvider).not.toHaveBeenCalled(); + }); }); diff --git a/src/app/features/registries/pages/revisions-custom-step/revisions-custom-step.component.ts b/src/app/features/registries/pages/revisions-custom-step/revisions-custom-step.component.ts index e59a55ef7..73b1a1c83 100644 --- a/src/app/features/registries/pages/revisions-custom-step/revisions-custom-step.component.ts +++ b/src/app/features/registries/pages/revisions-custom-step/revisions-custom-step.component.ts @@ -14,31 +14,18 @@ import { RegistriesSelectors, UpdateSchemaResponse } from '../../store'; changeDetection: ChangeDetectionStrategy.OnPush, }) export class RevisionsCustomStepComponent { + private readonly route = inject(ActivatedRoute); + private readonly router = inject(Router); + readonly schemaResponse = select(RegistriesSelectors.getSchemaResponse); readonly schemaResponseRevisionData = select(RegistriesSelectors.getSchemaResponseRevisionData); - private readonly route = inject(ActivatedRoute); - private readonly router = inject(Router); - actions = createDispatchMap({ - updateRevision: UpdateSchemaResponse, - }); - - filesLink = computed(() => { - return this.schemaResponse()?.filesLink || ' '; - }); - - provider = computed(() => { - return this.schemaResponse()?.registrationId || ''; - }); - - projectId = computed(() => { - return this.schemaResponse()?.registrationId || ''; - }); - - stepsData = computed(() => { - const schemaResponse = this.schemaResponse(); - return schemaResponse?.revisionResponses || {}; - }); + actions = createDispatchMap({ updateRevision: UpdateSchemaResponse }); + + filesLink = computed(() => this.schemaResponse()?.filesLink || ' '); + provider = computed(() => this.schemaResponse()?.registrationId || ''); + projectId = computed(() => this.schemaResponse()?.registrationId || ''); + stepsData = computed(() => this.schemaResponse()?.revisionResponses || {}); onUpdateAction(data: Record): void { const id: string = this.route.snapshot.params['id'] || ''; diff --git a/src/testing/mocks/registries.mock.ts b/src/testing/mocks/registries.mock.ts index fb5debaa8..bd3a5a998 100644 --- a/src/testing/mocks/registries.mock.ts +++ b/src/testing/mocks/registries.mock.ts @@ -1,25 +1,45 @@ import { FieldType } from '@osf/shared/enums/field-type.enum'; +import { UserPermissions } from '@osf/shared/enums/user-permissions.enum'; +import { DraftRegistrationModel } from '@osf/shared/models/registration/draft-registration.model'; +import { PageSchema } from '@osf/shared/models/registration/page-schema.model'; +import { ProviderSchema } from '@osf/shared/models/registration/provider-schema.model'; -export const MOCK_REGISTRIES_PAGE = { +export const MOCK_REGISTRIES_PAGE: PageSchema = { id: 'page-1', title: 'Page 1', questions: [ - { responseKey: 'field1', fieldType: FieldType.Text, required: true }, - { responseKey: 'field2', fieldType: FieldType.Text, required: false }, + { id: 'q1', displayText: 'Field 1', responseKey: 'field1', fieldType: FieldType.Text, required: true }, + { id: 'q2', displayText: 'Field 2', responseKey: 'field2', fieldType: FieldType.Text, required: false }, ], -} as any; +}; -export const MOCK_STEPS_DATA = { field1: 'value1', field2: 'value2' } as any; +export const MOCK_REGISTRIES_PAGE_WITH_SECTIONS: PageSchema = { + id: 'page-2', + title: 'Page 2', + questions: [], + sections: [ + { + id: 'sec-1', + title: 'Section 1', + questions: [ + { id: 'q3', displayText: 'Field 3', responseKey: 'field3', fieldType: FieldType.Text, required: true }, + ], + }, + ], +}; + +export const MOCK_STEPS_DATA: Record = { field1: 'value1', field2: 'value2' }; -export const MOCK_PAGES_SCHEMA = [MOCK_REGISTRIES_PAGE]; +export const MOCK_PAGES_SCHEMA: PageSchema[] = [MOCK_REGISTRIES_PAGE]; -export const MOCK_DRAFT_REGISTRATION = { +export const MOCK_DRAFT_REGISTRATION: Partial = { id: 'draft-1', title: ' My Title ', description: ' Description ', - license: { id: 'mit' }, + license: { id: 'mit', options: null }, providerId: 'osf', - currentUserPermissions: ['admin'], -} as any; + currentUserPermissions: [UserPermissions.Admin], + registrationSchemaId: 'schema-1', +}; -export const MOCK_PROVIDER_SCHEMAS = [{ id: 'schema-1' }] as any; +export const MOCK_PROVIDER_SCHEMAS: ProviderSchema[] = [{ id: 'schema-1', name: 'Schema 1' }];