mirror of
https://github.com/vector-im/element-web.git
synced 2025-11-10 21:21:10 +01:00
* Introduce snapshot class to track snapshot updates This avoids the hassle of having to manually call emit. * Better viewmodel ergonomics - Rename `SubscriptionViewModel` to `BaseViewModel`. I feel this is appropriate since that class does more than just manage subscriptions. - `getSnapshot` is no longer an abstract method. It's simply a method that returns the current snapshot state. This ensures that getSnapshot result is cached by default which is required by `useSyncExternalStore`. - `props` are a property of the base vm class so that actual VMs don't have to keep creating this property. * Update `TextualEventViewModel` * Fix test * Rename `TextualEvent` to `TextualEventView` * Fix snapshot object not being merged * Rename directory to `EventTileView` * Fix broken snapshot * Add test for snapshot class
42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
/*
|
|
Copyright 2025 New Vector Ltd.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
|
Please see LICENSE files in the repository root for full details.
|
|
*/
|
|
|
|
import { Snapshot } from "../../../src/viewmodels/base/Snapshot";
|
|
|
|
interface TestSnapshot {
|
|
key1: string;
|
|
key2: number;
|
|
key3: boolean;
|
|
}
|
|
|
|
describe("Snapshot", () => {
|
|
it("should accept an initial value", () => {
|
|
const snapshot = new Snapshot<TestSnapshot>({ key1: "foo", key2: 5, key3: false }, jest.fn());
|
|
expect(snapshot.current).toStrictEqual({ key1: "foo", key2: 5, key3: false });
|
|
});
|
|
|
|
it("should call emit callback when state changes", () => {
|
|
const emit = jest.fn();
|
|
const snapshot = new Snapshot<TestSnapshot>({ key1: "foo", key2: 5, key3: false }, emit);
|
|
snapshot.merge({ key3: true });
|
|
expect(emit).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("should swap out entire snapshot on set call", () => {
|
|
const snapshot = new Snapshot<TestSnapshot>({ key1: "foo", key2: 5, key3: false }, jest.fn());
|
|
const newValue = { key1: "bar", key2: 8, key3: true };
|
|
snapshot.set(newValue);
|
|
expect(snapshot.current).toStrictEqual(newValue);
|
|
});
|
|
|
|
it("should merge partial snapshot on merge call", () => {
|
|
const snapshot = new Snapshot<TestSnapshot>({ key1: "foo", key2: 5, key3: false }, jest.fn());
|
|
snapshot.merge({ key2: 10 });
|
|
expect(snapshot.current).toStrictEqual({ key1: "foo", key2: 10, key3: false });
|
|
});
|
|
});
|