6
6
StreamChat ,
7
7
Thread ,
8
8
} from '../../../src' ;
9
+ import { DeepPartial } from '../../../src/types.utility' ;
9
10
import { MessageComposer } from '../../../src/messageComposer/messageComposer' ;
10
11
import { StateStore } from '../../../src/store' ;
11
12
import { DraftResponse , MessageResponse } from '../../../src/types' ;
@@ -20,14 +21,32 @@ vi.mock('../../../src/utils', () => ({
20
21
isArray : vi . fn ( ) ,
21
22
isDate : vi . fn ( ) ,
22
23
isNumber : vi . fn ( ) ,
23
-
24
+ debounce : vi . fn ( ) . mockImplementation ( ( fn ) => fn ) ,
24
25
generateUUIDv4 : vi . fn ( ) . mockReturnValue ( 'test-uuid' ) ,
25
26
isLocalMessage : vi . fn ( ) . mockReturnValue ( true ) ,
26
27
formatMessage : vi . fn ( ) . mockImplementation ( ( msg ) => msg ) ,
27
28
randomId : vi . fn ( ) . mockReturnValue ( 'test-uuid' ) ,
28
29
throttle : vi . fn ( ) . mockImplementation ( ( fn ) => fn ) ,
29
30
} ) ) ;
30
31
32
+ // // Mock dependencies
33
+ // vi.mock('../../../src/utils', () => ({
34
+ // axiosParamsSerializer: vi.fn(),
35
+ // isFunction: vi.fn(),
36
+ // isString: vi.fn(),
37
+ // isObject: vi.fn(),
38
+ // isArray: vi.fn(),
39
+ // isDate: vi.fn(),
40
+ // isNumber: vi.fn(),
41
+ // logChatPromiseExecution: vi.fn(),
42
+ // generateUUIDv4: vi.fn().mockReturnValue('test-uuid'),
43
+ // debounce: vi.fn().mockImplementation((fn) => fn),
44
+ // randomId: vi.fn().mockReturnValue('test-uuid'),
45
+ // isLocalMessage: vi.fn().mockReturnValue(true),
46
+ // formatMessage: vi.fn().mockImplementation((msg) => msg),
47
+ // throttle: vi.fn().mockImplementation((fn) => fn),
48
+ // }));
49
+
31
50
vi . mock ( '../../../src/messageComposer/attachmentManager' , ( ) => ( {
32
51
AttachmentManager : vi . fn ( ) . mockImplementation ( ( ) => ( {
33
52
state : new StateStore ( { attachments : [ ] } ) ,
@@ -46,14 +65,14 @@ vi.mock('../../../src/messageComposer/linkPreviewsManager', () => ({
46
65
} ) ) ,
47
66
} ) ) ;
48
67
49
- vi . mock ( '../../../src/messageComposer/textComposer' , ( ) => ( {
50
- TextComposer : vi . fn ( ) . mockImplementation ( ( ) => ( {
51
- state : new StateStore ( { text : '' , mentionedUsers : [ ] } ) ,
52
- initState : vi . fn ( ) ,
53
- clear : vi . fn ( ) ,
54
- textIsEmpty : vi . fn ( ) . mockReturnValue ( true ) ,
55
- } ) ) ,
56
- } ) ) ;
68
+ // vi.mock('../../../src/messageComposer/textComposer', () => ({
69
+ // TextComposer: vi.fn().mockImplementation(() => ({
70
+ // state: new StateStore({ text: '', mentionedUsers: [] }),
71
+ // initState: vi.fn(),
72
+ // clear: vi.fn(),
73
+ // textIsEmpty: vi.fn().mockReturnValue(true),
74
+ // })),
75
+ // }));
57
76
58
77
vi . mock ( '../../../src/messageComposer/pollComposer' , ( ) => ( {
59
78
PollComposer : vi . fn ( ) . mockImplementation ( ( ) => ( {
@@ -74,7 +93,7 @@ vi.mock('../../../src/messageComposer/CustomDataManager', () => ({
74
93
} ) ) ,
75
94
} ) ) ;
76
95
77
- vi . mock ( '../../../src/messageComposer/middleware' , ( ) => ( {
96
+ vi . mock ( '../../../src/messageComposer/middleware/messageComposer ' , ( ) => ( {
78
97
MessageComposerMiddlewareExecutor : vi . fn ( ) . mockImplementation ( ( ) => ( {
79
98
execute : vi . fn ( ) . mockResolvedValue ( { state : { } } ) ,
80
99
} ) ) ,
@@ -134,7 +153,7 @@ const setup = ({
134
153
} : {
135
154
composition ?: LocalMessage | DraftResponse | MessageResponse | undefined ;
136
155
compositionContext ?: Channel | Thread | LocalMessage | undefined ;
137
- config ?: Partial < MessageComposerConfig > ;
156
+ config ?: DeepPartial < MessageComposerConfig > ;
138
157
} = { } ) => {
139
158
const mockClient = new StreamChat ( 'test-api-key' ) ;
140
159
mockClient . user = user ;
@@ -379,21 +398,26 @@ describe('MessageComposer', () => {
379
398
380
399
it ( 'should return the correct compositionIsEmpty' , ( ) => {
381
400
const { messageComposer } = setup ( ) ;
382
- const textComposerMock = messageComposer . textComposer as any ;
383
-
401
+ const spyTextComposerTextIsEmpty = vi
402
+ . spyOn ( messageComposer . textComposer , 'textIsEmpty' , 'get' )
403
+ . mockReturnValueOnce ( true )
404
+ . mockReturnValueOnce ( false ) ;
384
405
// First case - empty composition
385
- Object . defineProperty ( textComposerMock , 'textIsEmpty' , {
386
- get : ( ) => true ,
406
+ messageComposer . textComposer . state . partialNext ( {
407
+ text : '' ,
408
+ mentionedUsers : [ ] ,
409
+ selection : { start : 0 , end : 0 } ,
387
410
} ) ;
388
- textComposerMock . state . next ( { text : '' } ) ;
389
411
expect ( messageComposer . compositionIsEmpty ) . toBe ( true ) ;
390
412
391
413
// Second case - non-empty composition
392
- Object . defineProperty ( textComposerMock , 'textIsEmpty' , {
393
- get : ( ) => false ,
414
+ messageComposer . textComposer . state . partialNext ( {
415
+ text : 'Hello world' ,
416
+ mentionedUsers : [ ] ,
417
+ selection : { start : 0 , end : 0 } ,
394
418
} ) ;
395
- textComposerMock . state . next ( { text : 'Hello world' } ) ;
396
419
expect ( messageComposer . compositionIsEmpty ) . toBe ( false ) ;
420
+ spyTextComposerTextIsEmpty . mockRestore ( ) ;
397
421
} ) ;
398
422
} ) ;
399
423
@@ -982,7 +1006,7 @@ describe('MessageComposer', () => {
982
1006
description : '' ,
983
1007
enforce_unique_vote : false ,
984
1008
is_closed : false ,
985
- max_votes_allowed : 1 ,
1009
+ max_votes_allowed : '1' ,
986
1010
user_id : 'user-id' ,
987
1011
voting_visibility : 'public' ,
988
1012
} ,
@@ -1024,5 +1048,42 @@ describe('MessageComposer', () => {
1024
1048
expect ( spy ) . toHaveBeenCalled ( ) ;
1025
1049
} ) ;
1026
1050
} ) ;
1051
+
1052
+ describe ( 'subscribeMessageComposerConfigStateChanged' , ( ) => {
1053
+ const defaultValue = 'Default text' ;
1054
+
1055
+ it ( 'should insert default text when text is empty and config has a default value' , ( ) => {
1056
+ const { messageComposer } = setup ( ) ;
1057
+ const spy = vi . spyOn ( messageComposer . textComposer , 'insertText' ) ;
1058
+ messageComposer . registerSubscriptions ( ) ;
1059
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
1060
+
1061
+ messageComposer . textComposer . defaultValue = defaultValue ;
1062
+
1063
+ expect ( spy ) . toHaveBeenCalledWith ( {
1064
+ text : defaultValue ,
1065
+ selection : { start : 0 , end : 0 } ,
1066
+ } ) ;
1067
+ spy . mockRestore ( ) ;
1068
+ } ) ;
1069
+
1070
+ it ( 'should not insert default text when text is not empty' , ( ) => {
1071
+ const { messageComposer } = setup ( ) ;
1072
+ messageComposer . registerSubscriptions ( ) ;
1073
+ const spy = vi . spyOn ( messageComposer . textComposer , 'insertText' ) ;
1074
+
1075
+ messageComposer . textComposer . state . next ( {
1076
+ text : 'Hello world' ,
1077
+ mentionedUsers : [ ] ,
1078
+ selection : { start : 0 , end : 0 } ,
1079
+ } ) ;
1080
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
1081
+
1082
+ messageComposer . textComposer . defaultValue = defaultValue ;
1083
+
1084
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
1085
+ spy . mockRestore ( ) ;
1086
+ } ) ;
1087
+ } ) ;
1027
1088
} ) ;
1028
1089
} ) ;
0 commit comments