[PM-6853] Recreate `getUserStateOrDefault$` (#8374)
* Recreate getUserStateOrDefault$ * Update Tests
This commit is contained in:
parent
cc28149e60
commit
a3f6b9eacb
|
@ -1,5 +1,5 @@
|
|||
import { FakeAccountService, FakeStateProvider, mockAccountServiceWith } from "../../../spec";
|
||||
import { FakeActiveUserState } from "../../../spec/fake-state";
|
||||
import { FakeActiveUserState, FakeSingleUserState } from "../../../spec/fake-state";
|
||||
import { Utils } from "../../platform/misc/utils";
|
||||
import { UserId } from "../../types/guid";
|
||||
import { ProviderUserStatusType, ProviderUserType } from "../enums";
|
||||
|
@ -77,11 +77,13 @@ describe("ProviderService", () => {
|
|||
const fakeUserId = Utils.newGuid() as UserId;
|
||||
let fakeAccountService: FakeAccountService;
|
||||
let fakeStateProvider: FakeStateProvider;
|
||||
let fakeUserState: FakeSingleUserState<Record<string, ProviderData>>;
|
||||
let fakeActiveUserState: FakeActiveUserState<Record<string, ProviderData>>;
|
||||
|
||||
beforeEach(async () => {
|
||||
fakeAccountService = mockAccountServiceWith(fakeUserId);
|
||||
fakeStateProvider = new FakeStateProvider(fakeAccountService);
|
||||
fakeUserState = fakeStateProvider.singleUser.getFake(fakeUserId, PROVIDERS);
|
||||
fakeActiveUserState = fakeStateProvider.activeUser.getFake(PROVIDERS);
|
||||
providerService = new ProviderService(fakeStateProvider);
|
||||
});
|
||||
|
@ -89,7 +91,7 @@ describe("ProviderService", () => {
|
|||
describe("getAll()", () => {
|
||||
it("Returns an array of all providers stored in state", async () => {
|
||||
const mockData: ProviderData[] = buildMockProviders(5);
|
||||
fakeActiveUserState.nextState(arrayToRecord(mockData));
|
||||
fakeUserState.nextState(arrayToRecord(mockData));
|
||||
const providers = await providerService.getAll();
|
||||
expect(providers).toHaveLength(5);
|
||||
expect(providers).toEqual(mockData.map((x) => new Provider(x)));
|
||||
|
@ -97,7 +99,7 @@ describe("ProviderService", () => {
|
|||
|
||||
it("Returns an empty array if no providers are found in state", async () => {
|
||||
const mockData: ProviderData[] = undefined;
|
||||
fakeActiveUserState.nextState(arrayToRecord(mockData));
|
||||
fakeUserState.nextState(arrayToRecord(mockData));
|
||||
const result = await providerService.getAll();
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
@ -106,7 +108,7 @@ describe("ProviderService", () => {
|
|||
describe("get()", () => {
|
||||
it("Returns a single provider from state that matches the specified id", async () => {
|
||||
const mockData = buildMockProviders(5);
|
||||
fakeActiveUserState.nextState(arrayToRecord(mockData));
|
||||
fakeUserState.nextState(arrayToRecord(mockData));
|
||||
const result = await providerService.get(mockData[3].id);
|
||||
expect(result).toEqual(new Provider(mockData[3]));
|
||||
});
|
||||
|
@ -120,15 +122,12 @@ describe("ProviderService", () => {
|
|||
describe("save()", () => {
|
||||
it("replaces the entire provider list in state for the active user", async () => {
|
||||
const originalData = buildMockProviders(10);
|
||||
fakeActiveUserState.nextState(arrayToRecord(originalData));
|
||||
fakeUserState.nextState(arrayToRecord(originalData));
|
||||
|
||||
const newData = buildMockProviders(10, "newData");
|
||||
await providerService.save(arrayToRecord(newData));
|
||||
const newData = arrayToRecord(buildMockProviders(10, "newData"));
|
||||
await providerService.save(newData);
|
||||
|
||||
const result = await providerService.getAll();
|
||||
|
||||
expect(result).toEqual(newData);
|
||||
expect(result).not.toEqual(originalData);
|
||||
expect(fakeActiveUserState.nextMock).toHaveBeenCalledWith([fakeUserId, newData]);
|
||||
});
|
||||
|
||||
// This is more or less a test for logouts
|
||||
|
@ -136,9 +135,8 @@ describe("ProviderService", () => {
|
|||
const originalData = buildMockProviders(2);
|
||||
fakeActiveUserState.nextState(arrayToRecord(originalData));
|
||||
await providerService.save(null);
|
||||
const result = await providerService.getAll();
|
||||
expect(result).toEqual([]);
|
||||
expect(result).not.toEqual(originalData);
|
||||
|
||||
expect(fakeActiveUserState.nextMock).toHaveBeenCalledWith([fakeUserId, null]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Observable, map, firstValueFrom } from "rxjs";
|
||||
import { Observable, map, firstValueFrom, of, switchMap, take } from "rxjs";
|
||||
|
||||
import { KeyDefinition, PROVIDERS_DISK, StateProvider } from "../../platform/state";
|
||||
import { UserId } from "../../types/guid";
|
||||
|
@ -18,9 +18,17 @@ export class ProviderService implements ProviderServiceAbstraction {
|
|||
constructor(private stateProvider: StateProvider) {}
|
||||
|
||||
private providers$(userId?: UserId): Observable<Provider[] | undefined> {
|
||||
return this.stateProvider
|
||||
.getUserState$(PROVIDERS, userId)
|
||||
.pipe(this.mapProviderRecordToArray());
|
||||
// FIXME: Can be replaced with `getUserStateOrDefault$` if we weren't trying to pick this.
|
||||
return (
|
||||
userId != null
|
||||
? this.stateProvider.getUser(userId, PROVIDERS).state$
|
||||
: this.stateProvider.activeUserId$.pipe(
|
||||
take(1),
|
||||
switchMap((userId) =>
|
||||
userId != null ? this.stateProvider.getUser(userId, PROVIDERS).state$ : of(null),
|
||||
),
|
||||
)
|
||||
).pipe(this.mapProviderRecordToArray());
|
||||
}
|
||||
|
||||
private mapProviderRecordToArray() {
|
||||
|
|
Loading…
Reference in New Issue