Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
dotCMS/core#20270 The "Site or Folder" field dropdown randomly disapp…
Browse files Browse the repository at this point in the history
…ears (#1691)

* progress

* progress

* clean up

* this change because waiting for change detection

* cleanup
  • Loading branch information
hmoreras authored Jul 2, 2021
1 parent b53547c commit 0b1077e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
[archive]="archive"
[system]="system"
[live]="live"
[asField]="true"
></dot-site-selector>
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export class SiteSelectorComponent {
live: boolean;
@Input()
system: boolean;
@Input()
asField: boolean;
}

describe('SiteSelectorFieldComponent', () => {
Expand Down Expand Up @@ -141,6 +143,7 @@ describe('SiteSelectorFieldComponent', () => {
expect(siteSelector.componentInstance.archive).toBe(true);
expect(siteSelector.componentInstance.system).toBe(false);
expect(siteSelector.componentInstance.live).toBe(false);
expect(siteSelector.componentInstance.asField).toBe(true);
});

it('should not set current site when already hava a value', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<dot-searchable-dropdown
*ngIf="paginationService.totalRecords > 1; else hostName"
*ngIf="moreThanOneSite || asField; else hostName"
#searchableDropdown
(change)="siteChange($event)"
(filterChange)="handleFilterChange($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('SiteSelectorComponent', () => {
const page = 1;

paginatorService.totalRecords = 2;
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf([]));
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf(sites));
spyOn<any>(siteService, 'switchSite$').and.returnValue(observableOf({}));

fixture.detectChanges();
Expand All @@ -153,8 +153,7 @@ describe('SiteSelectorComponent', () => {
it('should paginate when the filter change', () => {
const filter = 'filter';

paginatorService.totalRecords = 2;
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf([]));
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf(sites));
spyOn<any>(siteService, 'switchSite$').and.returnValue(observableOf({}));

fixture.detectChanges();
Expand All @@ -173,7 +172,7 @@ describe('SiteSelectorComponent', () => {
it('should pass class name to searchable dropdown', async () => {
paginatorService.filter = 'filter';
paginatorService.totalRecords = 2;
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf([]));
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf(sites));

comp.cssClass = 'hello';
fixture.detectChanges();
Expand All @@ -186,7 +185,7 @@ describe('SiteSelectorComponent', () => {
it('should be assign to filter if empty', () => {
paginatorService.filter = 'filter';
paginatorService.totalRecords = 2;
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf([]));
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf(sites));

fixture.detectChanges();

Expand All @@ -202,7 +201,7 @@ describe('SiteSelectorComponent', () => {
it('should emit change event', () => {
paginatorService.filter = 'filter';
paginatorService.totalRecords = 2;
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf([]));
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf(sites));
spyOn(comp, 'handleSitesRefresh');
fixture.detectChanges();
const searchableDropdownComponent: DebugElement = de.query(
Expand Down Expand Up @@ -282,4 +281,30 @@ describe('SiteSelectorComponent', () => {
expect(paginatorService.getCurrentPage).toHaveBeenCalledTimes(1);
}));
});

it('should display as field', () => {
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf(sites));
fixture.detectChanges();
const searchableDropdownComponent: DebugElement = de.query(
By.css('dot-searchable-dropdown')
);
expect(searchableDropdownComponent).not.toBeNull();
});

it('should display only one result as field', () => {
comp.asField = true;
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf([sites[0]]));
fixture.detectChanges();
const searchableDropdownComponent: DebugElement = de.query(
By.css('dot-searchable-dropdown')
);
expect(searchableDropdownComponent).not.toBeNull();
});

it('should display as text if only one result', () => {
spyOn(paginatorService, 'getWithOffset').and.returnValue(observableOf([sites[0]]));
fixture.detectChanges();
const siteTitle: DebugElement = de.query(By.css('.site-selector__title'));
expect(siteTitle).not.toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class DotSiteSelectorComponent implements OnInit, OnChanges, OnDestroy {
@Input() cssClass: string;
@Input() width: string;
@Input() pageSize = 10;
@Input() asField = false;

@Output() change: EventEmitter<Site> = new EventEmitter();
@Output() hide: EventEmitter<any> = new EventEmitter();
Expand All @@ -49,6 +50,7 @@ export class DotSiteSelectorComponent implements OnInit, OnChanges, OnDestroy {
currentSiteSub$: Subject<Site> = new Subject();

sitesCurrentPage: Site[];
moreThanOneSite = false;

private destroy$: Subject<boolean> = new Subject<boolean>();

Expand All @@ -73,7 +75,6 @@ export class DotSiteSelectorComponent implements OnInit, OnChanges, OnDestroy {
this.siteService.refreshSites$
.pipe(takeUntil(this.destroy$))
.subscribe((_site: Site) => this.handleSitesRefresh(_site));

this.getSitesList();
['login-as', 'logout-as'].forEach((event: string) => {
this.dotEventsService
Expand All @@ -85,7 +86,9 @@ export class DotSiteSelectorComponent implements OnInit, OnChanges, OnDestroy {
});

this.siteService.switchSite$.pipe(takeUntil(this.destroy$)).subscribe(() => {
this.updateCurrentSite(this.siteService.currentSite);
setTimeout(() => {
this.updateCurrentSite(this.siteService.currentSite);
}, 200);
});
}

Expand Down Expand Up @@ -171,13 +174,13 @@ export class DotSiteSelectorComponent implements OnInit, OnChanges, OnDestroy {
* @memberof SiteSelectorComponent
*/
getSitesList(filter = '', offset = 0): void {
Promise.resolve().then(() => this.setSelectedSite());
this.paginationService.filter = filter;
this.paginationService
.getWithOffset(offset)
.pipe(take(1))
.subscribe((items: Site[]) => {
this.sitesCurrentPage = [...items];
this.moreThanOneSite = this.moreThanOneSite || items.length > 1;
});
}

Expand Down Expand Up @@ -226,15 +229,4 @@ export class DotSiteSelectorComponent implements OnInit, OnChanges, OnDestroy {
this.sitesCurrentPage = [...items];
this.updateCurrentSite(this.siteService.currentSite);
}

private setSelectedSite(): void {
if (this.id && this.siteService.currentSite.identifier !== this.id) {
this.siteService
.getSiteById(this.id)
.pipe(take(1))
.subscribe((selectedSite) => this.updateCurrentSite(selectedSite));
} else {
this.updateCurrentSite(this.siteService.currentSite);
}
}
}

0 comments on commit 0b1077e

Please sign in to comment.