|
1 | | -import { MockComponent } from 'ng-mocks'; |
| 1 | +import { Store } from '@ngxs/store'; |
| 2 | + |
| 3 | +import { TranslatePipe, TranslateService } from '@ngx-translate/core'; |
| 4 | +import { MockComponent, MockModule, MockPipe } from 'ng-mocks'; |
| 5 | + |
| 6 | +import { ButtonModule } from 'primeng/button'; |
| 7 | +import { DialogService } from 'primeng/dynamicdialog'; |
| 8 | +import { Message } from 'primeng/message'; |
| 9 | +import { TagModule } from 'primeng/tag'; |
| 10 | + |
| 11 | +import { EMPTY, of } from 'rxjs'; |
2 | 12 |
|
3 | 13 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 14 | +import { FormsModule } from '@angular/forms'; |
| 15 | +import { ActivatedRoute, Router } from '@angular/router'; |
| 16 | +import { RouterTestingModule } from '@angular/router/testing'; |
4 | 17 |
|
5 | | -import { SubHeaderComponent } from '@osf/shared/components'; |
| 18 | +import { ProjectOverviewSelectors } from '@osf/features/project/overview/store'; |
| 19 | +import { LoadingSpinnerComponent, ResourceMetadataComponent, SubHeaderComponent } from '@shared/components'; |
| 20 | +import { IS_XSMALL } from '@shared/helpers'; |
| 21 | +import { ToastService } from '@shared/services'; |
| 22 | +import { DataciteService } from '@shared/services/datacite/datacite.service'; |
6 | 23 |
|
| 24 | +import { |
| 25 | + LinkedResourcesComponent, |
| 26 | + OverviewComponentsComponent, |
| 27 | + OverviewToolbarComponent, |
| 28 | + OverviewWikiComponent, |
| 29 | + RecentActivityComponent, |
| 30 | +} from './components'; |
7 | 31 | import { ProjectOverviewComponent } from './project-overview.component'; |
8 | 32 |
|
| 33 | +// Mocking the child components used in the template |
| 34 | +const MockedSubHeaderComponent = MockComponent(SubHeaderComponent); |
| 35 | +const MockedLoadingSpinnerComponent = MockComponent(LoadingSpinnerComponent); |
| 36 | +const MockedOverviewWikiComponent = MockComponent(OverviewWikiComponent); |
| 37 | +const MockedOverviewComponentsComponent = MockComponent(OverviewComponentsComponent); |
| 38 | +const MockedLinkedResourcesComponent = MockComponent(LinkedResourcesComponent); |
| 39 | +const MockedRecentActivityComponent = MockComponent(RecentActivityComponent); |
| 40 | +const MockedOverviewToolbarComponent = MockComponent(OverviewToolbarComponent); |
| 41 | +const MockedResourceMetadataComponent = MockComponent(ResourceMetadataComponent); |
| 42 | +const MockedMessageComponent = MockComponent(Message); |
| 43 | + |
9 | 44 | describe('ProjectOverviewComponent', () => { |
10 | 45 | let component: ProjectOverviewComponent; |
11 | 46 | let fixture: ComponentFixture<ProjectOverviewComponent>; |
| 47 | + let store: Store; |
| 48 | + let dataciteService: jest.Mocked<DataciteService>; |
12 | 49 |
|
13 | 50 | beforeEach(async () => { |
| 51 | + // Corrected mock for DataciteService to include all used methods |
| 52 | + dataciteService = { |
| 53 | + watchIdentifiable: jest.fn().mockReturnValue(of(void 0)), |
| 54 | + } as unknown as jest.Mocked<DataciteService>; |
| 55 | + |
14 | 56 | await TestBed.configureTestingModule({ |
15 | | - imports: [ProjectOverviewComponent, MockComponent(SubHeaderComponent)], |
| 57 | + // Import the component-under-test and mocks for all its template dependencies |
| 58 | + imports: [ |
| 59 | + ProjectOverviewComponent, |
| 60 | + MockedSubHeaderComponent, |
| 61 | + MockedLoadingSpinnerComponent, |
| 62 | + MockedOverviewWikiComponent, |
| 63 | + MockedOverviewComponentsComponent, |
| 64 | + MockedLinkedResourcesComponent, |
| 65 | + MockedRecentActivityComponent, |
| 66 | + MockedOverviewToolbarComponent, |
| 67 | + MockedResourceMetadataComponent, |
| 68 | + MockedMessageComponent, |
| 69 | + MockPipe(TranslatePipe, (key: string) => key), |
| 70 | + MockModule(ButtonModule), |
| 71 | + MockModule(TagModule), |
| 72 | + MockModule(FormsModule), |
| 73 | + RouterTestingModule, |
| 74 | + ], |
| 75 | + providers: [ |
| 76 | + { |
| 77 | + provide: Store, |
| 78 | + useValue: { |
| 79 | + select: jest.fn(() => EMPTY), |
| 80 | + dispatch: jest.fn(() => of({})), |
| 81 | + }, |
| 82 | + }, |
| 83 | + { provide: DataciteService, useValue: dataciteService }, |
| 84 | + { |
| 85 | + provide: ActivatedRoute, |
| 86 | + useValue: { snapshot: { params: { id: 'test-id' }, queryParams: {} }, parent: null }, |
| 87 | + }, |
| 88 | + { provide: Router, useValue: { navigate: jest.fn(), url: '' } }, |
| 89 | + { provide: ToastService, useValue: { showSuccess: jest.fn() } }, |
| 90 | + { provide: DialogService, useValue: { open: jest.fn().mockReturnValue({ onClose: of(null) }) } }, |
| 91 | + { provide: TranslateService, useValue: { instant: (key: string) => key } }, |
| 92 | + { provide: IS_XSMALL, useValue: of(false) }, |
| 93 | + ], |
16 | 94 | }).compileComponents(); |
17 | 95 |
|
18 | 96 | fixture = TestBed.createComponent(ProjectOverviewComponent); |
19 | 97 | component = fixture.componentInstance; |
20 | | - fixture.detectChanges(); |
| 98 | + store = TestBed.inject(Store); |
21 | 99 | }); |
22 | 100 |
|
23 | 101 | it('should create', () => { |
| 102 | + // Setup the store selector for the project |
| 103 | + jest.spyOn(store, 'select').mockImplementation((selector: any) => { |
| 104 | + if (selector === ProjectOverviewSelectors.getProject) { |
| 105 | + return of({ id: 'test-id', identifiers: [{ category: 'doi', value: '123' }] }); |
| 106 | + } |
| 107 | + return EMPTY; |
| 108 | + }); |
| 109 | + |
| 110 | + fixture.detectChanges(); |
| 111 | + |
| 112 | + // Verify that the component is created successfully |
24 | 113 | expect(component).toBeTruthy(); |
| 114 | + // Verify that the method on the mocked service was called |
| 115 | + expect(dataciteService.watchIdentifiable).toHaveBeenCalled(); |
25 | 116 | }); |
26 | 117 | }); |
0 commit comments