Skip to content
Snippets Groups Projects
Commit 4e72a568 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-5012 make injectable/inject PostfachResourceService

parent a5ad5b37
No related branches found
No related tags found
No related merge requests found
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TechSharedModule } from '@alfa-client/tech-shared';
import { ResourceRepository, TechSharedModule } from '@alfa-client/tech-shared';
import { NavigationItemComponent } from './shared/navigation-item/navigation-item.component';
import { RouterModule } from '@angular/router';
import { ReactiveFormsModule } from '@angular/forms';
......@@ -20,6 +20,10 @@ import { NavigationComponent } from './navigation/navigation.component';
import { OrganisationseinheitNavigationItemComponent } from './organisationseinheit/organisationseinheit-navigation-item/organisationseinheit-navigation-item.component';
import KcAdminClient from '@keycloak/keycloak-admin-client';
import { Environment, ENVIRONMENT_CONFIG } from '@alfa-client/environment-shared';
import {
PostfachResourceService,
postfachResourceServiceFactory,
} from './postfach/postfach-resource.service';
@NgModule({
declarations: [
......@@ -56,6 +60,11 @@ import { Environment, ENVIRONMENT_CONFIG } from '@alfa-client/environment-shared
}),
deps: [ENVIRONMENT_CONFIG],
},
{
provide: PostfachResourceService,
useFactory: postfachResourceServiceFactory,
deps: [ResourceRepository, SettingsService],
},
],
})
export class AdminSettingsModule {}
import { ApiResourceService, ResourceRepository } from '@alfa-client/tech-shared';
import { PostfachResource } from './postfach.model';
import { SettingsService } from '../admin-settings.service';
import { PostfachLinkRel } from './postfach.linkrel';
export class PostfachResourceService extends ApiResourceService<
PostfachResource,
PostfachResource
> {}
export function postfachResourceServiceFactory(
repository: ResourceRepository,
settingService: SettingsService,
) {
return new ApiResourceService(buildConfig(settingService), repository);
}
function buildConfig(settingService: SettingsService) {
return {
resource: settingService.getPostfach(),
getLinkRel: PostfachLinkRel.SELF,
editLinkRel: PostfachLinkRel.SELF,
};
}
import { Mock, mock, useFromMock } from '@alfa-client/test-utils';
import { Mock, useFromMock } from '@alfa-client/test-utils';
import { PostfachResource, PostfachSettingsItem } from './postfach.model';
import { PostfachService } from './postfach.service';
import { SettingsService } from '../admin-settings.service';
import { createPostfachResource, createPostfachSettingItem } from '../../../test/postfach/postfach';
import {
createStateResource,
HttpError,
ResourceRepository,
ResourceServiceConfig,
StateResource,
} from '@alfa-client/tech-shared';
import { Observable, of } from 'rxjs';
import { createStateResource, HttpError, StateResource } from '@alfa-client/tech-shared';
import { Observable } from 'rxjs';
import { singleCold, singleHot } from '../../../../tech-shared/test/marbles';
import { PostfachLinkRel } from './postfach.linkrel';
import { PostfachResourceService } from './postfach-resource.service';
import { mockResourceService } from '../../../../tech-shared/test/resource';
describe('PostfachService', () => {
let service: PostfachService;
let settingsService: Mock<SettingsService>;
let repository: Mock<ResourceRepository>;
const postfachStateResource$: Observable<StateResource<PostfachResource>> = of(
createStateResource(createPostfachResource()),
);
let resourceService: Mock<PostfachResourceService>;
beforeEach(() => {
repository = mock(ResourceRepository);
settingsService = mock(SettingsService);
settingsService.getPostfach.mockReturnValue(postfachStateResource$);
resourceService = mockResourceService(PostfachResourceService);
service = new PostfachService(useFromMock(settingsService), useFromMock(repository));
service = new PostfachService(useFromMock(resourceService));
});
it('should create', () => {
expect(service).toBeTruthy();
});
it('should create resourceService', () => {
expect(service.resourceService).toBeTruthy();
});
describe('build config', () => {
it('should have resource', () => {
const config: ResourceServiceConfig<PostfachResource> = service.buildConfig();
expect(config.resource).toBe(postfachStateResource$);
});
it('should have editLinkRel', () => {
const config: ResourceServiceConfig<PostfachResource> = service.buildConfig();
expect(config.editLinkRel).toBe(PostfachLinkRel.SELF);
});
it('should have getLinkRel', () => {
const config: ResourceServiceConfig<PostfachResource> = service.buildConfig();
expect(config.getLinkRel).toBe(PostfachLinkRel.SELF);
});
});
describe('get', () => {
const postfachResource: PostfachResource = createPostfachResource();
const postfachStateResource: StateResource<PostfachResource> =
createStateResource(postfachResource);
it('should call resourceservice get', () => {
service.resourceService.get = jest.fn();
service.get();
expect(service.resourceService.get).toHaveBeenCalled();
expect(resourceService.get).toHaveBeenCalled();
});
it('should reurn value', () => {
service.resourceService.get = jest.fn().mockReturnValue(singleCold(postfachStateResource));
resourceService.get.mockReturnValue(singleCold(postfachStateResource));
const returnedPostfachResource: Observable<StateResource<PostfachResource | HttpError>> =
service.get();
......@@ -86,18 +47,16 @@ describe('PostfachService', () => {
const postfachSettingsItem: PostfachSettingsItem = createPostfachSettingItem();
it('should call resourceService', () => {
service.resourceService.save = jest.fn();
service.save(postfachSettingsItem.settingBody);
expect(service.resourceService.save).toHaveBeenCalledWith(postfachSettingsItem);
expect(resourceService.save).toHaveBeenCalledWith(postfachSettingsItem);
});
it('should return saved value', () => {
const postfachResource: PostfachResource = createPostfachResource();
const postfachStateResource: StateResource<PostfachResource> =
createStateResource(postfachResource);
service.resourceService.save = jest.fn().mockReturnValue(singleCold(postfachStateResource));
resourceService.save.mockReturnValue(singleCold(postfachStateResource));
const savedPostfach: Observable<StateResource<PostfachResource | HttpError>> = service.save(
postfachResource.settingBody,
......
import { Injectable } from '@angular/core';
import { Postfach, PostfachResource, PostfachSettingsItem } from './postfach.model';
import { SettingsService } from '../admin-settings.service';
import { Observable } from 'rxjs';
import {
ApiResourceService,
HttpError,
ResourceRepository,
ResourceServiceConfig,
StateResource,
} from '@alfa-client/tech-shared';
import { HttpError, StateResource } from '@alfa-client/tech-shared';
import { SettingName } from '../admin-settings.model';
import { PostfachLinkRel } from './postfach.linkrel';
import { PostfachResourceService } from './postfach-resource.service';
@Injectable()
export class PostfachService {
resourceService: ApiResourceService<PostfachResource, PostfachResource>;
constructor(
private settingsService: SettingsService,
repository: ResourceRepository,
) {
this.resourceService = new ApiResourceService(this.buildConfig(), repository);
}
buildConfig(): ResourceServiceConfig<PostfachResource> {
return {
resource: this.settingsService.getPostfach(),
editLinkRel: PostfachLinkRel.SELF,
getLinkRel: PostfachLinkRel.SELF,
};
}
constructor(private resourceService: PostfachResourceService) {}
public get(): Observable<StateResource<PostfachResource>> {
return this.resourceService.get();
......
......@@ -27,6 +27,8 @@ import { times, zipObject } from 'lodash-es';
import { ListResource } from '../src/lib/resource/resource.util';
import { isNotUndefined } from '../src/lib/tech.util';
import { createDummy, DummyListLinkRel } from './dummy';
import { mock, Mock } from '../../test-utils/src/lib/mocking';
import { Type } from '@angular/core';
// @ts-ignore
export function toResource<R = T & Resource, T = any>(
......@@ -82,3 +84,7 @@ export function createFilledDummyListResource(
[DummyListLinkRel.LIST]: resources,
});
}
export function mockResourceService<T>(service: Type<T>): Mock<T> {
return <Mock<T>>{ ...mock(service), get: jest.fn(), save: jest.fn() };
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment