|
1 | | -import { MockComponent } from 'ng-mocks'; |
| 1 | +import { Store } from '@ngxs/store'; |
2 | 2 |
|
| 3 | +import { TranslatePipe, TranslateService } from '@ngx-translate/core'; |
| 4 | +import { MockProvider } from 'ng-mocks'; |
| 5 | + |
| 6 | +import { ButtonGroupModule } from 'primeng/buttongroup'; |
| 7 | +import { DialogService } from 'primeng/dynamicdialog'; |
| 8 | +import { Message } from 'primeng/message'; |
| 9 | +import { TagModule } from 'primeng/tag'; |
| 10 | + |
| 11 | +import { of } from 'rxjs'; |
| 12 | + |
| 13 | +import { DestroyRef, signal } from '@angular/core'; |
3 | 14 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 15 | +import { FormsModule } from '@angular/forms'; |
| 16 | +import { ActivatedRoute, Router, RouterLink } from '@angular/router'; |
4 | 17 |
|
5 | | -import { SubHeaderComponent } from '@osf/shared/components'; |
| 18 | +import { CollectionSubmissionReviewAction } from '@osf/features/moderation/models'; |
| 19 | +import { CollectionsModerationSelectors } from '@osf/features/moderation/store/collections-moderation'; |
| 20 | +import { ProjectOverviewSelectors } from '@osf/features/project/overview/store'; |
| 21 | +import { |
| 22 | + LoadingSpinnerComponent, |
| 23 | + ResourceMetadataComponent, |
| 24 | + SubHeaderComponent, |
| 25 | + ViewOnlyLinkMessageComponent, |
| 26 | +} from '@osf/shared/components'; |
| 27 | +import { ToastService } from '@osf/shared/services'; |
| 28 | +import { MOCK_STORE } from '@shared/mocks'; |
| 29 | +import { Identifier } from '@shared/models'; |
| 30 | +import { DataciteService } from '@shared/services/datacite/datacite.service'; |
| 31 | +import { |
| 32 | + BookmarksSelectors, |
| 33 | + CitationsSelectors, |
| 34 | + CollectionsSelectors, |
| 35 | + MyResourcesSelectors, |
| 36 | + NodeLinksSelectors, |
| 37 | +} from '@shared/stores'; |
| 38 | +import { ActivityLogsSelectors } from '@shared/stores/activity-logs'; |
6 | 39 |
|
| 40 | +import { |
| 41 | + LinkedResourcesComponent, |
| 42 | + OverviewComponentsComponent, |
| 43 | + OverviewToolbarComponent, |
| 44 | + OverviewWikiComponent, |
| 45 | + RecentActivityComponent, |
| 46 | +} from './components'; |
7 | 47 | import { ProjectOverviewComponent } from './project-overview.component'; |
8 | 48 |
|
| 49 | +import { OSFTestingModule } from '@testing/osf.testing.module'; |
| 50 | + |
| 51 | +const sampleReviewAction: CollectionSubmissionReviewAction = { |
| 52 | + id: 'ra1', |
| 53 | + type: 'collection-submission-review-action', |
| 54 | + dateCreated: '2025-09-01T10:15:00Z', |
| 55 | + dateModified: '2025-09-01T10:30:00Z', |
| 56 | + fromState: 'pending', |
| 57 | + toState: 'accepted', |
| 58 | + comment: 'Submission approved by moderator', |
| 59 | + trigger: 'accept', |
| 60 | + targetId: 'sub123', |
| 61 | + targetNodeId: 'node456', |
| 62 | + createdBy: 'user789', |
| 63 | +}; |
| 64 | + |
9 | 65 | describe('ProjectOverviewComponent', () => { |
10 | | - let component: ProjectOverviewComponent; |
11 | 66 | let fixture: ComponentFixture<ProjectOverviewComponent>; |
| 67 | + let dataciteService: jest.Mocked<DataciteService>; |
| 68 | + const projectSignal = signal<any>(getProject()); |
| 69 | + |
| 70 | + const activatedRouteMock = { |
| 71 | + snapshot: { |
| 72 | + queryParams: { |
| 73 | + mode: 'moderation', // or whatever value you want to test |
| 74 | + }, |
| 75 | + params: { |
| 76 | + id: '1234', // value for this.route.snapshot.params['id'] |
| 77 | + }, |
| 78 | + parent: { |
| 79 | + snapshot: { |
| 80 | + params: { |
| 81 | + id: '5678', // fallback if top-level param is undefined |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }; |
12 | 87 |
|
13 | 88 | beforeEach(async () => { |
| 89 | + (MOCK_STORE.selectSignal as jest.Mock).mockImplementation((selector) => { |
| 90 | + switch (selector) { |
| 91 | + case ProjectOverviewSelectors.getProject: |
| 92 | + return projectSignal; |
| 93 | + case CollectionsModerationSelectors.getCurrentReviewAction: |
| 94 | + return signal(sampleReviewAction); |
| 95 | + case ProjectOverviewSelectors.getProjectLoading: |
| 96 | + case CollectionsSelectors.getCollectionProviderLoading: |
| 97 | + case CollectionsModerationSelectors.getCurrentReviewActionLoading: |
| 98 | + case ProjectOverviewSelectors.isProjectAnonymous: |
| 99 | + case MyResourcesSelectors.getBookmarksLoading: |
| 100 | + case BookmarksSelectors.getBookmarksCollectionIdSubmitting: |
| 101 | + case ProjectOverviewSelectors.getComponentsLoading: |
| 102 | + case NodeLinksSelectors.getLinkedResourcesLoading: |
| 103 | + case ActivityLogsSelectors.getActivityLogsLoading: |
| 104 | + return () => false; |
| 105 | + case ActivityLogsSelectors.getActivityLogsTotalCount: |
| 106 | + return () => 0; |
| 107 | + case BookmarksSelectors.getBookmarksCollectionId: |
| 108 | + return () => '123'; |
| 109 | + case MyResourcesSelectors.getBookmarks: |
| 110 | + case ActivityLogsSelectors.getActivityLogs: |
| 111 | + case CitationsSelectors.getCitationStyles: |
| 112 | + case ProjectOverviewSelectors.getComponents: |
| 113 | + case NodeLinksSelectors.getLinkedResources: |
| 114 | + return () => []; |
| 115 | + default: |
| 116 | + return () => ''; |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + dataciteService = { |
| 121 | + logView: jest.fn().mockReturnValue(of(void 0)), |
| 122 | + } as unknown as jest.Mocked<DataciteService>; |
| 123 | + |
14 | 124 | await TestBed.configureTestingModule({ |
15 | | - imports: [ProjectOverviewComponent, MockComponent(SubHeaderComponent)], |
| 125 | + imports: [ |
| 126 | + ProjectOverviewComponent, |
| 127 | + OSFTestingModule, |
| 128 | + ButtonGroupModule, |
| 129 | + TagModule, |
| 130 | + SubHeaderComponent, |
| 131 | + FormsModule, |
| 132 | + LoadingSpinnerComponent, |
| 133 | + OverviewWikiComponent, |
| 134 | + OverviewComponentsComponent, |
| 135 | + LinkedResourcesComponent, |
| 136 | + RecentActivityComponent, |
| 137 | + OverviewToolbarComponent, |
| 138 | + ResourceMetadataComponent, |
| 139 | + TranslatePipe, |
| 140 | + Message, |
| 141 | + RouterLink, |
| 142 | + ViewOnlyLinkMessageComponent, |
| 143 | + ], |
| 144 | + providers: [ |
| 145 | + { provide: ActivatedRoute, useValue: activatedRouteMock }, |
| 146 | + { provide: Store, useValue: MOCK_STORE }, |
| 147 | + { provide: DataciteService, useValue: dataciteService }, |
| 148 | + Router, |
| 149 | + DestroyRef, |
| 150 | + MockProvider(ToastService), |
| 151 | + DialogService, |
| 152 | + TranslateService, |
| 153 | + ], |
16 | 154 | }).compileComponents(); |
17 | 155 |
|
18 | 156 | fixture = TestBed.createComponent(ProjectOverviewComponent); |
19 | | - component = fixture.componentInstance; |
20 | 157 | fixture.detectChanges(); |
21 | 158 | }); |
22 | 159 |
|
23 | | - it('should create', () => { |
24 | | - expect(component).toBeTruthy(); |
| 160 | + it('reacts to sequence of state changes', () => { |
| 161 | + fixture.detectChanges(); |
| 162 | + expect(dataciteService.logView).toHaveBeenCalledTimes(0); |
| 163 | + |
| 164 | + projectSignal.set(getProject()); |
| 165 | + |
| 166 | + fixture.detectChanges(); |
| 167 | + expect(dataciteService.logView).toHaveBeenCalledTimes(0); |
| 168 | + |
| 169 | + projectSignal.set(getProject([{ category: 'dio', value: '123', id: '', type: 'identifier' }])); |
| 170 | + fixture.detectChanges(); |
| 171 | + expect(dataciteService.logView).toHaveBeenCalledTimes(0); |
| 172 | + |
| 173 | + projectSignal.set(getProject([{ category: 'doi', value: '123', id: '', type: 'identifier' }])); |
| 174 | + |
| 175 | + fixture.detectChanges(); |
| 176 | + expect(dataciteService.logView).toHaveBeenCalled(); |
| 177 | + |
| 178 | + projectSignal.set(getProject([{ category: 'doi', value: '456', id: '', type: 'identifier' }])); |
| 179 | + fixture.detectChanges(); |
| 180 | + expect(dataciteService.logView).toHaveBeenLastCalledWith('123'); |
25 | 181 | }); |
| 182 | + |
| 183 | + function getProject(identifiers?: Identifier[]) { |
| 184 | + return { |
| 185 | + identifiers: identifiers ?? [], |
| 186 | + }; |
| 187 | + } |
26 | 188 | }); |
0 commit comments