Newer
Older
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CommonModule } from '@angular/common';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ActivatedRoute, Router } from '@angular/router';
import { ItemDataService } from '../../../core/data/item-data.service';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { UploadBitstreamComponent } from './upload-bitstream.component';
import { AuthService } from '../../../core/auth/auth.service';
import { AuthServiceStub } from '../../../shared/testing/auth-service-stub';
import { Item } from '../../../core/shared/item.model';
import { of as observableOf } from 'rxjs';
import {
createPaginatedList,
createSuccessfulRemoteDataObject,
createSuccessfulRemoteDataObject$
} from '../../../shared/testing/utils';
import { RouterStub } from '../../../shared/testing/router-stub';
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service-stub';
import { VarDirective } from '../../../shared/utils/var.directive';
import { Bitstream } from '../../../core/shared/bitstream.model';
import { BundleDataService } from '../../../core/data/bundle-data.service';
import { Bundle } from '../../../core/shared/bundle.model';
import { RequestService } from '../../../core/data/request.service';
let comp: UploadBitstreamComponent;
let fixture: ComponentFixture<UploadBitstreamComponent>;
const bundle = Object.assign(new Bundle(), {
id: 'bundle',
uuid: 'bundle',
metadata: {
'dc.title': [
{
value: 'bundleName',
language: null
}
]
},
_links: {
self: { href: 'bundle-selflink' }
}
const customName = 'Custom Name';
const createdBundle = Object.assign(new Bundle(), {
id: 'created-bundle',
uuid: 'created-bundle',
metadata: {
'dc.title': [
{
value: customName,
language: null
}
]
},
_links: {
self: { href: 'created-bundle-selflink' }
}
});
const itemName = 'fake-name';
const mockItem = Object.assign(new Item(), {
id: 'fake-id',
handle: 'fake/handle',
metadata: {
'dc.title': [
{
language: null,
value: itemName
}
]
},
bundles: createSuccessfulRemoteDataObject$(createPaginatedList([bundle]))
});
let routeStub;
const routerStub = new RouterStub();
const restEndpoint = 'fake-rest-endpoint';
const mockItemDataService = jasmine.createSpyObj('mockItemDataService', {
getBitstreamsEndpoint: observableOf(restEndpoint),
createBundle: createSuccessfulRemoteDataObject$(createdBundle)
const bundleService = jasmine.createSpyObj('bundleService', {
getBitstreamsEndpoint: observableOf(restEndpoint),
findById: createSuccessfulRemoteDataObject$(bundle)
const authToken = 'fake-auth-token';
const authServiceStub = Object.assign(new AuthServiceStub(), {
buildAuthHeader: () => authToken
});
const notificationsServiceStub = new NotificationsServiceStub();
const uploaderComponent = jasmine.createSpyObj('uploaderComponent', ['ngOnInit', 'ngAfterViewInit']);
const requestService = jasmine.createSpyObj('requestService', {
removeByHrefSubstring: {}
});
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
describe('when a file is uploaded', () => {
beforeEach(async(() => {
createUploadBitstreamTestingModule({});
}));
beforeEach(() => {
loadFixtureAndComp();
});
describe('and it fails, calling onUploadError', () => {
beforeEach(() => {
comp.onUploadError();
});
it('should display an error notification', () => {
expect(notificationsServiceStub.error).toHaveBeenCalled();
});
});
describe('and it succeeds, calling onCompleteItem', () => {
const createdBitstream = Object.assign(new Bitstream(), {
id: 'fake-bitstream'
});
beforeEach(() => {
comp.onCompleteItem(createdBitstream);
});
it('should navigate the user to the next page', () => {
expect(routerStub.navigate).toHaveBeenCalled();
});
});
});
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
describe('when a bundle url parameter is present', () => {
beforeEach(async(() => {
createUploadBitstreamTestingModule({
bundle: bundle.id
});
}));
beforeEach(() => {
loadFixtureAndComp();
});
it('should set the selected id to the bundle\'s id', () => {
expect(comp.selectedBundleId).toEqual(bundle.id);
});
it('should set the selected name to the bundle\'s name', () => {
expect(comp.selectedBundleName).toEqual(bundle.name);
});
describe('and bundle name changed', () => {
beforeEach(() => {
comp.bundleNameChange();
});
it('should clear out the selected id', () => {
expect(comp.selectedBundleId).toBeUndefined();
});
});
});
describe('when a name is filled in, but no ID is selected', () => {
beforeEach(async(() => {
createUploadBitstreamTestingModule({});
}));
beforeEach(() => {
loadFixtureAndComp();
comp.selectedBundleName = customName;
});
describe('createBundle', () => {
beforeEach(() => {
comp.createBundle();
});
it('should create a new bundle', () => {
expect(mockItemDataService.createBundle).toHaveBeenCalledWith(mockItem.id, customName);
});
it('should set the selected id to the id of the new bundle', () => {
expect(comp.selectedBundleId).toEqual(createdBundle.id);
});
it('should display a success notification', () => {
expect(notificationsServiceStub.success).toHaveBeenCalled();
});
});
});
/**
* Setup an UploadBitstreamComponent testing module with custom queryParams for the route
* @param queryParams
*/
function createUploadBitstreamTestingModule(queryParams) {
routeStub = {
data: observableOf({
item: createSuccessfulRemoteDataObject(mockItem)
}),
queryParams: observableOf(queryParams),
snapshot: {
queryParams: queryParams,
params: {
id: mockItem.id
}
};
TestBed.configureTestingModule({
imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule],
declarations: [UploadBitstreamComponent, VarDirective],
providers: [
{ provide: ActivatedRoute, useValue: routeStub },
{ provide: Router, useValue: routerStub },
{ provide: ItemDataService, useValue: mockItemDataService },
{ provide: NotificationsService, useValue: notificationsServiceStub },
{ provide: AuthService, useValue: authServiceStub },
{ provide: BundleDataService, useValue: bundleService },
{ provide: RequestService, useValue: requestService }
], schemas: [
NO_ERRORS_SCHEMA
]
}).compileComponents();
}
/**
* Load the TestBed's fixture and component
*/
function loadFixtureAndComp() {
fixture = TestBed.createComponent(UploadBitstreamComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
}
});