|
1 |
| -import { MockComponent } from 'ng-mocks'; |
| 1 | +import { Store } from '@ngxs/store'; |
2 | 2 |
|
| 3 | +import { TranslatePipe } from '@ngx-translate/core'; |
| 4 | +import { MockProvider } from 'ng-mocks'; |
| 5 | + |
| 6 | +import { Button } from 'primeng/button'; |
| 7 | +import { Dialog } from 'primeng/dialog'; |
| 8 | +import { DialogService } from 'primeng/dynamicdialog'; |
| 9 | +import { TableModule } from 'primeng/table'; |
| 10 | + |
| 11 | +import { signal } from '@angular/core'; |
3 | 12 | import { ComponentFixture, TestBed } from '@angular/core/testing';
|
| 13 | +import { FormsModule, ReactiveFormsModule } from '@angular/forms'; |
| 14 | +import { ActivatedRoute } from '@angular/router'; |
4 | 15 |
|
5 |
| -import { SubHeaderComponent } from '@osf/shared/components'; |
| 16 | +import { |
| 17 | + FilesTreeComponent, |
| 18 | + FormSelectComponent, |
| 19 | + LoadingSpinnerComponent, |
| 20 | + SearchInputComponent, |
| 21 | + SubHeaderComponent, |
| 22 | + ViewOnlyLinkMessageComponent, |
| 23 | +} from '@osf/shared/components'; |
| 24 | +import { GoogleFilePickerComponent } from '@osf/shared/components/addons/folder-selector/google-file-picker/google-file-picker.component'; |
| 25 | +import { OsfFile } from '@osf/shared/models'; |
| 26 | +import { CustomConfirmationService, FilesService } from '@osf/shared/services'; |
| 27 | + |
| 28 | +import { FilesSelectors } from '../../store'; |
6 | 29 |
|
7 | 30 | import { FilesComponent } from './files.component';
|
8 | 31 |
|
9 |
| -describe('FilesComponent', () => { |
| 32 | +import { getConfiguredAddonsMappedData } from '@testing/data/addons/addons.configured.data'; |
| 33 | +import { getNodeFilesMappedData } from '@testing/data/files/node.data'; |
| 34 | +import { OSFTestingModule } from '@testing/osf.testing.module'; |
| 35 | +import { MockComponentWithSignal } from '@testing/providers/component-provider.mock'; |
| 36 | +import { provideMockStore } from '@testing/providers/store-provider.mock'; |
| 37 | + |
| 38 | +describe('Component: Files', () => { |
10 | 39 | let component: FilesComponent;
|
11 | 40 | let fixture: ComponentFixture<FilesComponent>;
|
| 41 | + const currentFolderSignal = signal(getNodeFilesMappedData(0)); |
12 | 42 |
|
13 | 43 | beforeEach(async () => {
|
| 44 | + jest.clearAllMocks(); |
14 | 45 | await TestBed.configureTestingModule({
|
15 |
| - imports: [FilesComponent, MockComponent(SubHeaderComponent)], |
16 |
| - }).compileComponents(); |
| 46 | + imports: [ |
| 47 | + OSFTestingModule, |
| 48 | + FilesComponent, |
| 49 | + Button, |
| 50 | + Dialog, |
| 51 | + FormSelectComponent, |
| 52 | + FormsModule, |
| 53 | + GoogleFilePickerComponent, |
| 54 | + LoadingSpinnerComponent, |
| 55 | + ReactiveFormsModule, |
| 56 | + SearchInputComponent, |
| 57 | + SubHeaderComponent, |
| 58 | + TableModule, |
| 59 | + TranslatePipe, |
| 60 | + ViewOnlyLinkMessageComponent, |
| 61 | + ], |
| 62 | + providers: [ |
| 63 | + FilesService, |
| 64 | + MockProvider(ActivatedRoute), |
| 65 | + MockProvider(CustomConfirmationService), |
| 66 | + |
| 67 | + DialogService, |
| 68 | + provideMockStore({ |
| 69 | + signals: [ |
| 70 | + { |
| 71 | + selector: FilesSelectors.getRootFolders, |
| 72 | + value: getNodeFilesMappedData(), |
| 73 | + }, |
| 74 | + { |
| 75 | + selector: FilesSelectors.getCurrentFolder, |
| 76 | + value: currentFolderSignal(), |
| 77 | + }, |
| 78 | + { |
| 79 | + selector: FilesSelectors.getConfiguredStorageAddons, |
| 80 | + value: getConfiguredAddonsMappedData(), |
| 81 | + }, |
| 82 | + ], |
| 83 | + }), |
| 84 | + ], |
| 85 | + }) |
| 86 | + .overrideComponent(FilesComponent, { |
| 87 | + remove: { |
| 88 | + imports: [FilesTreeComponent], |
| 89 | + }, |
| 90 | + add: { |
| 91 | + imports: [ |
| 92 | + MockComponentWithSignal('osf-files-tree', [ |
| 93 | + 'files', |
| 94 | + 'currentFolder', |
| 95 | + 'isLoading', |
| 96 | + 'actions', |
| 97 | + 'viewOnly', |
| 98 | + 'viewOnlyDownloadable', |
| 99 | + 'resourceId', |
| 100 | + 'provider', |
| 101 | + ]), |
| 102 | + ], |
| 103 | + }, |
| 104 | + }) |
| 105 | + .compileComponents(); |
17 | 106 |
|
18 | 107 | fixture = TestBed.createComponent(FilesComponent);
|
19 | 108 | component = fixture.componentInstance;
|
20 | 109 | fixture.detectChanges();
|
21 | 110 | });
|
22 | 111 |
|
23 |
| - it('should create', () => { |
24 |
| - expect(component).toBeTruthy(); |
| 112 | + describe('CurrentRootFolder effect', () => { |
| 113 | + it('should handle the initial effects', () => { |
| 114 | + expect(component.currentRootFolder()?.folder.name).toBe('osfstorage'); |
| 115 | + expect(component.isGoogleDrive()).toBeFalsy(); |
| 116 | + expect(component.accountId()).toBeFalsy(); |
| 117 | + expect(component.selectedRootFolder()).toEqual(Object({})); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should handle changing the folder to googledrive', () => { |
| 121 | + component.currentRootFolder.set( |
| 122 | + Object({ |
| 123 | + label: 'label', |
| 124 | + folder: Object({ |
| 125 | + name: 'Google Drive', |
| 126 | + provider: 'googledrive', |
| 127 | + }), |
| 128 | + }) |
| 129 | + ); |
| 130 | + |
| 131 | + fixture.detectChanges(); |
| 132 | + |
| 133 | + expect(component.currentRootFolder()?.folder.name).toBe('Google Drive'); |
| 134 | + expect(component.isGoogleDrive()).toBeTruthy(); |
| 135 | + expect(component.accountId()).toBe('62ed6dd7-f7b7-4003-b7b4-855789c1f991'); |
| 136 | + expect(component.selectedRootFolder()).toEqual( |
| 137 | + Object({ |
| 138 | + itemId: '0AIl0aR4C9JAFUk9PVA', |
| 139 | + }) |
| 140 | + ); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + describe('updateFilesList', () => { |
| 145 | + it('should handle the updateFilesList with a filesLink', () => { |
| 146 | + let results!: string; |
| 147 | + const store = TestBed.inject(Store); |
| 148 | + const dispatchSpy = jest.spyOn(store, 'dispatch'); |
| 149 | + dispatchSpy.mockClear(); |
| 150 | + jest.spyOn(component.filesTreeActions, 'setFilesIsLoading'); |
| 151 | + component.updateFilesList().subscribe({ |
| 152 | + next: (result) => { |
| 153 | + results = result as any; |
| 154 | + }, |
| 155 | + }); |
| 156 | + |
| 157 | + expect(results).toBeTruthy(); |
| 158 | + |
| 159 | + expect(component.filesTreeActions.setFilesIsLoading).toHaveBeenCalledWith(true); |
| 160 | + expect(dispatchSpy).toHaveBeenCalledWith({ |
| 161 | + filesLink: 'https://api.staging4.osf.io/v2/nodes/xgrm4/files/osfstorage/', |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should handle the updateFilesList without a filesLink', () => { |
| 166 | + let results!: string; |
| 167 | + const currentFolder = currentFolderSignal() as OsfFile; |
| 168 | + currentFolder.relationships.filesLink = ''; |
| 169 | + currentFolderSignal.set(currentFolder); |
| 170 | + const store = TestBed.inject(Store); |
| 171 | + const dispatchSpy = jest.spyOn(store, 'dispatch'); |
| 172 | + dispatchSpy.mockClear(); |
| 173 | + jest.spyOn(component.filesTreeActions, 'setFilesIsLoading'); |
| 174 | + component.updateFilesList().subscribe({ |
| 175 | + next: (result) => { |
| 176 | + results = result as any; |
| 177 | + }, |
| 178 | + }); |
| 179 | + |
| 180 | + expect(results).toBeUndefined(); |
| 181 | + |
| 182 | + expect(component.filesTreeActions.setFilesIsLoading).not.toHaveBeenCalled(); |
| 183 | + expect(dispatchSpy).not.toHaveBeenCalled(); |
| 184 | + }); |
25 | 185 | });
|
26 | 186 | });
|
0 commit comments