Skip to content

Commit

Permalink
Merge pull request #476 from PermanentOrg/PER-9881-the-location-picke…
Browse files Browse the repository at this point in the history
…r-never-closes

PER-9881: The location picker never closes
  • Loading branch information
crisnicandrei authored Oct 25, 2024
2 parents 6b2e6b1 + e307391 commit 8047b20
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
class="sidebar-item-content"
(click)="onLocationClick()"
tabindex="0"
(focus)="onLocationClick()"
(keydown)="onLocationEnterPress($event)"
[class.can-edit]="canEdit"
>
<pr-static-map [location]="selectedItem.LocnVO"></pr-static-map>
Expand Down
164 changes: 139 additions & 25 deletions src/app/file-browser/components/sidebar/sidebar.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,139 @@
// import { async, ComponentFixture, TestBed } from '@angular/core/testing';

// import { SidebarComponent } from './sidebar.component';

// describe('SidebarComponent', () => {
// let component: SidebarComponent;
// let fixture: ComponentFixture<SidebarComponent>;

// beforeEach(async(() => {
// TestBed.configureTestingModule({
// declarations: [ SidebarComponent ]
// })
// .compileComponents();
// }));

// beforeEach(() => {
// fixture = TestBed.createComponent(SidebarComponent);
// component = fixture.componentInstance;
// fixture.detectChanges();
// });

// it('should create', () => {
// expect(component).toBeTruthy();
// });
// });
/* @format */
import { Shallow } from 'shallow-render';
import { FileBrowserComponentsModule } from '@fileBrowser/file-browser-components.module';
import { DataService } from '@shared/services/data/data.service';
import { EditService } from '@core/services/edit/edit.service';
import { AccountService } from '@shared/services/account/account.service';
import { ArchiveVO, RecordVO } from '@models/index';
import { of } from 'rxjs';
import { RecordCastPipe } from '@shared/pipes/cast.pipe';
import { SidebarComponent } from './sidebar.component';

const mockDataService = {
selectedItems$: () =>
of(
new Set([
new RecordVO({
accessRole: 'access.role.owner',
}),
]),
),
fetchFullItems: (_) => {},
};

const mockEditService = {
openLocationDialog: (_) => {},
};

class MockAccountService {
getArchive() {
return new ArchiveVO({});
}
checkMinimumArchiveAccess() {
return true;
}
checkMinimumAccess() {
return true;
}
}

describe('SidebarComponent', () => {
let shallow: Shallow<SidebarComponent>;

beforeEach(() => {
shallow = new Shallow(SidebarComponent, FileBrowserComponentsModule)
.provideMock({
provide: DataService,
useValue: mockDataService,
})
.provideMock({
provide: EditService,
useValue: mockEditService,
})
.provideMock({
provide: AccountService,
useClass: MockAccountService,
})
.dontMock(RecordCastPipe);
});

it('should create', async () => {
const { instance } = await shallow.render();

expect(instance).toBeTruthy();
});

it('should open location dialog on Enter key press if editable', async () => {
const { instance } = await shallow.render();

const locationDialogSpy = spyOn(
mockEditService,
'openLocationDialog',
).and.callThrough();

instance.onLocationEnterPress(
new KeyboardEvent('keydown', { key: 'Enter' }),
);

expect(locationDialogSpy).toHaveBeenCalledWith(instance.selectedItem);
});

it('should set currentTab correctly when setCurrentTab is called', async () => {
const { instance, fixture } = await shallow.render();

instance.setCurrentTab('info');
fixture.detectChanges();

expect(instance.currentTab).toBe('info');

instance.isRootFolder = false;
instance.isPublicItem = false;
instance.setCurrentTab('sharing');
fixture.detectChanges();

expect(instance.currentTab).toBe('sharing');
});

it('should call editService.openLocationDialog when onLocationClick is called if editable', async () => {
const { instance, inject } = await shallow.render();
const editService = inject(EditService);
spyOn(editService, 'openLocationDialog');

instance.canEdit = true;
instance.selectedItem = new RecordVO({});

instance.onLocationClick();

expect(editService.openLocationDialog).toHaveBeenCalledWith(
instance.selectedItem,
);
});

it('should correctly update canEdit and canShare when checkPermissions is called', async () => {
const { instance } = await shallow.render();

instance.selectedItem = new RecordVO({
accessRole: 'access.role.editor',
});
instance.selectedItems = [instance.selectedItem];
instance.isRootFolder = false;
instance.isPublicItem = false;

instance.checkPermissions();

expect(instance.canEdit).toBe(true);
expect(instance.canShare).toBe(true);

instance.selectedItem = new RecordVO({
accessRole: 'access.role.viewer',
});
instance.selectedItems = [instance.selectedItem];
instance.isRootFolder = false;
instance.isPublicItem = false;

instance.checkPermissions();

expect(instance.canEdit).toBe(false);
expect(instance.canShare).toBe(true);
});
});
7 changes: 7 additions & 0 deletions src/app/file-browser/components/sidebar/sidebar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class SidebarComponent implements OnDestroy, HasSubscriptions {
}
if (
this.selectedItem instanceof RecordVO &&
this.selectedItem.FileVOs &&
this.selectedItem.FileVOs[0]
) {
this.originalFileExtension = this.selectedItem.FileVOs.find(
Expand Down Expand Up @@ -168,6 +169,12 @@ export class SidebarComponent implements OnDestroy, HasSubscriptions {
}
}

onLocationEnterPress(e: KeyboardEvent): void {
if (this.canEdit && e.key === 'Enter') {
this.editService.openLocationDialog(this.selectedItem);
}
}

onShareClick() {
this.editService.openShareDialog(this.selectedItem);
}
Expand Down

0 comments on commit 8047b20

Please sign in to comment.