|
| 1 | +import { Store } from '@ngxs/store'; |
| 2 | + |
1 | 3 | import { MockComponents } from 'ng-mocks'; |
2 | 4 |
|
| 5 | +import { TablePageEvent } from 'primeng/table'; |
| 6 | + |
3 | 7 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
4 | 8 |
|
5 | 9 | import { SearchInputComponent } from '@osf/shared/components/search-input/search-input.component'; |
| 10 | +import { ResourceSearchMode } from '@osf/shared/enums/resource-search-mode.enum'; |
| 11 | +import { ResourceType } from '@osf/shared/enums/resource-type.enum'; |
| 12 | +import { MyResourcesSelectors } from '@osf/shared/stores/my-resources'; |
| 13 | +import { NodeLinksSelectors } from '@osf/shared/stores/node-links'; |
| 14 | +import { MyResourcesItem } from '@shared/models/my-resources/my-resources.models'; |
| 15 | + |
| 16 | +import { ProjectOverviewSelectors } from '../../store'; |
6 | 17 |
|
7 | 18 | import { LinkResourceDialogComponent } from './link-resource-dialog.component'; |
8 | 19 |
|
9 | | -describe.skip('LinkProjectDialogComponent', () => { |
| 20 | +import { DynamicDialogRefMock } from '@testing/mocks/dynamic-dialog-ref.mock'; |
| 21 | +import { |
| 22 | + MOCK_MY_RESOURCES_ITEM_PROJECT, |
| 23 | + MOCK_MY_RESOURCES_ITEM_PROJECT_PRIVATE, |
| 24 | + MOCK_MY_RESOURCES_ITEM_REGISTRATION, |
| 25 | +} from '@testing/mocks/my-resources.mock'; |
| 26 | +import { MOCK_NODE_WITH_ADMIN } from '@testing/mocks/node.mock'; |
| 27 | +import { OSFTestingModule } from '@testing/osf.testing.module'; |
| 28 | +import { provideMockStore } from '@testing/providers/store-provider.mock'; |
| 29 | + |
| 30 | +describe('LinkResourceDialogComponent', () => { |
10 | 31 | let component: LinkResourceDialogComponent; |
11 | 32 | let fixture: ComponentFixture<LinkResourceDialogComponent>; |
| 33 | + let store: Store; |
| 34 | + let dialogRef: { close: jest.Mock }; |
| 35 | + |
| 36 | + const mockProjects: MyResourcesItem[] = [MOCK_MY_RESOURCES_ITEM_PROJECT, MOCK_MY_RESOURCES_ITEM_PROJECT_PRIVATE]; |
| 37 | + |
| 38 | + const mockRegistrations: MyResourcesItem[] = [MOCK_MY_RESOURCES_ITEM_REGISTRATION]; |
| 39 | + |
| 40 | + const mockLinkedResources = [{ ...MOCK_NODE_WITH_ADMIN, id: 'project-1', title: 'Linked Project 1' }]; |
| 41 | + |
| 42 | + const mockCurrentProject = { ...MOCK_NODE_WITH_ADMIN, id: 'current-project-id' }; |
12 | 43 |
|
13 | 44 | beforeEach(async () => { |
| 45 | + dialogRef = { close: jest.fn() }; |
| 46 | + |
14 | 47 | await TestBed.configureTestingModule({ |
15 | | - imports: [LinkResourceDialogComponent, ...MockComponents(SearchInputComponent)], |
| 48 | + imports: [LinkResourceDialogComponent, OSFTestingModule, ...MockComponents(SearchInputComponent)], |
| 49 | + providers: [ |
| 50 | + provideMockStore({ |
| 51 | + signals: [ |
| 52 | + { selector: MyResourcesSelectors.getProjects, value: mockProjects }, |
| 53 | + { selector: MyResourcesSelectors.getProjectsLoading, value: false }, |
| 54 | + { selector: MyResourcesSelectors.getRegistrations, value: mockRegistrations }, |
| 55 | + { selector: MyResourcesSelectors.getRegistrationsLoading, value: false }, |
| 56 | + { selector: MyResourcesSelectors.getTotalProjects, value: 2 }, |
| 57 | + { selector: MyResourcesSelectors.getTotalRegistrations, value: 1 }, |
| 58 | + { selector: NodeLinksSelectors.getNodeLinksSubmitting, value: false }, |
| 59 | + { selector: NodeLinksSelectors.getLinkedResources, value: mockLinkedResources }, |
| 60 | + { selector: ProjectOverviewSelectors.getProject, value: mockCurrentProject }, |
| 61 | + ], |
| 62 | + }), |
| 63 | + { provide: DynamicDialogRefMock.provide, useValue: dialogRef }, |
| 64 | + ], |
16 | 65 | }).compileComponents(); |
17 | 66 |
|
18 | 67 | fixture = TestBed.createComponent(LinkResourceDialogComponent); |
19 | 68 | component = fixture.componentInstance; |
| 69 | + store = TestBed.inject(Store); |
| 70 | + fixture.detectChanges(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should initialize with default values', () => { |
| 74 | + expect(component.currentPage()).toBe(1); |
| 75 | + expect(component.searchMode()).toBe(ResourceSearchMode.User); |
| 76 | + expect(component.resourceType()).toBe(ResourceType.Project); |
| 77 | + expect(component.searchControl.value).toBe(''); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should initialize skeleton data with correct length', () => { |
| 81 | + expect(component.skeletonData.length).toBe(component.tableRows); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should compute currentResourceId from currentProject', () => { |
| 85 | + expect(component.currentResourceId()).toBe('current-project-id'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should compute isCurrentTableLoading for registrations', () => { |
| 89 | + component.resourceType.set(ResourceType.Registration); |
20 | 90 | fixture.detectChanges(); |
| 91 | + expect(component.isCurrentTableLoading()).toBe(false); |
21 | 92 | }); |
22 | 93 |
|
23 | | - it('should create', () => { |
24 | | - expect(component).toBeTruthy(); |
| 94 | + it('should compute currentTotalCount for registrations', () => { |
| 95 | + component.resourceType.set(ResourceType.Registration); |
| 96 | + fixture.detectChanges(); |
| 97 | + expect(component.currentTotalCount()).toBe(1); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should compute tableParams correctly', () => { |
| 101 | + const params = component.tableParams(); |
| 102 | + expect(params.rows).toBe(component.tableRows); |
| 103 | + expect(params.firstRowIndex).toBe(0); |
| 104 | + expect(params.paginator).toBe(false); |
| 105 | + expect(params.totalRecords).toBe(2); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should compute linkedResourceIds as a Set', () => { |
| 109 | + const linkedIds = component.linkedResourceIds(); |
| 110 | + expect(linkedIds).toBeInstanceOf(Set); |
| 111 | + expect(linkedIds.has('project-1')).toBe(true); |
| 112 | + expect(linkedIds.has('project-2')).toBe(false); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should update searchMode and reset to first page', () => { |
| 116 | + component.currentPage.set(2); |
| 117 | + component.onSearchModeChange(ResourceSearchMode.All); |
| 118 | + |
| 119 | + expect(component.searchMode()).toBe(ResourceSearchMode.All); |
| 120 | + expect(component.currentPage()).toBe(1); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should update resourceType and reset to first page', () => { |
| 124 | + component.currentPage.set(2); |
| 125 | + component.onObjectTypeChange(ResourceType.Registration); |
| 126 | + |
| 127 | + expect(component.resourceType()).toBe(ResourceType.Registration); |
| 128 | + expect(component.currentPage()).toBe(1); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should update currentPage and trigger search', () => { |
| 132 | + const dispatchSpy = jest.spyOn(store, 'dispatch'); |
| 133 | + const event: TablePageEvent = { |
| 134 | + first: 6, |
| 135 | + rows: 6, |
| 136 | + }; |
| 137 | + |
| 138 | + component.onPageChange(event); |
| 139 | + |
| 140 | + expect(component.currentPage()).toBe(2); |
| 141 | + expect(dispatchSpy).toHaveBeenCalled(); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should return true for linked items', () => { |
| 145 | + expect(component.isItemLinked('project-1')).toBe(true); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should return false for non-linked items', () => { |
| 149 | + expect(component.isItemLinked('project-2')).toBe(false); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should close the dialog', () => { |
| 153 | + component.handleCloseDialog(); |
| 154 | + expect(dialogRef.close).toHaveBeenCalled(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should trigger search when searchMode changes', () => { |
| 158 | + const dispatchSpy = jest.spyOn(store, 'dispatch'); |
| 159 | + |
| 160 | + component.onSearchModeChange(ResourceSearchMode.All); |
| 161 | + |
| 162 | + expect(dispatchSpy).toHaveBeenCalled(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should trigger search when resourceType changes', () => { |
| 166 | + const dispatchSpy = jest.spyOn(store, 'dispatch'); |
| 167 | + |
| 168 | + component.onObjectTypeChange(ResourceType.Registration); |
| 169 | + |
| 170 | + expect(dispatchSpy).toHaveBeenCalled(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should handle page change with zero first value', () => { |
| 174 | + const dispatchSpy = jest.spyOn(store, 'dispatch'); |
| 175 | + const event: TablePageEvent = { |
| 176 | + first: 0, |
| 177 | + rows: 6, |
| 178 | + }; |
| 179 | + |
| 180 | + component.onPageChange(event); |
| 181 | + |
| 182 | + expect(component.currentPage()).toBe(1); |
| 183 | + expect(dispatchSpy).toHaveBeenCalled(); |
25 | 184 | }); |
26 | 185 | }); |
0 commit comments