Skip to content

Commit

Permalink
general: MainTitle as a pipe and test imports refactoring.
Browse files Browse the repository at this point in the history
* Refactoring MainTitleService as a pipe. It allows to use it more easely
  into components because we don't need to import the service and create
  a repetitive method in each component to get a document main title.
* This commit also renamed the Checkin component test file according to
  component name (previously named Checkout).
* Refactoring test 'spec.ts' files to limit imports and declarations.
  As main modules (AppModule and CirculationModules) already define
  many imports and declarations, use them into the 'spec.ts' file
  instead of a lot of single imports.
* Refactor name of some private attributes adding a leading '_'.

Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
  • Loading branch information
zannkukai committed Apr 30, 2020
1 parent a5a7be4 commit 5268819
Show file tree
Hide file tree
Showing 63 changed files with 328 additions and 635 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,28 @@

import { HttpClientModule } from '@angular/common/http';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { CoreModule, RecordModule } from '@rero/ng-core';
import { CollapseModule } from 'ngx-bootstrap/collapse';
import { userTestingService } from '../../../../tests/utils';
import { UserService } from '../../service/user.service';
import { ItemComponent } from '../item/item.component';
import { ItemsListComponent } from '../items-list/items-list.component';
import { CardComponent } from '../patron/card/card.component';
import { CirculationModule } from '../circulation.module';
import { CheckinComponent } from './checkin.component';


describe('CheckoutComponent', () => {
describe('CheckinComponent', () => {
let component: CheckinComponent;
let fixture: ComponentFixture<CheckinComponent>;

const userService = jasmine.createSpyObj(
'UserService', ['getCurrentUser']
);
userService.getCurrentUser.and.returnValue({
first_name: 'John',
last_name: 'Doe',
library: {
pid: '1',
organisation: {
pid: '1'
},
current: '1',
},
getCurrentLibrary: () => '1'
});

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot(),
CoreModule,
RecordModule,
HttpClientModule,
RouterTestingModule,
CollapseModule,
BrowserAnimationsModule
],
declarations: [
CardComponent,
CheckinComponent,
ItemsListComponent,
ItemComponent
CirculationModule
],
providers: [
{ provide: UserService, useValue: userService }
{ provide: UserService, useValue: userTestingService }
]
})
.compileComponents();
Expand Down
8 changes: 4 additions & 4 deletions projects/admin/src/app/circulation/item/item.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
<a [routerLink]="['/records','items','detail', item.pid]">{{ item.barcode }}</a>
</div>
<!-- TITLE -->
<div class="col-lg-4"><a
[routerLink]="['/records','documents','detail', item.document.pid]"
*ngIf="getMainTitle(item.document.title) != null">
{{ getMainTitle(item.document.title) | truncateText: 12 }}</a>
<div class="col-lg-4">
<a [routerLink]="['/records','documents','detail', item.document.pid]" *ngIf="item.document.title | mainTitle as title ">
{{ title | truncateText: 12 }}
</a>
</div>
<!-- CIRCULATION INFO -->
<div class="col-lg-3">
Expand Down
23 changes: 5 additions & 18 deletions projects/admin/src/app/circulation/item/item.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { HttpClientModule } from '@angular/common/http';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { CirculationModule } from '../circulation.module';
import { ItemComponent } from './item.component';
import { TranslateModule } from '@ngx-translate/core';
import { CoreModule, RecordModule } from '@rero/ng-core';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientModule } from '@angular/common/http';
import { CollapseModule } from 'ngx-bootstrap/collapse';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { LoanComponent } from '../patron/loan/loan.component';
import { ItemsListComponent } from '../items-list/items-list.component';

describe('ItemComponent', () => {
let component: ItemComponent;
Expand All @@ -18,17 +13,9 @@ describe('ItemComponent', () => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot(),
CoreModule,
RecordModule,
RouterTestingModule,
HttpClientModule,
CollapseModule,
BrowserAnimationsModule
],
declarations: [
LoanComponent,
ItemsListComponent,
ItemComponent
CirculationModule
]
})
.compileComponents();
Expand Down
12 changes: 1 addition & 11 deletions projects/admin/src/app/circulation/item/item.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { RecordService } from '@rero/ng-core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { MainTitleService } from '../../service/main-title.service';
import { OrganisationService } from '../../service/organisation.service';
import { Item, ItemAction, Loan, LoanState } from '../items';
import { PatronTransactionService } from '../patron-transaction.service';
Expand Down Expand Up @@ -66,8 +65,7 @@ export class ItemComponent implements OnInit {
constructor(
private recordService: RecordService,
private organisationService: OrganisationService,
private patronTransactionService: PatronTransactionService,
private _mainTitleService: MainTitleService
private patronTransactionService: PatronTransactionService
) { }

/**
Expand Down Expand Up @@ -124,12 +122,4 @@ export class ItemComponent implements OnInit {
get organisation() {
return this.organisationService.organisation;
}

/**
* Get main title (correspondig to 'bf_Title' type, present only once in metadata)
* @param titleMetadata: title metadata
*/
getMainTitle(titleMetadata: any): string {
return this._mainTitleService.getMainTitle(titleMetadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ItemsListComponent } from './items-list.component';
import { HttpClientModule } from '@angular/common/http';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { CoreModule, RecordModule } from '@rero/ng-core';
import { ItemComponent } from '../item/item.component';
import { HttpClientModule } from '@angular/common/http';
import { CollapseModule } from 'ngx-bootstrap/collapse';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CirculationModule } from '../circulation.module';
import { ItemsListComponent } from './items-list.component';


describe('ItemsListComponent', () => {
let component: ItemsListComponent;
Expand All @@ -34,16 +30,9 @@ describe('ItemsListComponent', () => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
TranslateModule,
CoreModule,
RecordModule,
TranslateModule.forRoot(),
HttpClientModule,
CollapseModule,
BrowserAnimationsModule
],
declarations: [
ItemsListComponent,
ItemComponent
CirculationModule
]
})
.compileComponents();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ import { HttpClientModule } from '@angular/common/http';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { RecordModule } from '@rero/ng-core';
import { RequestedItemsListComponent } from '../requested-items-list/requested-items-list.component';
import { CirculationModule } from '../circulation.module';
import { MainRequestComponent } from './main-request.component';


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

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RecordModule, RouterTestingModule, HttpClientModule, TranslateModule.forRoot()],
declarations: [ MainRequestComponent, RequestedItemsListComponent ]
imports: [
RouterTestingModule,
HttpClientModule,
TranslateModule.forRoot(),
CirculationModule
]
})
.compileComponents();
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import { ItemsService } from '../items.service';

