1import { describe, expect, it } from "@effect/vitest";
2
3import {
4 EMPTY_INCOMING_SHARE_PRESENTATION_STATE,
5 transitionIncomingSharePresentation,
6} from "./incoming-share-presentation";
7
8describe("incoming share presentation", () => {
9 it("does not reopen a dismissed share when refresh returns a new object for the same id", () => {
10 const presented = transitionIncomingSharePresentation(EMPTY_INCOMING_SHARE_PRESENTATION_STATE, {
11 isShareSheetPresented: false,
12 pendingShareId: "share-1",
13 });
14 expect(presented.shareIdToPresent).toBe("share-1");
15
16 const whilePresented = transitionIncomingSharePresentation(presented.state, {
17 isShareSheetPresented: true,
18 pendingShareId: "share-1",
19 });
20 expect(whilePresented).toEqual({ state: presented.state, shareIdToPresent: null });
21
22 const dismissed = transitionIncomingSharePresentation(whilePresented.state, {
23 isShareSheetPresented: false,
24 pendingShareId: "share-1",
25 });
26 expect(dismissed.state.dismissedShareId).toBe("share-1");
27
28 expect(
29 transitionIncomingSharePresentation(dismissed.state, {
30 isShareSheetPresented: false,
31 pendingShareId: "share-1",
32 }),
33 ).toEqual({ state: dismissed.state, shareIdToPresent: null });
34 });
35
36 it("presents the next queued share immediately after the previous sheet closes", () => {
37 const first = transitionIncomingSharePresentation(EMPTY_INCOMING_SHARE_PRESENTATION_STATE, {
38 isShareSheetPresented: false,
39 pendingShareId: "share-1",
40 });
41
42 const next = transitionIncomingSharePresentation(first.state, {
43 isShareSheetPresented: false,
44 pendingShareId: "share-2",
45 });
46 expect(next.shareIdToPresent).toBe("share-2");
47 expect(next.state).toEqual({ presentedShareId: "share-2", dismissedShareId: null });
48 });
49
50 it("forgets dismissal after consumption so a later handoff may reuse the id", () => {
51 const dismissed = {
52 presentedShareId: null,
53 dismissedShareId: "share-1",
54 };
55 const consumed = transitionIncomingSharePresentation(dismissed, {
56 isShareSheetPresented: false,
57 pendingShareId: null,
58 });
59 expect(consumed.state).toEqual(EMPTY_INCOMING_SHARE_PRESENTATION_STATE);
60
61 expect(
62 transitionIncomingSharePresentation(consumed.state, {
63 isShareSheetPresented: false,
64 pendingShareId: "share-1",
65 }).shareIdToPresent,
66 ).toBe("share-1");
67 });
68
69 it("forgets a consumed presentation while its sheet is still mounted", () => {
70 const presented = {
71 presentedShareId: "share-1",
72 dismissedShareId: null,
73 };
74 const consumed = transitionIncomingSharePresentation(presented, {
75 isShareSheetPresented: true,
76 pendingShareId: null,
77 });
78 expect(consumed.state).toEqual(EMPTY_INCOMING_SHARE_PRESENTATION_STATE);
79
80 const replacementWhileOpen = transitionIncomingSharePresentation(consumed.state, {
81 isShareSheetPresented: true,
82 pendingShareId: "share-1",
83 });
84 expect(replacementWhileOpen.shareIdToPresent).toBeNull();
85 expect(
86 transitionIncomingSharePresentation(replacementWhileOpen.state, {
87 isShareSheetPresented: false,
88 pendingShareId: "share-1",
89 }).shareIdToPresent,
90 ).toBe("share-1");
91 });
92});