[PM-6853] Recreate `getUserStateOrDefault$` (#8374)

* Recreate getUserStateOrDefault$

* Update Tests
This commit is contained in:
Justin Baur 2024-03-18 14:59:06 -05:00 committed by GitHub
parent cc28149e60
commit a3f6b9eacb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 18 deletions

View File

@ -1,5 +1,5 @@
import { FakeAccountService, FakeStateProvider, mockAccountServiceWith } from "../../../spec"; 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 { Utils } from "../../platform/misc/utils";
import { UserId } from "../../types/guid"; import { UserId } from "../../types/guid";
import { ProviderUserStatusType, ProviderUserType } from "../enums"; import { ProviderUserStatusType, ProviderUserType } from "../enums";
@ -77,11 +77,13 @@ describe("ProviderService", () => {
const fakeUserId = Utils.newGuid() as UserId; const fakeUserId = Utils.newGuid() as UserId;
let fakeAccountService: FakeAccountService; let fakeAccountService: FakeAccountService;
let fakeStateProvider: FakeStateProvider; let fakeStateProvider: FakeStateProvider;
let fakeUserState: FakeSingleUserState<Record<string, ProviderData>>;
let fakeActiveUserState: FakeActiveUserState<Record<string, ProviderData>>; let fakeActiveUserState: FakeActiveUserState<Record<string, ProviderData>>;
beforeEach(async () => { beforeEach(async () => {
fakeAccountService = mockAccountServiceWith(fakeUserId); fakeAccountService = mockAccountServiceWith(fakeUserId);
fakeStateProvider = new FakeStateProvider(fakeAccountService); fakeStateProvider = new FakeStateProvider(fakeAccountService);
fakeUserState = fakeStateProvider.singleUser.getFake(fakeUserId, PROVIDERS);
fakeActiveUserState = fakeStateProvider.activeUser.getFake(PROVIDERS); fakeActiveUserState = fakeStateProvider.activeUser.getFake(PROVIDERS);
providerService = new ProviderService(fakeStateProvider); providerService = new ProviderService(fakeStateProvider);
}); });
@ -89,7 +91,7 @@ describe("ProviderService", () => {
describe("getAll()", () => { describe("getAll()", () => {
it("Returns an array of all providers stored in state", async () => { it("Returns an array of all providers stored in state", async () => {
const mockData: ProviderData[] = buildMockProviders(5); const mockData: ProviderData[] = buildMockProviders(5);
fakeActiveUserState.nextState(arrayToRecord(mockData)); fakeUserState.nextState(arrayToRecord(mockData));
const providers = await providerService.getAll(); const providers = await providerService.getAll();
expect(providers).toHaveLength(5); expect(providers).toHaveLength(5);
expect(providers).toEqual(mockData.map((x) => new Provider(x))); 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 () => { it("Returns an empty array if no providers are found in state", async () => {
const mockData: ProviderData[] = undefined; const mockData: ProviderData[] = undefined;
fakeActiveUserState.nextState(arrayToRecord(mockData)); fakeUserState.nextState(arrayToRecord(mockData));
const result = await providerService.getAll(); const result = await providerService.getAll();
expect(result).toEqual([]); expect(result).toEqual([]);
}); });
@ -106,7 +108,7 @@ describe("ProviderService", () => {
describe("get()", () => { describe("get()", () => {
it("Returns a single provider from state that matches the specified id", async () => { it("Returns a single provider from state that matches the specified id", async () => {
const mockData = buildMockProviders(5); const mockData = buildMockProviders(5);
fakeActiveUserState.nextState(arrayToRecord(mockData)); fakeUserState.nextState(arrayToRecord(mockData));
const result = await providerService.get(mockData[3].id); const result = await providerService.get(mockData[3].id);
expect(result).toEqual(new Provider(mockData[3])); expect(result).toEqual(new Provider(mockData[3]));
}); });
@ -120,15 +122,12 @@ describe("ProviderService", () => {
describe("save()", () => { describe("save()", () => {
it("replaces the entire provider list in state for the active user", async () => { it("replaces the entire provider list in state for the active user", async () => {
const originalData = buildMockProviders(10); const originalData = buildMockProviders(10);
fakeActiveUserState.nextState(arrayToRecord(originalData)); fakeUserState.nextState(arrayToRecord(originalData));
const newData = buildMockProviders(10, "newData"); const newData = arrayToRecord(buildMockProviders(10, "newData"));
await providerService.save(arrayToRecord(newData)); await providerService.save(newData);
const result = await providerService.getAll(); expect(fakeActiveUserState.nextMock).toHaveBeenCalledWith([fakeUserId, newData]);
expect(result).toEqual(newData);
expect(result).not.toEqual(originalData);
}); });
// This is more or less a test for logouts // This is more or less a test for logouts
@ -136,9 +135,8 @@ describe("ProviderService", () => {
const originalData = buildMockProviders(2); const originalData = buildMockProviders(2);
fakeActiveUserState.nextState(arrayToRecord(originalData)); fakeActiveUserState.nextState(arrayToRecord(originalData));
await providerService.save(null); await providerService.save(null);
const result = await providerService.getAll();
expect(result).toEqual([]); expect(fakeActiveUserState.nextMock).toHaveBeenCalledWith([fakeUserId, null]);
expect(result).not.toEqual(originalData);
}); });
}); });
}); });

View File

@ -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 { KeyDefinition, PROVIDERS_DISK, StateProvider } from "../../platform/state";
import { UserId } from "../../types/guid"; import { UserId } from "../../types/guid";
@ -18,9 +18,17 @@ export class ProviderService implements ProviderServiceAbstraction {
constructor(private stateProvider: StateProvider) {} constructor(private stateProvider: StateProvider) {}
private providers$(userId?: UserId): Observable<Provider[] | undefined> { private providers$(userId?: UserId): Observable<Provider[] | undefined> {
return this.stateProvider // FIXME: Can be replaced with `getUserStateOrDefault$` if we weren't trying to pick this.
.getUserState$(PROVIDERS, userId) return (
.pipe(this.mapProviderRecordToArray()); 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() { private mapProviderRecordToArray() {