Skip to content
Snippets Groups Projects
upload-bitstream.component.spec.ts 7.29 KiB
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';
Kristof De Langhe's avatar
Kristof De Langhe committed
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';
Kristof De Langhe's avatar
Kristof De Langhe committed
import { BundleDataService } from '../../../core/data/bundle-data.service';
import { Bundle } from '../../../core/shared/bundle.model';
import { RequestService } from '../../../core/data/request.service';
Kristof De Langhe's avatar
Kristof De Langhe committed
describe('UploadBistreamComponent', () => {
  let comp: UploadBitstreamComponent;
  let fixture: ComponentFixture<UploadBitstreamComponent>;

Kristof De Langhe's avatar
Kristof De Langhe committed
  const bundle = Object.assign(new Bundle(), {
    id: 'bundle',
    uuid: 'bundle',
Kristof De Langhe's avatar
Kristof De Langhe committed
    metadata: {
      'dc.title': [
        {
          value: 'bundleName',
          language: null
        }
      ]
    },
Kristof De Langhe's avatar
Kristof De Langhe committed
    _links: {
      self: { href: 'bundle-selflink' }
    }
Kristof De Langhe's avatar
Kristof De Langhe committed
  });
Kristof De Langhe's avatar
Kristof De Langhe committed
  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
        }
      ]
Kristof De Langhe's avatar
Kristof De Langhe committed
    },
    bundles: createSuccessfulRemoteDataObject$(createPaginatedList([bundle]))
  });
  let routeStub;
  const routerStub = new RouterStub();
  const restEndpoint = 'fake-rest-endpoint';
  const mockItemDataService = jasmine.createSpyObj('mockItemDataService', {
Kristof De Langhe's avatar
Kristof De Langhe committed
    getBitstreamsEndpoint: observableOf(restEndpoint),
    createBundle: createSuccessfulRemoteDataObject$(createdBundle)
Kristof De Langhe's avatar
Kristof De Langhe committed
  const bundleService = jasmine.createSpyObj('bundleService', {
Kristof De Langhe's avatar
Kristof De Langhe committed
    getBitstreamsEndpoint: observableOf(restEndpoint),
    findById: createSuccessfulRemoteDataObject$(bundle)
Kristof De Langhe's avatar
Kristof De Langhe committed
  });
  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: {}
  });

  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();
      });
    });
  });

Kristof De Langhe's avatar
Kristof De Langhe committed
  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)
      }),
Kristof De Langhe's avatar
Kristof De Langhe committed
      queryParams: observableOf(queryParams),
      snapshot: {
Kristof De Langhe's avatar
Kristof De Langhe committed
        queryParams: queryParams,
        params: {
          id: mockItem.id
        }
    };

    TestBed.configureTestingModule({
Kristof De Langhe's avatar
Kristof De Langhe committed
      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;
Kristof De Langhe's avatar
Kristof De Langhe committed
    comp.uploaderComponent = uploaderComponent;
    fixture.detectChanges();
  }

});