Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PER-9881: The location picker never closes #476

Merged
merged 8 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Comment on lines +172 to +176
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you cover this function with some basic tests? You do not need to cover the rest of the component for now.


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