@@ -4,6 +4,8 @@ import { getValidator } from "../../src/utils/getValidator";
44import useRcbPlugin from "../../src/core/useRcbPlugin" ;
55
66const mockReplaceStyles = jest . fn ( ) ;
7+ const mockShowToast = jest . fn ( ) ;
8+
79// Mock react-chatbotify dependencies
810jest . mock ( "react-chatbotify" , ( ) => ( {
911 useToasts : jest . fn ( ( ) => ( { showToast : mockShowToast } ) ) ,
@@ -27,163 +29,150 @@ jest.mock("../../src/utils/getValidator", () => ({
2729const mockedValidateFile = validateFile as jest . Mock ;
2830const mockedGetValidator = getValidator as jest . Mock ;
2931
30-
3132mockedValidateFile . mockReturnValue ( {
3233 success : false ,
3334 promptContent : "Invalid file type" ,
3435} ) ;
3536
3637mockedGetValidator . mockReturnValue ( mockedValidateFile ) ;
3738
38- const mockShowToast = jest . fn ( ) ;
39+ // Define custom event interfaces
40+ interface FileUploadEvent extends Event {
41+ data : { files : File [ ] | null } ;
42+ }
43+
44+ interface TextInputEvent extends Event {
45+ data : { inputText : string } ;
46+ }
47+
48+ // Helper functions
49+ const createFileUploadEvent = ( files : File [ ] | null ) : FileUploadEvent => {
50+ const event = new Event ( "rcb-user-upload-file" ) as FileUploadEvent ;
51+ event . data = { files } ;
52+ return event ;
53+ } ;
54+
55+ const createTextInputEvent = ( inputText : string ) : TextInputEvent => {
56+ const event = new Event ( "rcb-user-submit-text" ) as TextInputEvent ;
57+ event . data = { inputText } ;
58+ return event ;
59+ } ;
60+
61+ const renderRcbPluginHook = ( ) => renderHook ( ( ) => useRcbPlugin ( ) ) ;
3962
4063describe ( "useRcbPlugin" , ( ) => {
4164 beforeEach ( ( ) => {
42- jest . clearAllMocks ( ) ; // Clear mocks before each test
65+ jest . clearAllMocks ( ) ;
4366 } ) ;
4467
45- test ( "handles file upload and displays error for invalid file" , ( ) => {
46- const mockFile = new File ( [ "invalid content" ] , "test.txt" , { type : "text/plain" } ) ;
47-
48- // Mock validateFile behavior
49- mockedValidateFile . mockReturnValue ( {
50- success : false ,
51- promptContent : "Invalid file type" ,
68+ describe ( "File Upload Handling" , ( ) => {
69+ describe ( "Valid and Invalid Files" , ( ) => {
70+ test ( "displays error for invalid file" , ( ) => {
71+ const mockFile = new File ( [ "invalid content" ] , "test.txt" , { type : "text/plain" } ) ;
72+ mockedValidateFile . mockReturnValue ( {
73+ success : false ,
74+ promptContent : "Invalid file type" ,
75+ } ) ;
76+
77+ renderRcbPluginHook ( ) ;
78+ const uploadEvent = createFileUploadEvent ( [ mockFile ] ) ;
79+ fireEvent ( window , uploadEvent ) ;
80+
81+ expect ( mockedValidateFile ) . toHaveBeenCalledWith ( mockFile ) ;
82+ expect ( mockShowToast ) . toHaveBeenCalledWith ( "Invalid file type" , 3000 ) ;
83+ } ) ;
84+
85+ test ( "does nothing for valid file" , ( ) => {
86+ const mockFile = new File ( [ "valid content" ] , "test.png" , { type : "image/png" } ) ;
87+ mockedValidateFile . mockReturnValue ( { success : true } ) ;
88+
89+ renderRcbPluginHook ( ) ;
90+ const uploadEvent = createFileUploadEvent ( [ mockFile ] ) ;
91+ fireEvent ( window , uploadEvent ) ;
92+
93+ expect ( mockedValidateFile ) . toHaveBeenCalledWith ( mockFile ) ;
94+ expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ;
95+ } ) ;
5296 } ) ;
53-
54- // Render the hook
55- renderHook ( ( ) => useRcbPlugin ( ) ) ;
56-
57- // Simulate file upload event
58- const uploadEvent = new Event ( "rcb-user-upload-file" ) ;
59- ( uploadEvent as any ) . data = { files : [ mockFile ] } ;
60- fireEvent ( window , uploadEvent ) ;
61-
62- // Debugging output
63- console . log ( "validateFile calls:" , mockedValidateFile . mock . calls ) ;
64- console . log ( "showToast calls:" , mockShowToast . mock . calls ) ;
65-
66- // Assertions
67- expect ( mockedValidateFile ) . toHaveBeenCalledWith ( mockFile ) ;
68- expect ( mockShowToast ) . toHaveBeenCalledWith ( "Invalid file type" , 3000 ) ;
69- } ) ;
70-
71- test ( "handles file upload and does nothing for valid file" , ( ) => {
72- const mockFile = new File ( [ "valid content" ] , "test.png" , { type : "image/png" } ) ;
7397
74- // Mock validateFile to return success
75- ( validateFile as jest . Mock ) . mockReturnValue ( {
76- success : true ,
98+ describe ( "Edge Cases" , ( ) => {
99+ test ( "handles null file upload" , ( ) => {
100+ renderRcbPluginHook ( ) ;
101+ const uploadEvent = createFileUploadEvent ( null ) ;
102+ fireEvent ( window , uploadEvent ) ;
103+
104+ expect ( mockedValidateFile ) . not . toHaveBeenCalled ( ) ;
105+ expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ;
106+ } ) ;
107+
108+ test ( "handles empty file upload" , ( ) => {
109+ renderRcbPluginHook ( ) ;
110+ const uploadEvent = createFileUploadEvent ( [ ] ) ;
111+ fireEvent ( window , uploadEvent ) ;
112+
113+ expect ( mockedValidateFile ) . not . toHaveBeenCalled ( ) ;
114+ expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ;
115+ } ) ;
77116 } ) ;
117+ } ) ;
78118
79- // Mock getValidator to return the validateFile function
80- ( getValidator as jest . Mock ) . mockReturnValue ( validateFile ) ;
119+ describe ( "Text Input Handling" , ( ) => {
120+ describe ( "Valid and Invalid Input" , ( ) => {
121+ test ( "displays error for invalid input" , ( ) => {
122+ const mockValidator = jest . fn ( ) . mockReturnValue ( {
123+ success : false ,
124+ promptContent : "Invalid input" ,
125+ } ) ;
81126
82- renderHook ( ( ) => useRcbPlugin ( ) ) ;
127+ mockedGetValidator . mockReturnValue ( mockValidator ) ;
83128
84- // Simulate file upload event
85- const uploadEvent = new Event ( "rcb-user-upload-file" ) ;
86- ( uploadEvent as any ) . data = { files : [ mockFile ] } ; // Attach mock data
87- fireEvent ( window , uploadEvent ) ;
129+ renderRcbPluginHook ( ) ;
130+ const textEvent = createTextInputEvent ( "invalid text" ) ;
131+ fireEvent ( window , textEvent ) ;
88132
89- // Assertions
90- expect ( validateFile ) . toHaveBeenCalledWith ( mockFile ) ;
91- expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ; // No toast for valid file
92- } ) ;
133+ expect ( mockValidator ) . toHaveBeenCalledWith ( "invalid text" ) ;
134+ expect ( mockShowToast ) . toHaveBeenCalledWith ( "Invalid input" , 3000 ) ;
135+ } ) ;
93136
94- test ( "handles text input and displays error for invalid input" , ( ) => {
95- const mockValidator = jest . fn ( ) . mockReturnValue ( {
96- success : false ,
97- promptContent : "Invalid input" ,
98- } ) ;
137+ test ( "does nothing for valid input" , ( ) => {
138+ const mockValidator = jest . fn ( ) . mockReturnValue ( { success : true } ) ;
139+ mockedGetValidator . mockReturnValue ( mockValidator ) ;
99140
100- // Mock getValidator to return the text validator
101- mockedGetValidator . mockReturnValue ( mockValidator ) ;
141+ renderRcbPluginHook ( ) ;
142+ const textEvent = createTextInputEvent ( "valid input" ) ;
143+ fireEvent ( window , textEvent ) ;
102144
103- renderHook ( ( ) => useRcbPlugin ( ) ) ;
145+ expect ( mockValidator ) . toHaveBeenCalledWith ( "valid input" ) ;
146+ expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ;
147+ } ) ;
148+ } ) ;
104149
105- // Simulate text input event
106- const textEvent = new Event ( "rcb-user-submit-text" ) ;
107- ( textEvent as any ) . data = { inputText : "invalid text" } ;
108- fireEvent ( window , textEvent ) ;
150+ test ( "displays error for empty text input" , ( ) => {
151+ const mockValidator = jest . fn ( ) . mockReturnValue ( {
152+ success : false ,
153+ promptContent : "Input cannot be empty" ,
154+ } ) ;
109155
110- // Assertions
111- expect ( mockValidator ) . toHaveBeenCalledWith ( "invalid text" ) ;
112- expect ( mockShowToast ) . toHaveBeenCalledWith ( "Invalid input" , 3000 ) ;
113- } ) ;
156+ mockedGetValidator . mockReturnValue ( mockValidator ) ;
114157
115- test ( "handles text input and does nothing for valid input" , ( ) => {
116- const mockValidator = jest . fn ( ) . mockReturnValue ( { success : true } ) ;
117-
118- // Mock getValidator to return the text validator
119- mockedGetValidator . mockReturnValue ( mockValidator ) ;
120-
121- renderHook ( ( ) => useRcbPlugin ( ) ) ;
122-
123- // Simulate text input event
124- const textEvent = new Event ( "rcb-user-submit-text" ) ;
125- ( textEvent as any ) . data = { inputText : "valid input" } ;
126- fireEvent ( window , textEvent ) ;
127-
128- // Assertions
129- expect ( mockValidator ) . toHaveBeenCalledWith ( "valid input" ) ;
130- expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ; // No toast for valid input
131- } ) ;
158+ renderRcbPluginHook ( ) ;
159+ const textEvent = createTextInputEvent ( "" ) ;
160+ fireEvent ( window , textEvent ) ;
132161
133- test ( "handles empty text input validation" , ( ) => {
134- const mockValidator = jest . fn ( ) . mockReturnValue ( {
135- success : false ,
136- promptContent : "Input cannot be empty" ,
162+ expect ( mockValidator ) . toHaveBeenCalledWith ( "" ) ;
163+ expect ( mockShowToast ) . toHaveBeenCalledWith ( "Input cannot be empty" , 3000 ) ;
137164 } ) ;
138-
139- mockedGetValidator . mockReturnValue ( mockValidator ) ;
140-
141- renderHook ( ( ) => useRcbPlugin ( ) ) ;
142-
143- const textEvent = new Event ( "rcb-user-submit-text" ) ;
144- ( textEvent as any ) . data = { inputText : "" } ;
145- fireEvent ( window , textEvent ) ;
146-
147- // Assertions
148- expect ( mockValidator ) . toHaveBeenCalledWith ( "" ) ;
149- expect ( mockShowToast ) . toHaveBeenCalledWith ( "Input cannot be empty" , 3000 ) ;
150- } ) ;
151-
152- test ( "handles null file upload" , ( ) => {
153- renderHook ( ( ) => useRcbPlugin ( ) ) ;
154-
155- const uploadEvent = new Event ( "rcb-user-upload-file" ) ;
156- ( uploadEvent as any ) . data = { files : null } ;
157- fireEvent ( window , uploadEvent ) ;
158-
159- // Assertions
160- expect ( mockedValidateFile ) . not . toHaveBeenCalled ( ) ;
161- expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ;
162- } ) ;
163-
164- test ( "handles empty file upload" , ( ) => {
165- renderHook ( ( ) => useRcbPlugin ( ) ) ;
166-
167- // Simulate empty file upload event
168- const uploadEvent = new Event ( "rcb-user-upload-file" ) ;
169- ( uploadEvent as any ) . data = { files : [ ] } ;
170- fireEvent ( window , uploadEvent ) ;
171-
172- // Assertions
173- expect ( mockedValidateFile ) . not . toHaveBeenCalled ( ) ;
174- expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ; // No toast for empty file list
175- } ) ;
176- test ( "restores styles after all toasts are dismissed" , ( ) => {
177- renderHook ( ( ) => useRcbPlugin ( ) ) ;
178-
179- // Simulate toast dismissal event
180- const dismissEvent = new Event ( "rcb-dismiss-toast" ) ;
181- fireEvent ( window , dismissEvent ) ;
182-
183- // Verify that styles are restored
184- setTimeout ( ( ) => {
185- expect ( mockReplaceStyles ) . toHaveBeenCalled ( ) ;
186- } , 0 ) ;
187165 } ) ;
188166
167+ describe ( "Styles Restoration" , ( ) => {
168+ test ( "restores styles after all toasts are dismissed" , ( ) => {
169+ renderRcbPluginHook ( ) ;
170+ const dismissEvent = new Event ( "rcb-dismiss-toast" ) ;
171+ fireEvent ( window , dismissEvent ) ;
172+
173+ setTimeout ( ( ) => {
174+ expect ( mockReplaceStyles ) . toHaveBeenCalled ( ) ;
175+ } , 0 ) ;
176+ } ) ;
177+ } ) ;
189178} ) ;
0 commit comments