Skip to content
Snippets Groups Projects
  • Giuseppe Digilio's avatar
    Merge remote-tracking branch 'remotes/origin/master' into submission · 880d9ed0
    Giuseppe Digilio authored
    # Conflicts:
    #	package.json
    #	src/app/+item-page/edit-item-page/item-delete/item-delete.component.spec.ts
    #	src/app/+item-page/edit-item-page/item-private/item-private.component.spec.ts
    #	src/app/+item-page/edit-item-page/item-public/item-public.component.spec.ts
    #	src/app/+item-page/edit-item-page/item-reinstate/item-reinstate.component.spec.ts
    #	src/app/+item-page/edit-item-page/item-withdraw/item-withdraw.component.spec.ts
    #	src/app/+item-page/edit-item-page/simple-item-action/abstract-simple-item-action.component.spec.ts
    #	src/app/+search-page/search-service/search.service.spec.ts
    #	src/app/core/auth/auth-response-parsing.service.spec.ts
    #	src/app/core/auth/auth-response-parsing.service.ts
    #	src/app/core/cache/builders/remote-data-build.service.ts
    #	src/app/core/cache/response-cache.reducer.spec.ts
    #	src/app/core/cache/response-cache.service.spec.ts
    #	src/app/core/cache/response.models.ts
    #	src/app/core/config/config-response-parsing.service.ts
    #	src/app/core/core.effects.ts
    #	src/app/core/core.module.ts
    #	src/app/core/core.reducers.ts
    #	src/app/core/data/base-response-parsing.service.ts
    #	src/app/core/data/data.service.ts
    #	src/app/core/data/item-data.service.spec.ts
    #	src/app/core/data/request.models.ts
    #	src/app/core/data/request.service.spec.ts
    #	src/app/core/data/request.service.ts
    #	src/app/core/integration/integration-response-parsing.service.spec.ts
    #	src/app/core/integration/integration-response-parsing.service.ts
    #	src/app/core/integration/integration.service.ts
    #	src/app/core/metadata/metadata.service.spec.ts
    #	src/app/core/registry/registry.service.spec.ts
    #	src/app/core/shared/hal-endpoint.service.ts
    #	src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-form-control.component.ts
    #	src/app/shared/mocks/mock-response-cache.service.ts
    #	src/app/shared/shared.module.ts
    880d9ed0
item-data.service.spec.ts 5.32 KiB
import { Store } from '@ngrx/store';
import { cold, getTestScheduler } from 'jasmine-marbles';
import { TestScheduler } from 'rxjs/testing';
import { BrowseService } from '../browse/browse.service';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { CoreState } from '../core.reducers';
import { ItemDataService } from './item-data.service';
import { RequestService } from './request.service';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { FindAllOptions, RestRequest } from './request.models';
import { ObjectCacheService } from '../cache/object-cache.service';
import { Observable } from 'rxjs';
import { RestResponse } from '../cache/response.models';
import { NotificationsService } from '../../shared/notifications/notifications.service';
import { NormalizedObjectBuildService } from '../cache/builders/normalized-object-build.service';
import { HttpClient } from '@angular/common/http';
import { RequestEntry } from './request.reducer';
import { of as observableOf } from 'rxjs';

describe('ItemDataService', () => {
  let scheduler: TestScheduler;
  let service: ItemDataService;
  let bs: BrowseService;
  const requestService = {
    generateRequestId(): string {
      return scopeID;
    },
    configure(request: RestRequest) {
      // Do nothing
    },
    getByHref(requestHref: string) {
      const responseCacheEntry = new RequestEntry();
      responseCacheEntry.response = new RestResponse(true, 200, 'OK');
      return observableOf(responseCacheEntry);
    }
  } as RequestService;
  const rdbService = {} as RemoteDataBuildService;

  const store = {} as Store<CoreState>;
  const objectCache = {} as ObjectCacheService;
  const halEndpointService = {
    getEndpoint(linkPath: string): Observable<string> {
      return cold('a', {a: itemEndpoint});
    }
  } as HALEndpointService;

  const scopeID = '4af28e99-6a9c-4036-a199-e1b587046d39';
  const options = Object.assign(new FindAllOptions(), {
    scopeID: scopeID,
    sort: {
      field: '',
      direction: undefined
    }
  });

  const browsesEndpoint = 'https://rest.api/discover/browses';
  const itemBrowseEndpoint = `${browsesEndpoint}/author/items`;
  const scopedEndpoint = `${itemBrowseEndpoint}?scope=${scopeID}`;
  const serviceEndpoint = `https://rest.api/core/items`;
  const browseError = new Error('getBrowseURL failed');
  const notificationsService = {} as NotificationsService;
  const http = {} as HttpClient;
  const comparator = {} as any;
  const dataBuildService = {} as NormalizedObjectBuildService;
  const itemEndpoint = 'https://rest.api/core/items';
  const ScopedItemEndpoint = `https://rest.api/core/items/${scopeID}`;

  function initMockBrowseService(isSuccessful: boolean) {
    const obs = isSuccessful ?
      cold('--a-', { a: itemBrowseEndpoint }) :
      cold('--#-', undefined, browseError);
    return jasmine.createSpyObj('bs', {
      getBrowseURLFor: obs
    });
  }

  function initTestService() {
    return new ItemDataService(
      requestService,
      rdbService,
      dataBuildService,
      store,
      bs,
      objectCache,
      halEndpointService,
      notificationsService,
      http,
      comparator
    );
  }

  describe('getBrowseEndpoint', () => {
    beforeEach(() => {
      scheduler = getTestScheduler();
    });

    it('should return the endpoint to fetch Items within the given scope and starting with the given string', () => {
      bs = initMockBrowseService(true);
      service = initTestService();

      const result = service.getBrowseEndpoint(options);
      const expected = cold('--b-', { b: scopedEndpoint });

      expect(result).toBeObservable(expected);
    });

    describe('if the dc.date.issue browse isn\'t configured for items', () => {
      beforeEach(() => {
        bs = initMockBrowseService(false);
        service = initTestService();
      });
      it('should throw an error', () => {
        const result = service.getBrowseEndpoint(options);
        const expected = cold('--#-', undefined, browseError);

        expect(result).toBeObservable(expected);
      });
    });
  });

  describe('getItemWithdrawEndpoint', () => {
    beforeEach(() => {
      scheduler = getTestScheduler();
      service = initTestService();

    });

    it('should return the endpoint to withdraw and reinstate items', () => {
      const result = service.getItemWithdrawEndpoint(scopeID);
      const expected = cold('a', {a: ScopedItemEndpoint});

      expect(result).toBeObservable(expected);
    });

    it('should setWithDrawn', () => {
      const expected = new RestResponse(true, 200, 'OK');
      const result = service.setWithDrawn(scopeID, true);
      result.subscribe((v) => expect(v).toEqual(expected));

    });
  });

  describe('getItemDiscoverableEndpoint', () => {
    beforeEach(() => {
      scheduler = getTestScheduler();
      service = initTestService();

    });

    it('should return the endpoint to make an item private or public', () => {
      const result = service.getItemDiscoverableEndpoint(scopeID);
      const expected = cold('a', {a: ScopedItemEndpoint});

      expect(result).toBeObservable(expected);
    });

    it('should setDiscoverable', () => {
      const expected = new RestResponse(true, 200, 'OK');
      const result = service.setDiscoverable(scopeID, false);
      result.subscribe((v) => expect(v).toEqual(expected));

    });
  });

});