Skip to content
Snippets Groups Projects
Commit 036655c0 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-3560 adjust unittest; tiny code cleanup

parent 27edd490
No related branches found
No related tags found
No related merge requests found
......@@ -22,13 +22,14 @@
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
import { faker } from '@faker-js/faker';
import { ApiRootService } from '@goofy-client/api-root-shared';
import { ApiRootResource, ApiRootService } from '@goofy-client/api-root-shared';
import { CommandListResource, CommandResource, CommandService } from '@goofy-client/command-shared';
import { NavigationService } from '@goofy-client/navigation-shared';
import { createEmptyStateResource, createStateResource, StateResource } from '@goofy-client/tech-shared';
import { StateResource, createEmptyStateResource, createStateResource } from '@goofy-client/tech-shared';
import { Mock, mock, useFromMock } from '@goofy-client/test-utils';
import { getUrl, ResourceUri } from '@ngxp/rest';
import { cold } from 'jest-marbles';
import { ResourceUri, getUrl } from '@ngxp/rest';
import { cold, hot } from 'jest-marbles';
import { createApiRootResource } from 'libs/api-root-shared/test/api-root';
import { createBinaryFileListResource } from 'libs/binary-file-shared/test/binary-file';
import { CommandLinkRel } from 'libs/command-shared/src/lib/command.linkrel';
import { createCommandListResource, createCommandResource } from 'libs/command-shared/test/command';
......@@ -61,30 +62,69 @@ describe('VorgangService', () => {
service = new VorgangService(useFromMock(repository), useFromMock(navigationService), useFromMock(commandService), useFromMock(vorgangFacade), useFromMock(apiRootService));
})
describe('get vorgang with eingang', () => {
describe('getVorgangWithEingang', () => {
const vorgangWithEingang: VorgangWithEingangResource = createVorgangWithEingangResource();
const url: ResourceUri = getUrl(vorgangWithEingang);
const vorgangWithEingangStateResource: StateResource<VorgangWithEingangResource> = createStateResource(vorgangWithEingang);
const apiRootStateResource: StateResource<ApiRootResource> = createStateResource(createApiRootResource());
beforeEach(() => {
repository.getVorgang.mockReturnValue(of(vorgangWithEingang));
service.setStateResourceOnLoading = jest.fn();
apiRootService.getApiRoot.mockReturnValue(of(apiRootStateResource));
})
it('should call service.setVorgangWithEingang', () => {
const spy = jest.spyOn(service, 'setVorgangWithEingang');
const resource = createStateResource(vorgangWithEingang);
it('should get vorgangWithEingang', () => {
service.getVorgangAsObservable = jest.fn();
service.loadVorgangWithEingang(url);
service.getVorgangWithEingang();
expect(spy).toHaveBeenCalledWith(resource);
expect(service.getVorgangAsObservable).toBeCalled();
})
it('should call apiRootService.getApiRoot', () => {
it('should get getApiRoot', () => {
service.getVorgangWithEingang();
expect(apiRootService.getApiRoot).toBeCalled();
})
describe('initial', () => {
beforeEach(() => {
apiRootService.getApiRoot.mockReturnValue(hot('-a', { a: apiRootStateResource }));
service.vorgangWithEingang$.next(vorgangWithEingangStateResource);
})
it('should return value', () => {
const vorgangList = service.getVorgangWithEingang();
expect(vorgangList).toBeObservable(cold('ab', { a: createEmptyStateResource(true), b: vorgangWithEingangStateResource }));
})
})
describe('loadVorgangWithEingang', () => {
beforeEach(() => {
service.loadVorgangWithEingang = jest.fn();
})
it('should be called if loading required', () => {
service.vorgangWithEingang$.next(createEmptyStateResource());
service.getVorgangWithEingang().subscribe();
expect(service.loadVorgangWithEingang).toHaveBeenCalled();
})
it('should NOT be called if already loaded', () => {
service.vorgangWithEingang$.next(vorgangWithEingangStateResource);
service.getVorgangWithEingang().subscribe();
expect(service.loadVorgangWithEingang).not.toHaveBeenCalled();
})
})
})
describe('loadVorgangWithEingang', () => {
......
......@@ -28,10 +28,10 @@ import { BinaryFileListResource } from '@goofy-client/binary-file-shared';
import { CommandListResource, CommandResource, CommandService, getPendingCommandByOrder, hasError, isDone } from '@goofy-client/command-shared';
import { NavigationService } from '@goofy-client/navigation-shared';
import { PostfachOrder } from '@goofy-client/postfach-shared';
import { createEmptyStateResource, createStateResource, doIfLoadingRequired, isNotNil, isNotNull, isNotUndefined, StateResource } from '@goofy-client/tech-shared';
import { getUrl, hasLink, ResourceUri } from '@ngxp/rest';
import { StateResource, createEmptyStateResource, createStateResource, doIfLoadingRequired, isNotNil, isNotNull, isNotUndefined } from '@goofy-client/tech-shared';
import { ResourceUri, getUrl, hasLink } from '@ngxp/rest';
import { CommandLinkRel } from 'libs/command-shared/src/lib/command.linkrel';
import { BehaviorSubject, combineLatest, Observable, Subscription } from 'rxjs';
import { BehaviorSubject, Observable, Subscription, combineLatest } from 'rxjs';
import { first, map, startWith, tap } from 'rxjs/operators';
import { VorgangFacade } from './+state/vorgang.facade';
import { VorgangRepository } from './+state/vorgang.repository';
......@@ -67,21 +67,23 @@ export class VorgangService {
}
public getVorgangWithEingang(): Observable<StateResource<VorgangWithEingangResource>> {
return combineLatest([this.vorgangWithEingang$, this.apiRootService.getApiRoot()]).pipe(
return combineLatest([this.getVorgangAsObservable(), this.apiRootService.getApiRoot()]).pipe(
tap(([vorgangWithEingang, apiRoot]) => {
if(isNotNull(apiRoot.resource)){
doIfLoadingRequired(vorgangWithEingang, () => this.loadVorgangWithEingang(this.getVorgangWithEingangUrl()));
}
if(isNotNull(apiRoot.resource)) doIfLoadingRequired(vorgangWithEingang, () => this.loadVorgangWithEingang(this.getVorgangWithEingangUrl()));
}),
map(([vorgangWithEingang,]) => vorgangWithEingang),
startWith(createEmptyStateResource<VorgangWithEingangResource>(true)));
}
getVorgangAsObservable(): Observable<StateResource<VorgangWithEingangResource>>{
return this.vorgangWithEingang$.asObservable();
}
public getVorgang(): VorgangWithEingangResource {
return this.vorgangWithEingang$.value.resource;
}
private getVorgangWithEingangUrl(): ResourceUri | undefined {
private getVorgangWithEingangUrl(): ResourceUri {
return this.navigationService.getDecodedParam(VorgangService.VORGANG_WITH_EINGANG_URL);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment