2022-08-16 14:05:03 +02:00
|
|
|
import { BehaviorSubject } from "rxjs";
|
|
|
|
|
2023-01-12 21:39:33 +01:00
|
|
|
import { AbstractMemoryStorageService } from "@bitwarden/common/abstractions/storage.service";
|
|
|
|
import { MemoryStorageService } from "@bitwarden/common/services/memoryStorage.service";
|
|
|
|
|
2022-11-23 23:26:57 +01:00
|
|
|
import { BrowserStateService } from "../../services/browser-state.service";
|
2022-08-16 14:05:03 +02:00
|
|
|
|
|
|
|
import { browserSession } from "./browser-session.decorator";
|
|
|
|
import { SessionStorable } from "./session-storable";
|
|
|
|
import { sessionSync } from "./session-sync.decorator";
|
|
|
|
|
|
|
|
// browserSession initializes SessionSyncers for each sessionSync decorated property
|
|
|
|
// We don't want to test SessionSyncers, so we'll mock them
|
|
|
|
jest.mock("./session-syncer");
|
|
|
|
|
|
|
|
describe("browserSession decorator", () => {
|
2023-01-12 21:39:33 +01:00
|
|
|
it("should throw if neither StateService nor MemoryStorageService is a constructor argument", () => {
|
2022-08-16 14:05:03 +02:00
|
|
|
@browserSession
|
|
|
|
class TestClass {}
|
|
|
|
expect(() => {
|
|
|
|
new TestClass();
|
|
|
|
}).toThrowError(
|
2023-01-12 21:39:33 +01:00
|
|
|
"Cannot decorate TestClass with browserSession, Browser's AbstractMemoryStorageService must be accessible through the observed classes parameters"
|
2022-08-16 14:05:03 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create if StateService is a constructor argument", () => {
|
2023-01-12 21:39:33 +01:00
|
|
|
const stateService = Object.create(BrowserStateService.prototype, {
|
|
|
|
memoryStorageService: {
|
|
|
|
value: Object.create(MemoryStorageService.prototype, {
|
|
|
|
type: { value: MemoryStorageService.TYPE },
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
2022-08-16 14:05:03 +02:00
|
|
|
|
|
|
|
@browserSession
|
|
|
|
class TestClass {
|
2022-11-23 23:26:57 +01:00
|
|
|
constructor(private stateService: BrowserStateService) {}
|
2022-08-16 14:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(new TestClass(stateService)).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2023-01-12 21:39:33 +01:00
|
|
|
it("should create if MemoryStorageService is a constructor argument", () => {
|
|
|
|
const memoryStorageService = Object.create(MemoryStorageService.prototype, {
|
|
|
|
type: { value: MemoryStorageService.TYPE },
|
|
|
|
});
|
|
|
|
|
|
|
|
@browserSession
|
|
|
|
class TestClass {
|
|
|
|
constructor(private memoryStorageService: AbstractMemoryStorageService) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(new TestClass(memoryStorageService)).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2022-08-16 14:05:03 +02:00
|
|
|
describe("interaction with @sessionSync decorator", () => {
|
2023-01-12 21:39:33 +01:00
|
|
|
let memoryStorageService: MemoryStorageService;
|
2022-08-16 14:05:03 +02:00
|
|
|
|
|
|
|
@browserSession
|
|
|
|
class TestClass {
|
|
|
|
@sessionSync({ initializer: (s: string) => s })
|
2022-11-01 11:25:46 +01:00
|
|
|
private behaviorSubject = new BehaviorSubject("");
|
2022-08-16 14:05:03 +02:00
|
|
|
|
2023-01-12 21:39:33 +01:00
|
|
|
constructor(private memoryStorageService: MemoryStorageService) {}
|
2022-08-16 14:05:03 +02:00
|
|
|
|
|
|
|
fromJSON(json: any) {
|
|
|
|
this.behaviorSubject.next(json);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-01-12 21:39:33 +01:00
|
|
|
memoryStorageService = Object.create(MemoryStorageService.prototype, {
|
|
|
|
type: { value: MemoryStorageService.TYPE },
|
|
|
|
});
|
2022-08-16 14:05:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create a session syncer", () => {
|
2023-01-12 21:39:33 +01:00
|
|
|
const testClass = new TestClass(memoryStorageService) as any as SessionStorable;
|
2022-08-16 14:05:03 +02:00
|
|
|
expect(testClass.__sessionSyncers.length).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should initialize the session syncer", () => {
|
2023-01-12 21:39:33 +01:00
|
|
|
const testClass = new TestClass(memoryStorageService) as any as SessionStorable;
|
2022-08-16 14:05:03 +02:00
|
|
|
expect(testClass.__sessionSyncers[0].init).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|