[CST-7757] fix tests

This commit is contained in:
Giuseppe Digilio
2022-12-28 15:29:13 +01:00
parent 88e191e961
commit 88eefc6b79
6 changed files with 202 additions and 4545 deletions

View File

@@ -2,11 +2,11 @@ import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { BehaviorSubject, combineLatest, from, shareReplay } from 'rxjs'; import { BehaviorSubject, combineLatest, from, shareReplay } from 'rxjs';
import { map, mergeMap, take, tap } from 'rxjs/operators'; import { map, mergeMap, take, tap } from 'rxjs/operators';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'; import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
import findIndex from 'lodash/findIndex';
import { Subscription } from '../models/subscription.model'; import { Subscription } from '../models/subscription.model';
import { DSpaceObject } from '../../../core/shared/dspace-object.model'; import { DSpaceObject } from '../../../core/shared/dspace-object.model';
@@ -17,7 +17,6 @@ import { RemoteData } from '../../../core/data/remote-data';
import { getFirstCompletedRemoteData, getFirstSucceededRemoteDataPayload } from '../../../core/shared/operators'; import { getFirstCompletedRemoteData, getFirstSucceededRemoteDataPayload } from '../../../core/shared/operators';
import { AuthService } from '../../../core/auth/auth.service'; import { AuthService } from '../../../core/auth/auth.service';
import { isNotEmpty } from '../../empty.util'; import { isNotEmpty } from '../../empty.util';
import { findIndex } from 'lodash';
@Component({ @Component({
selector: 'ds-subscription-modal', selector: 'ds-subscription-modal',

View File

@@ -9,9 +9,6 @@ export class NotificationsServiceStub {
remove = jasmine.createSpy('remove'); remove = jasmine.createSpy('remove');
removeAll = jasmine.createSpy('removeAll'); removeAll = jasmine.createSpy('removeAll');
notificationWithAnchor() {
}
private getDefaultOptions(): NotificationOptions { private getDefaultOptions(): NotificationOptions {
return new NotificationOptions(); return new NotificationOptions();
} }

File diff suppressed because it is too large Load Diff

View File

@@ -7,14 +7,13 @@
<ds-themed-loading *ngIf="loading$ | async"></ds-themed-loading> <ds-themed-loading *ngIf="loading$ | async"></ds-themed-loading>
<ng-container *ngVar="(subscriptions$ | async) as subscriptions"> <ng-container *ngVar="(subscriptions$ | async) as subscriptions">
<ds-pagination <ds-pagination *ngIf="subscriptions?.pageInfo?.totalElements > 0 && !(loading$ | async)"
*ngIf="subscriptions?.pageInfo?.totalElements > 0 && !(loading$ | async)"
[paginationOptions]="config" [paginationOptions]="config"
[collectionSize]="subscriptions?.pageInfo?.totalPages" [collectionSize]="subscriptions?.pageInfo?.totalPages"
[hideGear]="true" [hideGear]="true"
[hidePagerWhenSinglePage]="true"> [hidePagerWhenSinglePage]="true">
<div class="table-responsive"> <div class="table-responsive">
<table id="formats" class="table table-striped table-hover"> <table id="formats" class="table table-striped table-hover" data-test="subscription-table">
<thead> <thead>
<tr> <tr>
<th scope="col">{{'subscriptions.table.dso' | translate}}</th> <th scope="col">{{'subscriptions.table.dso' | translate}}</th>
@@ -35,7 +34,7 @@
</div> </div>
</ds-pagination> </ds-pagination>
<ds-alert *ngIf="subscriptions?.pageInfo?.totalElements == 0 && !(loading$ | async)" [type]="'alert-info'"> <ds-alert *ngIf="subscriptions?.pageInfo?.totalElements == 0 && !(loading$ | async)" [type]="'alert-info'" data-test="empty-alert">
{{ 'subscriptions.table.empty.message' | translate }} {{ 'subscriptions.table.empty.message' | translate }}
</ds-alert> </ds-alert>

View File

@@ -1,41 +1,31 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core'; import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core';
// Import modules
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { BrowserModule, By } from '@angular/platform-browser'; import { BrowserModule, By } from '@angular/platform-browser';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { of as observableOf } from 'rxjs';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
// Import components
import { SubscriptionsPageComponent } from './subscriptions-page.component'; import { SubscriptionsPageComponent } from './subscriptions-page.component';
// Import services
import { PaginationService } from '../core/pagination/pagination.service'; import { PaginationService } from '../core/pagination/pagination.service';
import { SubscriptionService } from '../shared/subscriptions/subscription.service'; import { SubscriptionService } from '../shared/subscriptions/subscription.service';
import { PaginationServiceStub } from '../shared/testing/pagination-service.stub'; import { PaginationServiceStub } from '../shared/testing/pagination-service.stub';
import { AuthService } from '../core/auth/auth.service'; import { AuthService } from '../core/auth/auth.service';
// Import utils
import { HostWindowService } from '../shared/host-window.service';
import { HostWindowServiceStub } from '../shared/testing/host-window-service.stub';
// Import mocks
import { TranslateLoaderMock } from '../shared/mocks/translate-loader.mock'; import { TranslateLoaderMock } from '../shared/mocks/translate-loader.mock';
import { findAllSubscriptionRes } from '../shared/testing/subscriptions-data.mock';
import { MockActivatedRoute } from '../shared/mocks/active-router.mock';
import { of as observableOf } from 'rxjs';
import { EPersonMock } from '../shared/testing/eperson.mock';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { VarDirective } from '../shared/utils/var.directive';
import { import {
SubscriptionViewComponent mockSubscriptionEperson,
} from '../shared/subscriptions/components/subscription-view/subscription-view.component'; subscriptionMock,
subscriptionMock2
} from '../shared/testing/subscriptions-data.mock';
import { MockActivatedRoute } from '../shared/mocks/active-router.mock';
import { VarDirective } from '../shared/utils/var.directive';
import { SubscriptionViewComponent } from '../shared/subscriptions/subscription-view/subscription-view.component';
import { PageInfo } from '../core/shared/page-info.model';
import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils';
import { buildPaginatedList } from '../core/data/paginated-list.model';
describe('SubscriptionsPageComponent', () => { describe('SubscriptionsPageComponent', () => {
let component: SubscriptionsPageComponent; let component: SubscriptionsPageComponent;
@@ -43,15 +33,26 @@ describe('SubscriptionsPageComponent', () => {
let de: DebugElement; let de: DebugElement;
const authServiceStub = jasmine.createSpyObj('authorizationService', { const authServiceStub = jasmine.createSpyObj('authorizationService', {
getAuthenticatedUserFromStore: observableOf(EPersonMock) getAuthenticatedUserFromStore: observableOf(mockSubscriptionEperson)
}); });
const subscriptionServiceStub = jasmine.createSpyObj('SubscriptionService', { const subscriptionServiceStub = jasmine.createSpyObj('SubscriptionService', {
findByEPerson: observableOf(findAllSubscriptionRes) findByEPerson: jasmine.createSpy('findByEPerson')
}); });
const paginationService = new PaginationServiceStub(); const paginationService = new PaginationServiceStub();
beforeEach(waitForAsync( () => { const mockSubscriptionList = [subscriptionMock, subscriptionMock2];
const emptyPageInfo = Object.assign(new PageInfo(), {
totalElements: 0
});
const pageInfo = Object.assign(new PageInfo(), {
totalElements: 2
});
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
CommonModule, CommonModule,
@@ -65,10 +66,9 @@ describe('SubscriptionsPageComponent', () => {
}), }),
NoopAnimationsModule NoopAnimationsModule
], ],
declarations: [ SubscriptionsPageComponent, SubscriptionViewComponent, VarDirective ], declarations: [SubscriptionsPageComponent, SubscriptionViewComponent, VarDirective],
providers:[ providers: [
{ provide: SubscriptionService, useValue: subscriptionServiceStub }, { provide: SubscriptionService, useValue: subscriptionServiceStub },
{ provide: HostWindowService, useValue: new HostWindowServiceStub(0) },
{ provide: ActivatedRoute, useValue: new MockActivatedRoute() }, { provide: ActivatedRoute, useValue: new MockActivatedRoute() },
{ provide: AuthService, useValue: authServiceStub }, { provide: AuthService, useValue: authServiceStub },
{ provide: PaginationService, useValue: paginationService } { provide: PaginationService, useValue: paginationService }
@@ -82,46 +82,46 @@ describe('SubscriptionsPageComponent', () => {
fixture = TestBed.createComponent(SubscriptionsPageComponent); fixture = TestBed.createComponent(SubscriptionsPageComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
de = fixture.debugElement; de = fixture.debugElement;
});
describe('when there are subscriptions', () => {
beforeEach(() => {
subscriptionServiceStub.findByEPerson.and.returnValue(createSuccessfulRemoteDataObject$(buildPaginatedList(pageInfo, mockSubscriptionList)));
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should create', () => { it('should create', () => {
fixture.detectChanges();
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
describe('when table', () => { it('should show table', () => {
expect(de.query(By.css('[data-test="subscription-table"]'))).toBeTruthy();
expect(de.query(By.css('[data-test="empty-alert"]'))).toBeNull();
});
it('should show table', async() => { it('should show a row for each results entry',() => {
await fixture.whenStable(); expect(de.query(By.css('[data-test="subscription-table"]'))).toBeTruthy();
expect(de.query(By.css('[data-test="empty-alert"]'))).toBeNull();
expect(de.queryAll(By.css('tbody > tr')).length).toEqual(2);
});
});
describe('when there are no subscriptions', () => {
beforeEach(() => {
subscriptionServiceStub.findByEPerson.and.returnValue(createSuccessfulRemoteDataObject$(buildPaginatedList(emptyPageInfo, [])));
fixture.detectChanges(); fixture.detectChanges();
const table = de.query(By.css('table'));
expect(table).toBeTruthy();
}); });
it('should create', () => {
expect(component).toBeTruthy();
}); });
it('should show all the results', () => { it('should not show table', () => {
expect(de.queryAll(By.css('tbody > tr')).length).toEqual(10); expect(de.query(By.css('[data-test="subscription-table"]'))).toBeNull();
expect(de.query(By.css('[data-test="empty-alert"]'))).toBeTruthy();
}); });
it('should have dso object info', () => {
expect(de.query(By.css('.dso-info > span'))).toBeTruthy();
expect(de.query(By.css('.dso-info > p > a'))).toBeTruthy();
}); });
it('should have subscription type info', () => {
expect(de.query(By.css('.subscription-type'))).toBeTruthy();
});
it('should have subscription paramenter info', () => {
expect(de.query(By.css('.subscription-parmenters > span'))).toBeTruthy();
});
it('should have subscription action info', () => {
expect(de.query(By.css('.btn-outline-primary'))).toBeTruthy();
expect(de.query(By.css('.btn-outline-danger'))).toBeTruthy();
});
}); });

View File

@@ -11,7 +11,8 @@ import { PaginationService } from '../core/pagination/pagination.service';
import { PageInfo } from '../core/shared/page-info.model'; import { PageInfo } from '../core/shared/page-info.model';
import { AuthService } from '../core/auth/auth.service'; import { AuthService } from '../core/auth/auth.service';
import { EPerson } from '../core/eperson/models/eperson.model'; import { EPerson } from '../core/eperson/models/eperson.model';
import { getFirstSucceededRemoteDataPayload } from '../core/shared/operators'; import { getFirstCompletedRemoteData } from '../core/shared/operators';
import { RemoteData } from '../core/data/remote-data';
@Component({ @Component({
selector: 'ds-subscriptions-page', selector: 'ds-subscriptions-page',
@@ -43,7 +44,7 @@ export class SubscriptionsPageComponent implements OnInit {
ePersonId$: Observable<string>; ePersonId$: Observable<string>;
/** /**
* EPerson id of the logged in user * EPerson id of the logged-in user
*/ */
// ePersonId: string; // ePersonId: string;
@@ -61,35 +62,26 @@ export class SubscriptionsPageComponent implements OnInit {
this.ePersonId$ = this.authService.getAuthenticatedUserFromStore().pipe( this.ePersonId$ = this.authService.getAuthenticatedUserFromStore().pipe(
take(1), take(1),
map((ePerson: EPerson) => ePerson.id), map((ePerson: EPerson) => ePerson.id),
shareReplay(), shareReplay()
/*tap((ePersonId: string) => { // TODO unused
this.ePersonId = ePersonId;
}),*/
); );
this.retrieveSubscriptions(); this.retrieveSubscriptions();
} }
private retrieveSubscriptions() { private retrieveSubscriptions() {
this.paginationService.getCurrentPagination(this.config.id, this.config).pipe( this.paginationService.getCurrentPagination(this.config.id, this.config).pipe(
tap(console.log),
combineLatestWith(this.ePersonId$), combineLatestWith(this.ePersonId$),
tap(() => {this.loading$.next(true);}), tap(() => this.loading$.next(true)),
switchMap(([currentPagination, ePersonId]) => this.subscriptionService.findByEPerson(ePersonId,{ switchMap(([currentPagination, ePersonId]) => this.subscriptionService.findByEPerson(ePersonId,{
currentPage: currentPagination.currentPage, currentPage: currentPagination.currentPage,
elementsPerPage: currentPagination.pageSize elementsPerPage: currentPagination.pageSize
})), })),
getFirstSucceededRemoteDataPayload(), getFirstCompletedRemoteData()
tap((x) => console.log('find', x)),
// getFirstSucceededRemoteDataPayload(), ).subscribe((res: RemoteData<PaginatedList<Subscription>>) => {
).subscribe({ if (res.hasSucceeded) {
next: (res: any) => { this.subscriptions$.next(res.payload);
console.log('next',res);
this.subscriptions$.next(res);
this.loading$.next(false);
},
error: () => {
this.loading$.next(false);
} }
this.loading$.next(false);
}); });
} }
/** /**