|
| 1 | +import { MockComponent } from 'ng-mocks'; |
| 2 | + |
| 3 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 4 | + |
| 5 | +import { FileKind } from '@osf/shared/enums/file-kind.enum'; |
| 6 | +import { FileMenuType } from '@osf/shared/enums/file-menu-type.enum'; |
| 7 | +import { FileModel } from '@shared/models/files/file.model'; |
| 8 | +import { FileMenuFlags } from '@shared/models/files/file-menu-action.model'; |
| 9 | + |
| 10 | +import { provideOSFCore } from '@testing/osf.testing.provider'; |
| 11 | + |
| 12 | +import { FileMenuComponent } from '../file-menu/file-menu.component'; |
| 13 | + |
| 14 | +import { FilesTreeRowComponent } from './files-tree-row.component'; |
| 15 | + |
| 16 | +describe('FilesTreeRowComponent', () => { |
| 17 | + let component: FilesTreeRowComponent; |
| 18 | + let fixture: ComponentFixture<FilesTreeRowComponent>; |
| 19 | + |
| 20 | + const ALL_MENU_ACTIONS: FileMenuFlags = { |
| 21 | + [FileMenuType.Download]: true, |
| 22 | + [FileMenuType.Copy]: true, |
| 23 | + [FileMenuType.Move]: true, |
| 24 | + [FileMenuType.Delete]: true, |
| 25 | + [FileMenuType.Rename]: true, |
| 26 | + [FileMenuType.Share]: true, |
| 27 | + [FileMenuType.Embed]: true, |
| 28 | + }; |
| 29 | + |
| 30 | + function createTestFile(overrides: Partial<FileModel> = {}): FileModel { |
| 31 | + return { |
| 32 | + id: 'f1', |
| 33 | + guid: 'g1', |
| 34 | + name: 'test.pdf', |
| 35 | + kind: FileKind.File, |
| 36 | + path: '/test.pdf', |
| 37 | + size: 2048, |
| 38 | + materializedPath: '/test.pdf', |
| 39 | + dateModified: '2024-06-01T12:00:00.000Z', |
| 40 | + extra: { |
| 41 | + hashes: { md5: 'm', sha256: 's' }, |
| 42 | + downloads: 5, |
| 43 | + }, |
| 44 | + links: { |
| 45 | + info: 'i', |
| 46 | + move: 'm', |
| 47 | + upload: 'u', |
| 48 | + delete: 'd', |
| 49 | + download: 'dl', |
| 50 | + render: 'r', |
| 51 | + html: 'h', |
| 52 | + self: 's', |
| 53 | + }, |
| 54 | + filesLink: null, |
| 55 | + previousFolder: false, |
| 56 | + provider: 'osfstorage', |
| 57 | + ...overrides, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + beforeEach(() => { |
| 62 | + TestBed.configureTestingModule({ |
| 63 | + imports: [FilesTreeRowComponent, MockComponent(FileMenuComponent)], |
| 64 | + providers: [provideOSFCore()], |
| 65 | + }); |
| 66 | + |
| 67 | + fixture = TestBed.createComponent(FilesTreeRowComponent); |
| 68 | + component = fixture.componentInstance; |
| 69 | + fixture.componentRef.setInput('file', createTestFile()); |
| 70 | + fixture.componentRef.setInput('allowedMenuActions', ALL_MENU_ACTIONS); |
| 71 | + fixture.detectChanges(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should create', () => { |
| 75 | + expect(component).toBeTruthy(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should set isFolder when kind is folder', () => { |
| 79 | + fixture.componentRef.setInput('file', createTestFile({ kind: FileKind.Folder })); |
| 80 | + fixture.detectChanges(); |
| 81 | + |
| 82 | + expect(component.isFolder()).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should set isFolder false when kind is file', () => { |
| 86 | + fixture.componentRef.setInput('file', createTestFile({ kind: FileKind.File })); |
| 87 | + fixture.detectChanges(); |
| 88 | + |
| 89 | + expect(component.isFolder()).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should clear downloadsCount for folder', () => { |
| 93 | + fixture.componentRef.setInput( |
| 94 | + 'file', |
| 95 | + createTestFile({ |
| 96 | + kind: FileKind.Folder, |
| 97 | + extra: { hashes: { md5: 'm', sha256: 's' }, downloads: 99 }, |
| 98 | + }) |
| 99 | + ); |
| 100 | + fixture.detectChanges(); |
| 101 | + |
| 102 | + expect(component.downloadsCount()).toBe(''); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should expose downloadsCount for file with downloads', () => { |
| 106 | + fixture.componentRef.setInput( |
| 107 | + 'file', |
| 108 | + createTestFile({ |
| 109 | + kind: FileKind.File, |
| 110 | + extra: { hashes: { md5: 'm', sha256: 's' }, downloads: 12 }, |
| 111 | + }) |
| 112 | + ); |
| 113 | + fixture.detectChanges(); |
| 114 | + |
| 115 | + expect(component.downloadsCount()).toBe(12); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should clear downloadsCount when downloads is zero', () => { |
| 119 | + fixture.componentRef.setInput( |
| 120 | + 'file', |
| 121 | + createTestFile({ |
| 122 | + kind: FileKind.File, |
| 123 | + extra: { hashes: { md5: 'm', sha256: 's' }, downloads: 0 }, |
| 124 | + }) |
| 125 | + ); |
| 126 | + fixture.detectChanges(); |
| 127 | + |
| 128 | + expect(component.downloadsCount()).toBe(''); |
| 129 | + }); |
| 130 | +}); |
0 commit comments