99 lines
3.6 KiB
TypeScript
99 lines
3.6 KiB
TypeScript
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
|
|
import { type EnvironmentId, ThreadId } from "@t3tools/contracts";
|
|
import { beforeEach, describe, expect, it } from "vite-plus/test";
|
|
|
|
import {
|
|
browserMiniPlayerSource,
|
|
type PreviewMiniPlayerSource,
|
|
selectThreadPreviewMiniPlayer,
|
|
selectThreadPreviewMiniPlayerTabId,
|
|
usePreviewMiniPlayerStore,
|
|
} from "./previewMiniPlayerStore";
|
|
|
|
const refA = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-A"));
|
|
const refB = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-B"));
|
|
const tabA = browserMiniPlayerSource("tab-a");
|
|
const tabB = browserMiniPlayerSource("tab-b");
|
|
const pixel: PreviewMiniPlayerSource = {
|
|
kind: "device",
|
|
hostId: "nucbox",
|
|
deviceId: "emulator-5580",
|
|
platform: "android",
|
|
name: "Pixel",
|
|
};
|
|
|
|
beforeEach(() => {
|
|
usePreviewMiniPlayerStore.setState({ byThreadKey: {} });
|
|
});
|
|
|
|
describe("previewMiniPlayerStore", () => {
|
|
it("keeps floating previews scoped to their thread", () => {
|
|
usePreviewMiniPlayerStore.getState().open(refA, tabA);
|
|
usePreviewMiniPlayerStore.getState().open(refB, tabB);
|
|
|
|
expect(
|
|
selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
|
|
).toMatchObject({ source: tabA });
|
|
expect(
|
|
selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refB),
|
|
).toMatchObject({ source: tabB });
|
|
});
|
|
|
|
it("preserves position when switching the floating tab within one thread", () => {
|
|
usePreviewMiniPlayerStore.getState().open(refA, tabA);
|
|
usePreviewMiniPlayerStore.getState().move(refA, "browser:tab-a", { x: 24, y: 48 });
|
|
usePreviewMiniPlayerStore.getState().open(refA, tabB);
|
|
|
|
expect(
|
|
selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
|
|
).toEqual({
|
|
source: tabB,
|
|
position: { x: 24, y: 48 },
|
|
width: null,
|
|
});
|
|
});
|
|
|
|
it("ignores stale drag updates after the floating tab changes", () => {
|
|
usePreviewMiniPlayerStore.getState().open(refA, tabA);
|
|
usePreviewMiniPlayerStore.getState().open(refA, tabB);
|
|
usePreviewMiniPlayerStore.getState().move(refA, "browser:tab-a", { x: 100, y: 100 });
|
|
|
|
expect(
|
|
selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
|
|
).toEqual({
|
|
source: tabB,
|
|
position: null,
|
|
width: null,
|
|
});
|
|
});
|
|
|
|
it("preserves a thread-bound width while switching tabs", () => {
|
|
usePreviewMiniPlayerStore.getState().open(refA, tabA);
|
|
usePreviewMiniPlayerStore.getState().resize(refA, "browser:tab-a", 480);
|
|
usePreviewMiniPlayerStore.getState().open(refA, tabB);
|
|
|
|
expect(
|
|
selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
|
|
).toMatchObject({ source: tabB, width: 480 });
|
|
});
|
|
|
|
it("floats one source per thread, so a device replaces the browser tab", () => {
|
|
usePreviewMiniPlayerStore.getState().open(refA, tabA);
|
|
usePreviewMiniPlayerStore.getState().open(refA, pixel);
|
|
const floating = selectThreadPreviewMiniPlayer(
|
|
usePreviewMiniPlayerStore.getState().byThreadKey,
|
|
refA,
|
|
);
|
|
|
|
expect(floating).toMatchObject({ source: pixel });
|
|
expect(
|
|
selectThreadPreviewMiniPlayerTabId(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
|
|
).toBeNull();
|
|
// The same device under a new label is still the same floating source.
|
|
usePreviewMiniPlayerStore.getState().open(refA, { ...pixel, name: "Renamed" });
|
|
expect(
|
|
selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
|
|
).toBe(floating);
|
|
});
|
|
});
|