@Component({
selector: 'admin-circulation-main-request',
templateUrl: './main-request.component.html',
styleUrls: ['./main-request.component.scss']
templateUrl: './main-request.component.html'
})
export class MainRequestComponent implements OnInit {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@ import { HttpClientModule } from '@angular/common/http';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { CirculationModule } from '../../circulation.module';
import { CardComponent } from './card.component';


describe('PatronDetailedComponent', () => {
describe('CardComponent', () => {
let component: CardComponent;
let fixture: ComponentFixture<CardComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [TranslateModule, RouterTestingModule, HttpClientModule],
declarations: [ CardComponent ]
imports: [
TranslateModule.forRoot(),
RouterTestingModule,
HttpClientModule,
CirculationModule
]
})
.compileComponents();
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
<i [ngClass]="{ 'fa-caret-down': !isCollapsed, 'fa-caret-right': isCollapsed }" class="fa" aria-hidden="true"></i>
</button>
<a [routerLink]="['/records','documents','detail', document.metadata.pid]"
*ngIf="getMainTitle(document.metadata.title) != null">
*ngIf="document.metadata.title | mainTitle as title">
<ng-container *ngIf="isCollapsed; else titleNotCollapsed">
{{ getMainTitle(document.metadata.title) | truncateText: 10 }}
{{ title | truncateText: 10 }}
</ng-container>
<ng-template #titleNotCollapsed>
{{ getMainTitle(document.metadata.title) }}
{{ title }}
</ng-template>
</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { CoreModule, RecordModule } from '@rero/ng-core';
import { HttpClientModule } from '@angular/common/http';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CirculationModule } from '../../../circulation.module';
import { HistoryItemComponent } from './history-item.component';
import { SharedPipesModule } from '../../../../shared/shared-pipes.module';

describe('HistoryItemComponent', () => {
let component: HistoryItemComponent;
Expand All @@ -31,13 +30,10 @@ describe('HistoryItemComponent', () => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot(),
CoreModule,
RecordModule,
RouterTestingModule,
HttpClientModule,
SharedPipesModule
],
declarations: [ HistoryItemComponent ]
CirculationModule
]
})
.compileComponents();
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { Component, Input, OnInit } from '@angular/core';
import { extractIdOnRef, RecordService } from '@rero/ng-core';
import { forkJoin } from 'rxjs';
import { ProvisionActivityType } from '../../../../pipe/provision-activity.pipe';
import { MainTitleService } from '../../../../service/main-title.service';
import { PatronService } from '../../../../service/patron.service';

@Component({
Expand Down Expand Up @@ -53,8 +52,7 @@ export class HistoryItemComponent implements OnInit {
*/
constructor(
private _recordService: RecordService,
private _patronService: PatronService,
private _mainTitleService: MainTitleService
private _patronService: PatronService
) { }

/**
Expand All @@ -80,14 +78,4 @@ export class HistoryItemComponent implements OnInit {
});
}
}

/**
* Get main title (corresponding to 'bf_Title' type, present only once in metadata)
* @param titleMetadata: title metadata
* @return string: the document main title, `null` if not found
*/
getMainTitle(titleMetadata: any): string {
return this._mainTitleService.getMainTitle(titleMetadata);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { CoreModule, RecordModule } from '@rero/ng-core';
import { HttpClientModule } from '@angular/common/http';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CirculationModule } from '../../circulation.module';
import { HistoryComponent } from './history.component';
import { HistoryItemComponent } from './history-item/history-item.component';
import { SharedPipesModule } from '../../../shared/shared-pipes.module';

describe('HistoryComponent', () => {
let component: HistoryComponent;
Expand All @@ -32,15 +30,9 @@ describe('HistoryComponent', () => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot(),
CoreModule,
RecordModule,
RouterTestingModule,
HttpClientModule,
SharedPipesModule
],
declarations: [
HistoryComponent,
HistoryItemComponent
CirculationModule
]
})
.compileComponents();
Expand Down
Loading

0 comments on commit 5268819

Please sign in to comment.