mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-10 11:33:04 +00:00
54472: CreateCommunityPageComponent and CreateCollectionPageComponent tests
This commit is contained in:
@@ -0,0 +1,99 @@
|
|||||||
|
import { SharedModule } from '../../shared/shared.module';
|
||||||
|
import { Community } from '../../core/shared/community.model';
|
||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
import { DSOSuccessResponse, ErrorResponse } from '../../core/cache/response-cache.models';
|
||||||
|
import { CommonModule } from '@angular/common';
|
||||||
|
import { CreateCommunityPageComponent } from '../../+community-page/create-community-page/create-community-page.component';
|
||||||
|
import { Router } from '@angular/router';
|
||||||
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
|
import { CommunityFormComponent } from '../../+community-page/community-form/community-form.component';
|
||||||
|
import { Observable } from 'rxjs/Observable';
|
||||||
|
import { CommunityDataService } from '../../core/data/community-data.service';
|
||||||
|
import { RequestError } from '../../core/data/request.models';
|
||||||
|
import { RouteService } from '../../shared/services/route.service';
|
||||||
|
import { RemoteData } from '../../core/data/remote-data';
|
||||||
|
import { CreateCollectionPageComponent } from './create-collection-page.component';
|
||||||
|
import { CollectionDataService } from '../../core/data/collection-data.service';
|
||||||
|
import { Collection } from '../../core/shared/collection.model';
|
||||||
|
import { RouterTestingModule } from '@angular/router/testing';
|
||||||
|
import { CollectionFormComponent } from '../collection-form/collection-form.component';
|
||||||
|
|
||||||
|
describe('CreateCollectionPageComponent', () => {
|
||||||
|
let comp: CreateCollectionPageComponent;
|
||||||
|
let fixture: ComponentFixture<CreateCollectionPageComponent>;
|
||||||
|
let collectionDataService: CollectionDataService;
|
||||||
|
let communityDataService: CommunityDataService;
|
||||||
|
let routeService: RouteService;
|
||||||
|
let router: Router;
|
||||||
|
|
||||||
|
const community = Object.assign(new Community(), {
|
||||||
|
uuid: 'a20da287-e174-466a-9926-f66b9300d347',
|
||||||
|
name: 'test community'
|
||||||
|
});
|
||||||
|
|
||||||
|
const collectionDataServiceStub = {
|
||||||
|
create: (com, uuid?) => Observable.of({
|
||||||
|
response: new DSOSuccessResponse(null,'200',null)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
const communityDataServiceStub = {
|
||||||
|
findById: (uuid) => Observable.of(new RemoteData(false, false, true, null, Object.assign(new Community(), {
|
||||||
|
uuid: uuid,
|
||||||
|
name: community.name
|
||||||
|
})))
|
||||||
|
};
|
||||||
|
const routeServiceStub = {
|
||||||
|
getQueryParameterValue: (param) => Observable.of(community.uuid)
|
||||||
|
};
|
||||||
|
const routerStub = {
|
||||||
|
navigateByUrl: (url) => url
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
imports: [TranslateModule.forRoot(), SharedModule, CommonModule, RouterTestingModule],
|
||||||
|
declarations: [CreateCollectionPageComponent, CollectionFormComponent],
|
||||||
|
providers: [
|
||||||
|
{ provide: CollectionDataService, useValue: collectionDataServiceStub },
|
||||||
|
{ provide: CommunityDataService, useValue: communityDataServiceStub },
|
||||||
|
{ provide: RouteService, useValue: routeServiceStub },
|
||||||
|
{ provide: Router, useValue: routerStub }
|
||||||
|
]
|
||||||
|
}).compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(CreateCollectionPageComponent);
|
||||||
|
comp = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
collectionDataService = (comp as any).collectionDataService;
|
||||||
|
communityDataService = (comp as any).communityDataService;
|
||||||
|
routeService = (comp as any).routeService;
|
||||||
|
router = (comp as any).router;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('onSubmit', () => {
|
||||||
|
const data = {
|
||||||
|
name: 'test'
|
||||||
|
};
|
||||||
|
|
||||||
|
it('should navigate when successful', () => {
|
||||||
|
spyOn(router, 'navigateByUrl');
|
||||||
|
comp.onSubmit(data);
|
||||||
|
fixture.detectChanges();
|
||||||
|
expect(router.navigateByUrl).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not navigate on failure', () => {
|
||||||
|
spyOn(router, 'navigateByUrl');
|
||||||
|
spyOn(collectionDataService, 'create').and.returnValue(Observable.of({
|
||||||
|
response: Object.assign(new ErrorResponse(new RequestError()), {
|
||||||
|
isSuccessful: false
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
comp.onSubmit(data);
|
||||||
|
fixture.detectChanges();
|
||||||
|
expect(router.navigateByUrl).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@@ -7,18 +7,20 @@ import { TranslateModule } from '@ngx-translate/core';
|
|||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
import { RemoteData } from '../../core/data/remote-data';
|
import { RemoteData } from '../../core/data/remote-data';
|
||||||
import { Community } from '../../core/shared/community.model';
|
import { Community } from '../../core/shared/community.model';
|
||||||
import { DSOSuccessResponse } from '../../core/cache/response-cache.models';
|
import { DSOSuccessResponse, ErrorResponse } from '../../core/cache/response-cache.models';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
import { SharedModule } from '../../shared/shared.module';
|
import { SharedModule } from '../../shared/shared.module';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { CommunityFormComponent } from '../community-form/community-form.component';
|
import { CommunityFormComponent } from '../community-form/community-form.component';
|
||||||
|
import { RouterTestingModule } from '@angular/router/testing';
|
||||||
|
import { RequestError } from '../../core/data/request.models';
|
||||||
|
|
||||||
describe('CreateCommunityPageComponent', () => {
|
describe('CreateCommunityPageComponent', () => {
|
||||||
let comp: CreateCommunityPageComponent;
|
let comp: CreateCommunityPageComponent;
|
||||||
let fixture: ComponentFixture<CreateCommunityPageComponent>;
|
let fixture: ComponentFixture<CreateCommunityPageComponent>;
|
||||||
let communityDataService: CommunityDataService;
|
let communityDataService: CommunityDataService;
|
||||||
let routeService: RouteService;
|
let routeService: RouteService;
|
||||||
let router: any = {};
|
let router: Router;
|
||||||
|
|
||||||
const community = Object.assign(new Community(), {
|
const community = Object.assign(new Community(), {
|
||||||
uuid: 'a20da287-e174-466a-9926-f66b9300d347',
|
uuid: 'a20da287-e174-466a-9926-f66b9300d347',
|
||||||
@@ -37,15 +39,18 @@ describe('CreateCommunityPageComponent', () => {
|
|||||||
const routeServiceStub = {
|
const routeServiceStub = {
|
||||||
getQueryParameterValue: (param) => Observable.of(community.uuid)
|
getQueryParameterValue: (param) => Observable.of(community.uuid)
|
||||||
};
|
};
|
||||||
|
const routerStub = {
|
||||||
|
navigateByUrl: (url) => url
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(async(() => {
|
beforeEach(async(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
imports: [TranslateModule.forRoot(), SharedModule, CommonModule],
|
imports: [TranslateModule.forRoot(), SharedModule, CommonModule, RouterTestingModule],
|
||||||
declarations: [CreateCommunityPageComponent, CommunityFormComponent],
|
declarations: [CreateCommunityPageComponent, CommunityFormComponent],
|
||||||
providers: [
|
providers: [
|
||||||
{ provide: CommunityDataService, useValue: communityDataServiceStub },
|
{ provide: CommunityDataService, useValue: communityDataServiceStub },
|
||||||
{ provide: RouteService, useValue: routeServiceStub },
|
{ provide: RouteService, useValue: routeServiceStub },
|
||||||
{ provide: Router, useValue: router }
|
{ provide: Router, useValue: routerStub }
|
||||||
]
|
]
|
||||||
}).compileComponents();
|
}).compileComponents();
|
||||||
}));
|
}));
|
||||||
@@ -59,11 +64,28 @@ describe('CreateCommunityPageComponent', () => {
|
|||||||
router = (comp as any).router;
|
router = (comp as any).router;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should navigate on successful submit', () => {
|
describe('onSubmit', () => {
|
||||||
spyOn(router, 'navigateByUrl');
|
const data = {
|
||||||
comp.onSubmit({
|
|
||||||
name: 'test'
|
name: 'test'
|
||||||
|
};
|
||||||
|
|
||||||
|
it('should navigate when successful', () => {
|
||||||
|
spyOn(router, 'navigateByUrl');
|
||||||
|
comp.onSubmit(data);
|
||||||
|
fixture.detectChanges();
|
||||||
|
expect(router.navigateByUrl).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not navigate on failure', () => {
|
||||||
|
spyOn(router, 'navigateByUrl');
|
||||||
|
spyOn(communityDataService, 'create').and.returnValue(Observable.of({
|
||||||
|
response: Object.assign(new ErrorResponse(new RequestError()), {
|
||||||
|
isSuccessful: false
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
comp.onSubmit(data);
|
||||||
|
fixture.detectChanges();
|
||||||
|
expect(router.navigateByUrl).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
expect(router.navigateByUrl).toHaveBeenCalled();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user