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

[ACA-20] Add/Remove favorite library #804

Merged
merged 9 commits into from
Nov 16, 2018
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
14 changes: 8 additions & 6 deletions docs/extending/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ The components are used to create custom:
- toolbar buttons
- menu items

| Key | Type | Description |
| -- | -- | -- |
| app.layout.main | LayoutComponent | Main application layout with the menu bar, navigation sidebar and main content area to project your components. |
| app.toolbar.toggleInfoDrawer | ToggleInfoDrawerComponent | The toolbar button component that toggles Info Drawer for the selection. |
| app.toolbar.toggleFavorite | ToggleFavoriteComponent | The toolbar button component that toggles Favorite state for the selection. |
| Key | Type | Description |
| --------------------------------- | ------------------------------ | --------------------------------------------------------------------------------------------------------------- |
| app.layout.main | LayoutComponent | Main application layout with the menu bar, navigation sidebar and main content area to project your components. |
| app.toolbar.toggleInfoDrawer | ToggleInfoDrawerComponent | The toolbar button component that toggles Info Drawer for the selection. |
| app.toolbar.toggleFavorite | ToggleFavoriteComponent | The toolbar button component that toggles Favorite state for the selection. |
| app.toolbar.toggleFavoriteLibrary | ToggleFavoriteLibraryComponent | The toolbar button component that toggles Favorite library state for the selection. |

See [Registration](/extending/registration) section for more details
on how to register your own entries to be re-used at runtime.

Note that custom extensions can also replace any existing component at runtime by a known identifier,
besides registering a new one.
besides registering a new one.
9 changes: 7 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ import { AppHeaderModule } from './components/header/header.module';
import { environment } from '../environments/environment';
import { LibraryMembershipDirective } from './directives/library-membership.directive';
import { ToggleJoinLibraryComponent } from './components/toolbar/toggle-join-library/toggle-join-library.component';
import { LibraryFavoriteDirective } from './directives/library-favorite.directive';
import { ToggleFavoriteLibraryComponent } from './components/toolbar/toggle-favorite-library/toggle-favorite-library.component';

