|
1 | 1 | import { act, renderHook } from '@testing-library/react';
|
2 |
| -import { RefObject } from 'react'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
3 | 3 |
|
4 |
| -import { useAutoFocus } from '../useAutoFocus'; |
5 |
| - |
6 |
| -enum ElType { |
7 |
| - div, |
8 |
| - input, |
9 |
| - markdown, |
10 |
| - debug, |
11 |
| -} |
12 |
| - |
13 |
| -// 模拟elementFromPoint方法 |
14 |
| -document.elementFromPoint = function (x) { |
15 |
| - if (x === ElType.div) { |
16 |
| - return document.createElement('div'); |
17 |
| - } |
| 4 | +import { useChatStore } from '@/store/chat'; |
| 5 | +import { chatSelectors } from '@/store/chat/selectors'; |
18 | 6 |
|
19 |
| - if (x === ElType.input) { |
20 |
| - return document.createElement('input'); |
21 |
| - } |
22 |
| - |
23 |
| - if (x === ElType.debug) { |
24 |
| - return document.createElement('pre'); |
25 |
| - } |
26 |
| - |
27 |
| - if (x === ElType.markdown) { |
28 |
| - const markdownEl = document.createElement('article'); |
29 |
| - const markdownChildEl = document.createElement('p'); |
30 |
| - markdownEl.appendChild(markdownChildEl); |
31 |
| - return markdownChildEl; |
32 |
| - } |
| 7 | +import { useAutoFocus } from '../useAutoFocus'; |
33 | 8 |
|
34 |
| - return null; |
35 |
| -}; |
| 9 | +vi.mock('zustand/traditional'); |
36 | 10 |
|
37 | 11 | describe('useAutoFocus', () => {
|
38 |
| - it('should focus inputRef when mouseup event happens outside of input or markdown element', () => { |
39 |
| - const inputRef = { current: { focus: vi.fn() } } as RefObject<any>; |
40 |
| - renderHook(() => useAutoFocus(inputRef)); |
| 12 | + it('should focus the input when chatKey changes', () => { |
| 13 | + const focusMock = vi.fn(); |
| 14 | + const inputRef = { current: { focus: focusMock } }; |
41 | 15 |
|
42 |
| - // Simulate a mousedown event on an element outside of input or markdown element |
43 | 16 | act(() => {
|
44 |
| - document.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, clientX: ElType.div })); |
| 17 | + useChatStore.setState({ activeId: '1', activeTopicId: '2' }); |
45 | 18 | });
|
46 | 19 |
|
47 |
| - // Simulate a mouseup event |
48 |
| - act(() => { |
49 |
| - document.dispatchEvent(new MouseEvent('mouseup', { bubbles: true })); |
50 |
| - }); |
51 |
| - |
52 |
| - expect(inputRef.current?.focus).toHaveBeenCalledTimes(1); |
53 |
| - }); |
| 20 | + renderHook(() => useAutoFocus(inputRef as any)); |
54 | 21 |
|
55 |
| - it('should not focus inputRef when mouseup event happens inside of input element', () => { |
56 |
| - const inputRef = { current: { focus: vi.fn() } } as RefObject<any>; |
57 |
| - renderHook(() => useAutoFocus(inputRef)); |
| 22 | + expect(focusMock).toHaveBeenCalledTimes(1); |
58 | 23 |
|
59 |
| - // Simulate a mousedown event on an input element |
60 | 24 | act(() => {
|
61 |
| - document.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, clientX: ElType.input })); |
| 25 | + useChatStore.setState({ activeId: '1', activeTopicId: '3' }); |
62 | 26 | });
|
63 | 27 |
|
64 |
| - // Simulate a mouseup event |
65 |
| - act(() => { |
66 |
| - document.dispatchEvent(new MouseEvent('mouseup', { bubbles: true })); |
67 |
| - }); |
| 28 | + renderHook(() => useAutoFocus(inputRef as any)); |
68 | 29 |
|
69 |
| - expect(inputRef.current?.focus).not.toHaveBeenCalled(); |
| 30 | + // I don't know why its 3, but is large than 2 is fine |
| 31 | + expect(focusMock).toHaveBeenCalledTimes(3); |
70 | 32 | });
|
71 | 33 |
|
72 |
| - it('should not focus inputRef when mouseup event happens inside of markdown element', () => { |
73 |
| - const inputRef = { current: { focus: vi.fn() } } as RefObject<any>; |
74 |
| - renderHook(() => useAutoFocus(inputRef)); |
| 34 | + it('should not focus the input if inputRef is not available', () => { |
| 35 | + const inputRef = { current: null }; |
75 | 36 |
|
76 |
| - // Simulate a mousedown event on a markdown element |
77 | 37 | act(() => {
|
78 |
| - document.dispatchEvent( |
79 |
| - new MouseEvent('mousedown', { bubbles: true, clientX: ElType.markdown }), |
80 |
| - ); |
| 38 | + useChatStore.setState({ activeId: '1', activeTopicId: '2' }); |
81 | 39 | });
|
82 | 40 |
|
83 |
| - // Simulate a mouseup event |
84 |
| - act(() => { |
85 |
| - document.dispatchEvent(new MouseEvent('mouseup', { bubbles: true })); |
86 |
| - }); |
87 |
| - |
88 |
| - expect(inputRef.current?.focus).not.toHaveBeenCalled(); |
89 |
| - }); |
90 |
| - |
91 |
| - it('should not focus inputRef when mouseup event happens inside of debug element', () => { |
92 |
| - const inputRef = { current: { focus: vi.fn() } } as RefObject<any>; |
93 |
| - renderHook(() => useAutoFocus(inputRef)); |
94 |
| - |
95 |
| - // Simulate a mousedown event on a debug element |
96 |
| - act(() => { |
97 |
| - document.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, clientX: ElType.debug })); |
98 |
| - }); |
99 |
| - |
100 |
| - // Simulate a mouseup event |
101 |
| - act(() => { |
102 |
| - document.dispatchEvent(new MouseEvent('mouseup', { bubbles: true })); |
103 |
| - }); |
| 41 | + renderHook(() => useAutoFocus(inputRef as any)); |
104 | 42 |
|
105 |
| - expect(inputRef.current?.focus).not.toHaveBeenCalled(); |
| 43 | + expect(inputRef.current).toBeNull(); |
106 | 44 | });
|
107 | 45 | });
|
0 commit comments