Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 closeSheetAction,
5 isSheetOpen,
6 openSheetState,
7 readSheetState,
8 withSheetState,
9} from "./app-sheet-history";
10
11describe("isSheetOpen", () => {
12 it("follows the entry's record", () => {
13 expect(isSheetOpen({ appSheet: true }, "/")).toBe(true);
14 expect(isSheetOpen({ appSheet: false }, "/folders/work")).toBe(false);
15 });
16
17 it("opens on app screens when the entry has no record (direct load)", () => {
18 expect(isSheetOpen({ __TSR_index: 0 }, "/folders/work/notes/note-01")).toBe(true);
19 expect(isSheetOpen(undefined, "/settings")).toBe(true);
20 expect(isSheetOpen({}, "/")).toBe(false);
21 });
22});
23
24describe("readSheetState", () => {
25 it("ignores junk", () => {
26 expect(readSheetState({ appSheet: "yes", appSheetOpenedAt: "2" })).toEqual({
27 appSheet: undefined,
28 appSheetOpenedAt: undefined,
29 });
30 expect(readSheetState(null)).toEqual({});
31 });
32});
33
34describe("withSheetState", () => {
35 const open = { state: { appSheet: true, appSheetOpenedAt: 3 }, pathname: "/folders/work" };
36 const closed = { state: { appSheet: false }, pathname: "/folders/work" };
37
38 it("keeps the sheet open for navigation inside the app", () => {
39 expect(withSheetState(open, { __TSR_key: "k" })).toEqual({
40 __TSR_key: "k",
41 appSheet: true,
42 appSheetOpenedAt: 3,
43 });
44 });
45
46 it("makes an inherited record explicit, so the root screen stays open too", () => {
47 const directLoad = { state: {}, pathname: "/folders/work" };
48 expect(withSheetState(directLoad, {})).toEqual({ appSheet: true });
49 });
50
51 it("keeps it closed when it was closed", () => {
52 expect(withSheetState(closed, {})).toEqual({ appSheet: false });
53 });
54
55 it("lets the navigation decide when it says so", () => {
56 expect(withSheetState(open, { appSheet: false })).toEqual({ appSheet: false });
57 expect(withSheetState(closed, openSheetState(5))).toEqual({
58 appSheet: true,
59 appSheetOpenedAt: 6,
60 });
61 });
62});
63
64describe("closeSheetAction", () => {
65 it("goes back to the entry before the one that opened the sheet", () => {
66 // Opened at index 4 (from 3), then two screens deeper.
67 expect(closeSheetAction({ appSheet: true, appSheetOpenedAt: 4 }, 6)).toEqual({
68 type: "go",
69 delta: -3,
70 });
71 expect(closeSheetAction({ appSheet: true, appSheetOpenedAt: 4 }, 4)).toEqual({
72 type: "go",
73 delta: -1,
74 });
75 });
76
77 it("replaces the entry when the sheet opened on a direct load", () => {
78 expect(closeSheetAction({ appSheet: true }, 0)).toEqual({
79 type: "replace",
80 state: { appSheet: false, appSheetOpenedAt: undefined },
81 });
82 });
83
84 it("doesn't trust an opener index that isn't behind us", () => {
85 expect(closeSheetAction({ appSheet: true, appSheetOpenedAt: 9 }, 2).type).toBe("replace");
86 expect(closeSheetAction({ appSheet: true, appSheetOpenedAt: 0 }, 2).type).toBe("replace");
87 });
88});