@NgModule({
imports: [
Expand Down Expand Up @@ -114,7 +116,9 @@ import { ToggleJoinLibraryComponent } from './components/toolbar/toggle-join-lib
NodeVersionsDialogComponent,
LibraryDialogComponent,
LibraryMembershipDirective,
ToggleJoinLibraryComponent
ToggleJoinLibraryComponent,
LibraryFavoriteDirective,
ToggleFavoriteLibraryComponent
],
providers: [
{ provide: RouteReuseStrategy, useClass: AppRouteReuseStrategy },
Expand All @@ -131,7 +135,8 @@ import { ToggleJoinLibraryComponent } from './components/toolbar/toggle-join-lib
entryComponents: [
LibraryDialogComponent,
NodeVersionsDialogComponent,
ToggleJoinLibraryComponent
ToggleJoinLibraryComponent,
ToggleFavoriteLibraryComponent
],
bootstrap: [AppComponent]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export class FavoriteLibrariesComponent extends PageComponent

this.subscriptions = this.subscriptions.concat([
this.content.libraryDeleted.subscribe(() => this.reload()),
this.content.libraryUpdated.subscribe(() => this.documentList.reload()),
this.content.libraryUpdated.subscribe(() => this.reload()),
this.content.favoriteLibraryToggle.subscribe(() => this.reload()),

this.breakpointObserver
.observe([Breakpoints.HandsetPortrait, Breakpoints.HandsetLandscape])
.subscribe(result => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/libraries/libraries.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class LibrariesComponent extends PageComponent implements OnInit {

this.subscriptions.push(
this.content.libraryDeleted.subscribe(() => this.reload()),
this.content.libraryUpdated.subscribe(() => this.documentList.reload()),
this.content.libraryUpdated.subscribe(() => this.reload()),

this.breakpointObserver
.observe([Breakpoints.HandsetPortrait, Breakpoints.HandsetLandscape])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2018 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/

import { TestBed } from '@angular/core/testing';
import {
setupTestBed,
CoreModule,
AlfrescoApiService,
AlfrescoApiServiceMock
} from '@alfresco/adf-core';
import { ToggleFavoriteLibraryComponent } from './toggle-favorite-library.component';
import { LibraryFavoriteDirective } from '../../../directives/library-favorite.directive';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppTestingModule } from '../../../testing/app-testing.module';
import { ContentManagementService } from '../../../services/content-management.service';
import { of } from 'rxjs';

describe('ToggleFavoriteLibraryComponent', () => {
let fixture;
let component;
let contentManagementService;
const selection = { library: { entry: { id: 'libraryId' } } };

setupTestBed({
imports: [CoreModule, AppTestingModule],
declarations: [ToggleFavoriteLibraryComponent, LibraryFavoriteDirective],
providers: [
{
provide: AlfrescoApiService,
useClass: AlfrescoApiServiceMock
},
{
provide: Store,
useValue: {
dispatch: () => {},
select: () => of(selection)
}
},
ContentManagementService
],
schemas: [NO_ERRORS_SCHEMA]
});

beforeEach(() => {
fixture = TestBed.createComponent(ToggleFavoriteLibraryComponent);
component = fixture.componentInstance;

contentManagementService = TestBed.get(ContentManagementService);
const api = TestBed.get(AlfrescoApiService);
spyOn(api.peopleApi, 'getFavoriteSite').and.returnValue(Promise.resolve());
});

it('should get library selection from Store', done => {
fixture.detectChanges();
component.selection$.subscribe(selected => {
expect(selected).toEqual(selection);
done();
});
});

it('should emit onToggleEvent() event', () => {
fixture.detectChanges();
spyOn(contentManagementService.favoriteLibraryToggle, 'next');

component.onToggleEvent();

expect(
contentManagementService.favoriteLibraryToggle.next
).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2018 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/

import { Component, ViewEncapsulation, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppStore } from '../../../store/states';
import { appSelection } from '../../../store/selectors/app.selectors';
import { Observable } from 'rxjs';
import { SelectionState } from '@alfresco/adf-extensions';
import { ContentManagementService } from '../../../services/content-management.service';

@Component({
selector: 'app-toggle-favorite-library',
template: `
<button
mat-menu-item
#favoriteLibrary="favoriteLibrary"
(toggle)="onToggleEvent()"
[acaFavoriteLibrary]="(selection$ | async).library"
>
<mat-icon *ngIf="favoriteLibrary.isFavorite()">star</mat-icon>
<mat-icon *ngIf="!favoriteLibrary.isFavorite()">star_border</mat-icon>
<span>{{ 'APP.ACTIONS.FAVORITE' | translate }}</span>
</button>
`,
encapsulation: ViewEncapsulation.None,
host: { class: 'app-toggle-favorite-library' }
})
export class ToggleFavoriteLibraryComponent implements OnInit {
selection$: Observable<SelectionState>;

constructor(
private store: Store<AppStore>,
private content: ContentManagementService
) {}

ngOnInit() {
this.selection$ = this.store.select(appSelection);
}

onToggleEvent() {
this.content.favoriteLibraryToggle.next();
}
}
144 changes: 144 additions & 0 deletions src/app/directives/library-favorite.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*!
* @license
* Alfresco Example Content Application
*
* Copyright (C) 2005 - 2018 Alfresco Software Limited
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/

import { Component, ViewChild } from '@angular/core';
import { LibraryFavoriteDirective } from './library-favorite.directive';
import {
AlfrescoApiService,
AlfrescoApiServiceMock,
setupTestBed,
CoreModule
} from '@alfresco/adf-core';
import { TestBed, async } from '@angular/core/testing';

@Component({
selector: 'app-test-component',
template: `
<button
#favoriteLibrary="favoriteLibrary"
[acaFavoriteLibrary]="selection"
></button>
`
})
class TestComponent {
@ViewChild('favoriteLibrary')
directive: LibraryFavoriteDirective;

selection = null;
}

describe('LibraryFavoriteDirective', () => {
let fixture;
let api;
let component;
let selection;

setupTestBed({
imports: [CoreModule],
declarations: [TestComponent, LibraryFavoriteDirective],
providers: [
{
provide: AlfrescoApiService,
useClass: AlfrescoApiServiceMock
}
]
});

beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
api = TestBed.get(AlfrescoApiService);
selection = { entry: { guid: 'guid', id: 'id' } };
});

it('should not check for favorite if no selection exists', () => {
spyOn(api.peopleApi, 'getFavoriteSite');
fixture.detectChanges();

expect(api.peopleApi.getFavoriteSite).not.toHaveBeenCalled();
});

it('should mark selection as favorite when getFavoriteSite returns successfully', async(() => {
spyOn(api.peopleApi, 'getFavoriteSite').and.returnValue(Promise.resolve());
component.selection = selection;
fixture.detectChanges();

fixture.whenStable().then(() => {
expect(api.peopleApi.getFavoriteSite).toHaveBeenCalled();
expect(component.directive.isFavorite()).toBe(true);
});
}));

it('should mark selection not favorite when getFavoriteSite errors', async(() => {
spyOn(api.peopleApi, 'getFavoriteSite').and.returnValue(Promise.reject());
component.selection = selection;
fixture.detectChanges();

fixture.whenStable().then(() => {
expect(api.peopleApi.getFavoriteSite).toHaveBeenCalled();
expect(component.directive.isFavorite()).toBe(false);
});
}));

it('should call addFavorite() on click event when selection is not a favorite', async(() => {
spyOn(api.peopleApi, 'getFavoriteSite').and.returnValue(Promise.reject());
spyOn(api.peopleApi, 'addFavorite').and.returnValue(Promise.resolve());
component.selection = selection;
fixture.detectChanges();

expect(component.directive.isFavorite()).toBeFalsy();

fixture.whenStable().then(() => {
fixture.nativeElement
.querySelector('button')
.dispatchEvent(new MouseEvent('click'));

fixture.detectChanges();

expect(api.peopleApi.addFavorite).toHaveBeenCalled();
});
}));

it('should call removeFavoriteSite() on click event when selection is not a favorite', async(() => {
spyOn(api.peopleApi, 'getFavoriteSite').and.returnValue(Promise.resolve());
spyOn(api.peopleApi, 'removeFavoriteSite').and.returnValue(
Promise.resolve()
);
component.selection = selection;
fixture.detectChanges();

expect(component.directive.isFavorite()).toBeFalsy();

fixture.whenStable().then(() => {
fixture.nativeElement
.querySelector('button')
.dispatchEvent(new MouseEvent('click'));

fixture.detectChanges();

expect(api.peopleApi.removeFavoriteSite).toHaveBeenCalled();
});
}));
});
Loading