Select Git revision
resource.service.spec.ts 18.04 KiB
import { Mock, mock, useFromMock } from '@alfa-client/test-utils';
import { Observable, lastValueFrom, of, throwError } from 'rxjs';
import { ResourceService } from './resource.service';
import { ResourceRepository } from './resource.repository';
import { LinkRelationName, ResourceServiceConfig, SaveResourceData } from './resource.model';
import { Resource, getUrl } from '@ngxp/rest';
import { createDummyResource } from 'libs/tech-shared/test/resource';
import {
StateResource,
createEmptyStateResource,
createErrorStateResource,
createStateResource,
} from './resource.util';
import { fakeAsync, tick } from '@angular/core/testing';
import { singleCold, singleHot } from './marbles';
import { HttpErrorResponse } from '@angular/common/http';
import { createProblemDetail } from 'libs/tech-shared/test/error';
import { HttpError, ProblemDetail } from '../tech.model';
import { cold } from 'jest-marbles';
import * as ResourceUtil from './resource.util';
describe('ResourceService', () => {
let service: ResourceService<Resource, Resource>;
let config: ResourceServiceConfig<Resource>;
let repository: Mock<ResourceRepository>;
const configResource: Resource = createDummyResource();
const configStateResource: StateResource<Resource> = createStateResource(configResource);
const configStateResource$: Observable<StateResource<Resource>> = of(configStateResource);
const deleteLinkRel: string = 'dummyDeleteLinkRel';
const editLinkRel: string = 'dummyEditLinkRel';
const getLinkRel: LinkRelationName = 'dummyGetLinkRel';
beforeEach(() => {
config = {
resource: configStateResource$,
getLinkRel,
deleteLinkRel,
editLinkRel,
};
repository = mock(ResourceRepository);
service = new ResourceService(config, useFromMock(repository));
});
it('should be created', () => {
expect(service).toBeTruthy();
});
describe('get', () => {
const stateResource: StateResource<Resource> = createStateResource(configResource);
beforeEach(() => {
service.stateResource.next(stateResource);
service.handleNullConfigResource = jest.fn();
service.handleConfigResource = jest.fn();
service.handleConfigResourceChanged = jest.fn();
service.shouldFilter = jest.fn().mockReturnValue(true);
});
it('should handle config resource changed', fakeAsync(() => {
service.get().subscribe();
tick();
expect(service.handleConfigResourceChanged).toHaveBeenCalledWith(configResource);
}));