diff --git a/goofy-client/libs/user-settings-shared/src/lib/user-settings.service.spec.ts b/goofy-client/libs/user-settings-shared/src/lib/user-settings.service.spec.ts index b404b3fe44053f9c124defab870a265a630b66aa..2df35c2dce1d5dc32814f7ecd2d75af9a279ba75 100644 --- a/goofy-client/libs/user-settings-shared/src/lib/user-settings.service.spec.ts +++ b/goofy-client/libs/user-settings-shared/src/lib/user-settings.service.spec.ts @@ -1,28 +1,65 @@ +import { createEmptyStateResource, createStateResource } from '@goofy-client/tech-shared'; import { mock, Mock, useFromMock } from '@goofy-client/test-utils'; -import { UserProfileService } from '@goofy-client/user-profile-shared'; +import { UserProfileResource, UserProfileService } from '@goofy-client/user-profile-shared'; +import { cold, hot } from 'jest-marbles'; +import { createUserProfileResource } from 'libs/user-profile-shared/test/user-profile'; +import { createUserSettingsResource } from 'libs/user-settings-shared/test/user-settings'; import { UserSettingsFacade } from './+state/user-settings.facade'; import { UserSettingsService } from './user-settings.service'; describe('UserSettingsService', () => { - let service: UserSettingsService; let facade: Mock<UserSettingsFacade>; + let userProfileService: Mock<UserProfileService>; - const userProfileService = mock(UserProfileService); + let service: UserSettingsService; beforeEach(() => { facade = mock(UserSettingsFacade); + userProfileService = mock(UserProfileService); + service = new UserSettingsService(useFromMock(facade), useFromMock(userProfileService)); }); - xit('should be created', () => { + it('should be created', () => { expect(service).toBeTruthy(); }); - xdescribe('user settings', () => { - it('get settings for current user', () => { - const userSettings = service.getUserSettings(); + describe('get user settings', () => { + const userProfileResource: UserProfileResource = createUserProfileResource(); + const stateResourceCU = createStateResource(createUserSettingsResource()); + const emptyStateResourceCU = createEmptyStateResource<UserProfileResource>(true); + + beforeEach(() => { + const stateResourceUP = createStateResource(userProfileResource); + userProfileService.getCurrentUser.mockReturnValue(hot('a', {a: stateResourceUP})); + + facade.getUserSettings.mockReturnValue(hot('-a', {a: stateResourceCU})); + }) + + it('should call userProfileService.getCurrentUser', () => { + service.getUserSettings(); + + expect(userProfileService.getCurrentUser).toHaveBeenCalled(); + }) + + it.skip('should load user settings from facade', () => { + // Prüfen dass sowie womit aufgerufen wird + service.getUserSettings(); + + expect(facade.loadUserSettings).toHaveBeenCalledWith(userProfileResource); + }) + + it('should return user settings', () => { + const userSettings$ = service.getUserSettings(); + + expect(userSettings$).toBeObservable(cold('ab', {a: emptyStateResourceCU, b: stateResourceCU})); + }) + + }) - expect(userSettings).toBe(undefined); + describe('set UserSettings.notificationsSendFor', () => { + it('should call patch method in facade', () => { + // Naming an die Implementierung anpassen/spezifizieren }) }) }); diff --git a/goofy-client/libs/user-settings-shared/src/lib/user-settings.service.ts b/goofy-client/libs/user-settings-shared/src/lib/user-settings.service.ts index d3c18499cfca5dd91b3cb4512b124def56a18870..ee262b3ede1bf664481523502f618923d30b55ee 100644 --- a/goofy-client/libs/user-settings-shared/src/lib/user-settings.service.ts +++ b/goofy-client/libs/user-settings-shared/src/lib/user-settings.service.ts @@ -1,8 +1,8 @@ import { Injectable } from '@angular/core'; -import { StateResource } from '@goofy-client/tech-shared'; +import { createEmptyStateResource, doIfLoadingRequired, StateResource } from '@goofy-client/tech-shared'; import { UserProfileService } from '@goofy-client/user-profile-shared'; -import { Observable } from 'rxjs'; -import { first } from 'rxjs/operators'; +import { combineLatest, Observable } from 'rxjs'; +import { map, startWith, tap } from 'rxjs/operators'; import { UserSettingsFacade } from './+state/user-settings.facade'; import { UserSettingsResource } from './user-settings.model'; @Injectable({ @@ -13,12 +13,18 @@ export class UserSettingsService { constructor( private userSettingsFacade: UserSettingsFacade, private userProfileService: UserProfileService, - ) { - // TODO Kein schöner Ort - geht das eleganter? - this.userProfileService.getCurrentUser().pipe(first()).subscribe(); - } + ) { } getUserSettings(): Observable<StateResource<UserSettingsResource>> { - return this.userSettingsFacade.getUserSettings(); + const userProfile$ = this.userProfileService.getCurrentUser(); + const userSetting$ = this.userSettingsFacade.getUserSettings(); + + return combineLatest([userProfile$, userSetting$]).pipe( + tap(([userProfile, userSettings]) => doIfLoadingRequired(userSettings, () => { + this.userSettingsFacade.loadUserSettings(userProfile.resource) + })), + map(([, userSettings]) => userSettings), + startWith(createEmptyStateResource<UserSettingsResource>(true)) + ); } }