1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-10 05:30:01 +00:00

fromJSON should return null if the cache is empty

This commit is contained in:
Alec Rippberger
2025-04-15 15:52:49 -05:00
parent 07035a1b58
commit b11b950ef5
4 changed files with 54 additions and 2 deletions

View File

@@ -11,6 +11,24 @@ import {
TwoFactorAuthEmailComponentCacheService,
} from "./two-factor-auth-email-cache.service";
describe("TwoFactorAuthEmailCache", () => {
describe("fromJSON", () => {
it("returns null when input is null", () => {
const result = TwoFactorAuthEmailCache.fromJSON(null as any);
expect(result).toBeNull();
});
it("creates a TwoFactorAuthEmailCache instance from valid JSON", () => {
const jsonData = { emailSent: true };
const result = TwoFactorAuthEmailCache.fromJSON(jsonData);
expect(result).not.toBeNull();
expect(result).toBeInstanceOf(TwoFactorAuthEmailCache);
expect(result?.emailSent).toBe(true);
});
});
});
describe("TwoFactorAuthEmailComponentCacheService", () => {
let service: TwoFactorAuthEmailComponentCacheService;
let mockViewCacheService: MockProxy<ViewCacheService>;

View File

@@ -13,7 +13,12 @@ const TWO_FACTOR_AUTH_EMAIL_CACHE_KEY = "two-factor-auth-email-cache";
export class TwoFactorAuthEmailCache {
emailSent: boolean = false;
static fromJSON(obj: Partial<Jsonify<TwoFactorAuthEmailCache>>): TwoFactorAuthEmailCache {
static fromJSON(obj: Partial<Jsonify<TwoFactorAuthEmailCache>>): TwoFactorAuthEmailCache | null {
// Return null if the cache is empty
if (obj == null) {
return null;
}
return Object.assign(new TwoFactorAuthEmailCache(), obj);
}
}