Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useComposerFocusState.browser.test.tsx

65 lines2.1 KB

1import { act, useLayoutEffect } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4​
5import { shouldUseRestingComposerLayout } from "../composerFooterLayout";
6import { useComposerFocusState } from "./useComposerFocusState";
7​
8let root: Root;
9let composer: ReturnType<typeof useComposerFocusState>;
10let isResting: boolean;
11​
12function ComposerProbe() {
13 const state = useComposerFocusState();
14 useLayoutEffect(() => {
15 composer = state;
16 isResting = shouldUseRestingComposerLayout({
17 isExistingThread: true,
18 isMobileViewport: false,
19 isScrollCollapsed: state.isComposerScrollCollapsed,
20 hasExpandedChrome: false,
21 hasMultilinePrompt: false,
22 timelineOverflows: true,
23 });
24 });
25 return null;
27​
28beforeEach(async () => {
29 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
30 root = createRoot(document.createElement("div"));
31 await act(() => root.render(<ComposerProbe />));
32});
33​
34afterEach(async () => {
35 await act(() => root.unmount());
36 vi.unstubAllGlobals();
37});
38​
39describe("composer focus state", () => {
40 it("stays expanded when the composer loses focus", async () => {
41 await act(() => composer.setIsComposerFocused(true));
42 expect(isResting).toBe(false);
43​
44 await act(() => composer.setIsComposerFocused(false));
45 expect(isResting).toBe(false);
46 });
47​
48 it("can collapse again on the next scroll after returning to the end", async () => {
49 await act(() => composer.setIsComposerScrollCollapsed(true));
50 expect(isResting).toBe(true);
51​
52 await act(() => composer.restoreAfterTimelineReachedEnd());
53 expect(isResting).toBe(false);
54​
55 await act(() => composer.setIsComposerScrollCollapsed(true));
56 expect(isResting).toBe(true);
57 });
58​
59 it("does not move focus into the composer when the timeline reaches the end", async () => {
60 await act(() => composer.setIsComposerScrollCollapsed(true));
61 await act(() => composer.restoreAfterTimelineReachedEnd());
62 expect(isResting).toBe(false);
63 expect(composer.isComposerFocused).toBe(false);
64 });
65});