1
+ import { renderHook , act } from "@testing-library/react" ;
2
+ import { useCreateConversation } from "#/hooks/mutation/use-create-conversation" ;
3
+ import OpenHands from "#/api/open-hands" ;
4
+ import { useNavigate } from "react-router" ;
5
+ import { useDispatch , useSelector } from "react-redux" ;
6
+ import { QueryClient , QueryClientProvider } from "@tanstack/react-query" ;
7
+ import { describe , it , expect , vi , beforeEach } from "vitest" ;
8
+
9
+ // Mock dependencies
10
+ vi . mock ( "react-router" , ( ) => ( {
11
+ useNavigate : vi . fn ( ) ,
12
+ } ) ) ;
13
+
14
+ vi . mock ( "react-redux" , ( ) => ( {
15
+ useDispatch : vi . fn ( ) ,
16
+ useSelector : vi . fn ( ) ,
17
+ } ) ) ;
18
+
19
+ vi . mock ( "#/api/open-hands" , ( ) => ( {
20
+ default : {
21
+ createConversation : vi . fn ( ) ,
22
+ } ,
23
+ } ) ) ;
24
+
25
+ vi . mock ( "posthog-js" , ( ) => ( {
26
+ default : {
27
+ capture : vi . fn ( ) ,
28
+ } ,
29
+ } ) ) ;
30
+
31
+ describe ( "useCreateConversation" , ( ) => {
32
+ const mockNavigate = vi . fn ( ) ;
33
+ const mockDispatch = vi . fn ( ) ;
34
+ const mockQueryClient = new QueryClient ( ) ;
35
+
36
+ beforeEach ( ( ) => {
37
+ vi . clearAllMocks ( ) ;
38
+ ( useNavigate as any ) . mockReturnValue ( mockNavigate ) ;
39
+ ( useDispatch as any ) . mockReturnValue ( mockDispatch ) ;
40
+ ( useSelector as any ) . mockReturnValue ( {
41
+ selectedRepository : null ,
42
+ files : [ ] ,
43
+ replayJson : null ,
44
+ } ) ;
45
+ ( OpenHands . createConversation as any ) . mockResolvedValue ( {
46
+ conversation_id : "test-id" ,
47
+ } ) ;
48
+ } ) ;
49
+
50
+ const wrapper = ( { children } : { children : React . ReactNode } ) => (
51
+ < QueryClientProvider client = { mockQueryClient } > { children } </ QueryClientProvider >
52
+ ) ;
53
+
54
+ it ( "should throw an error when no query, repository, files, or replayJson is provided" , async ( ) => {
55
+ const { result } = renderHook ( ( ) => useCreateConversation ( ) , { wrapper } ) ;
56
+
57
+ await act ( async ( ) => {
58
+ await expect ( result . current . mutateAsync ( { q : "" } ) ) . rejects . toThrow (
59
+ "No query provided"
60
+ ) ;
61
+ } ) ;
62
+ } ) ;
63
+
64
+ it ( "should allow empty query when allowEmptyQuery is true" , async ( ) => {
65
+ const { result } = renderHook ( ( ) => useCreateConversation ( ) , { wrapper } ) ;
66
+
67
+ await act ( async ( ) => {
68
+ await result . current . mutateAsync ( { q : "" , allowEmptyQuery : true } ) ;
69
+ } ) ;
70
+
71
+ expect ( OpenHands . createConversation ) . toHaveBeenCalledWith (
72
+ undefined ,
73
+ "" ,
74
+ [ ] ,
75
+ undefined
76
+ ) ;
77
+ expect ( mockNavigate ) . toHaveBeenCalledWith ( "/conversations/test-id" ) ;
78
+ } ) ;
79
+ } ) ;
0 commit comments