1
1
import { Test , TestingModule } from '@nestjs/testing' ;
2
2
import { MessagingService } from './messaging.service' ;
3
3
import { App } from 'firebase-admin/app' ;
4
- import { Messaging , Message , MulticastMessage , TopicMessage } from 'firebase-admin/messaging' ;
4
+ import {
5
+ Messaging ,
6
+ Message ,
7
+ MulticastMessage ,
8
+ TopicMessage ,
9
+ } from 'firebase-admin/messaging' ;
5
10
import { FIREBASE_ADMIN_APP } from './admin.constants' ;
6
11
7
12
/**
8
13
* Extended App type that includes messaging functionality
9
14
*/
10
15
interface AppWithMessaging extends App {
11
- messaging ( ) : Messaging ;
16
+ messaging ( ) : Messaging ;
12
17
}
13
18
14
19
// Mock firebase-admin/messaging
15
20
jest . mock ( 'firebase-admin/messaging' , ( ) => {
16
- return {
17
- Messaging : jest . fn ( ) . mockImplementation ( ( ) => ( {
18
- send : jest . fn ( ) . mockResolvedValue ( 'message-id' ) ,
19
- sendMulticast : jest . fn ( ) . mockResolvedValue ( {
20
- successCount : 1 ,
21
- failureCount : 0 ,
22
- responses : [ { messageId : 'message-id' } ] ,
23
- } ) ,
24
- subscribeToTopic : jest . fn ( ) . mockResolvedValue ( {
25
- successCount : 1 ,
26
- failureCount : 0 ,
27
- errors : [ ] ,
28
- } ) ,
29
- unsubscribeFromTopic : jest . fn ( ) . mockResolvedValue ( {
30
- successCount : 1 ,
31
- failureCount : 0 ,
32
- errors : [ ] ,
33
- } ) ,
34
- } ) ) ,
35
- } ;
21
+ return {
22
+ Messaging : jest . fn ( ) . mockImplementation ( ( ) => ( {
23
+ send : jest . fn ( ) . mockResolvedValue ( 'message-id' ) ,
24
+ sendMulticast : jest . fn ( ) . mockResolvedValue ( {
25
+ successCount : 1 ,
26
+ failureCount : 0 ,
27
+ responses : [ { messageId : 'message-id' } ] ,
28
+ } ) ,
29
+ subscribeToTopic : jest . fn ( ) . mockResolvedValue ( {
30
+ successCount : 1 ,
31
+ failureCount : 0 ,
32
+ errors : [ ] ,
33
+ } ) ,
34
+ unsubscribeFromTopic : jest . fn ( ) . mockResolvedValue ( {
35
+ successCount : 1 ,
36
+ failureCount : 0 ,
37
+ errors : [ ] ,
38
+ } ) ,
39
+ } ) ) ,
40
+ } ;
36
41
} ) ;
37
42
38
43
describe ( 'MessagingService' , ( ) => {
39
- let service : MessagingService ;
40
- let mockMessaging : jest . Mocked < Messaging > ;
41
-
42
- beforeEach ( async ( ) => {
43
- const mockApp = {
44
- messaging : jest . fn ( ) . mockReturnValue ( {
45
- send : jest . fn ( ) . mockResolvedValue ( 'message-id' ) ,
46
- sendMulticast : jest . fn ( ) . mockResolvedValue ( {
47
- successCount : 1 ,
48
- failureCount : 0 ,
49
- responses : [ { messageId : 'message-id' } ] ,
50
- } ) ,
51
- subscribeToTopic : jest . fn ( ) . mockResolvedValue ( {
52
- successCount : 1 ,
53
- failureCount : 0 ,
54
- errors : [ ] ,
55
- } ) ,
56
- unsubscribeFromTopic : jest . fn ( ) . mockResolvedValue ( {
57
- successCount : 1 ,
58
- failureCount : 0 ,
59
- errors : [ ] ,
60
- } ) ,
61
- } ) ,
62
- } as unknown as AppWithMessaging ;
63
-
64
- const module : TestingModule = await Test . createTestingModule ( {
65
- providers : [
66
- MessagingService ,
67
- {
68
- provide : FIREBASE_ADMIN_APP ,
69
- useValue : mockApp ,
70
- } ,
71
- ] ,
72
- } ) . compile ( ) ;
73
-
74
- service = module . get < MessagingService > ( MessagingService ) ;
75
- mockMessaging = mockApp . messaging ( ) as jest . Mocked < Messaging > ;
44
+ let service : MessagingService ;
45
+ let mockMessaging : jest . Mocked < Messaging > ;
46
+
47
+ beforeEach ( async ( ) => {
48
+ const mockApp = {
49
+ messaging : jest . fn ( ) . mockReturnValue ( {
50
+ send : jest . fn ( ) . mockResolvedValue ( 'message-id' ) ,
51
+ sendMulticast : jest . fn ( ) . mockResolvedValue ( {
52
+ successCount : 1 ,
53
+ failureCount : 0 ,
54
+ responses : [ { messageId : 'message-id' } ] ,
55
+ } ) ,
56
+ subscribeToTopic : jest . fn ( ) . mockResolvedValue ( {
57
+ successCount : 1 ,
58
+ failureCount : 0 ,
59
+ errors : [ ] ,
60
+ } ) ,
61
+ unsubscribeFromTopic : jest . fn ( ) . mockResolvedValue ( {
62
+ successCount : 1 ,
63
+ failureCount : 0 ,
64
+ errors : [ ] ,
65
+ } ) ,
66
+ } ) ,
67
+ } as unknown as AppWithMessaging ;
68
+
69
+ const module : TestingModule = await Test . createTestingModule ( {
70
+ providers : [
71
+ MessagingService ,
72
+ {
73
+ provide : FIREBASE_ADMIN_APP ,
74
+ useValue : mockApp ,
75
+ } ,
76
+ ] ,
77
+ } ) . compile ( ) ;
78
+
79
+ service = module . get < MessagingService > ( MessagingService ) ;
80
+ mockMessaging = mockApp . messaging ( ) as jest . Mocked < Messaging > ;
81
+ } ) ;
82
+
83
+ it ( 'should be defined' , ( ) => {
84
+ expect ( service ) . toBeDefined ( ) ;
85
+ } ) ;
86
+
87
+ describe ( 'sendToDevice' , ( ) => {
88
+ it ( 'should send a message to a single device' , async ( ) => {
89
+ const token = 'device-token' ;
90
+ const payload : Partial < Message > = {
91
+ notification : {
92
+ title : 'Test Title' ,
93
+ body : 'Test Body' ,
94
+ } ,
95
+ } ;
96
+
97
+ const result = await service . sendToDevice ( token , payload ) ;
98
+ expect ( result ) . toBe ( 'message-id' ) ;
99
+ expect ( mockMessaging . send ) . toHaveBeenCalledWith ( {
100
+ token,
101
+ ...payload ,
102
+ } as Message ) ;
76
103
} ) ;
77
-
78
- it ( 'should be defined' , ( ) => {
79
- expect ( service ) . toBeDefined ( ) ;
104
+ } ) ;
105
+
106
+ describe ( 'sendToDevices' , ( ) => {
107
+ it ( 'should send a message to multiple devices' , async ( ) => {
108
+ const tokens = [ 'token1' , 'token2' ] ;
109
+ const payload : Partial < MulticastMessage > = {
110
+ notification : {
111
+ title : 'Test Title' ,
112
+ body : 'Test Body' ,
113
+ } ,
114
+ } ;
115
+
116
+ const result = await service . sendToDevices ( tokens , payload ) ;
117
+ expect ( result ) . toEqual ( {
118
+ successCount : 1 ,
119
+ failureCount : 0 ,
120
+ responses : [ { messageId : 'message-id' } ] ,
121
+ } ) ;
122
+ expect ( mockMessaging . sendMulticast ) . toHaveBeenCalledWith ( {
123
+ tokens,
124
+ ...payload ,
125
+ } as MulticastMessage ) ;
80
126
} ) ;
81
-
82
- describe ( 'sendToDevice' , ( ) => {
83
- it ( 'should send a message to a single device' , async ( ) => {
84
- const token = 'device-token' ;
85
- const payload : Partial < Message > = {
86
- notification : {
87
- title : 'Test Title' ,
88
- body : 'Test Body ' ,
89
- } ,
90
- } ;
91
-
92
- const result = await service . sendToDevice ( token , payload ) ;
93
- expect ( result ) . toBe ( 'message-id' ) ;
94
- expect ( mockMessaging . send ) . toHaveBeenCalledWith ( {
95
- token ,
96
- ... payload ,
97
- } as Message ) ;
98
- } ) ;
127
+ } ) ;
128
+
129
+ describe ( 'sendToTopic' , ( ) => {
130
+ it ( 'should send a message to a topic' , async ( ) => {
131
+ const topic = 'test-topic' ;
132
+ const payload : Partial < TopicMessage > = {
133
+ notification : {
134
+ title : 'Test Title ' ,
135
+ body : 'Test Body' ,
136
+ } ,
137
+ } ;
138
+
139
+ const result = await service . sendToTopic ( topic , payload ) ;
140
+ expect ( result ) . toBe ( 'message-id' ) ;
141
+ expect ( mockMessaging . send ) . toHaveBeenCalledWith ( {
142
+ topic ,
143
+ ... payload ,
144
+ } as TopicMessage ) ;
99
145
} ) ;
100
-
101
- describe ( 'sendToDevices' , ( ) => {
102
- it ( 'should send a message to multiple devices' , async ( ) => {
103
- const tokens = [ 'token1' , 'token2' ] ;
104
- const payload : Partial < MulticastMessage > = {
105
- notification : {
106
- title : 'Test Title' ,
107
- body : 'Test Body' ,
108
- } ,
109
- } ;
110
-
111
- const result = await service . sendToDevices ( tokens , payload ) ;
112
- expect ( result ) . toEqual ( {
113
- successCount : 1 ,
114
- failureCount : 0 ,
115
- responses : [ { messageId : 'message-id' } ] ,
116
- } ) ;
117
- expect ( mockMessaging . sendMulticast ) . toHaveBeenCalledWith ( {
118
- tokens,
119
- ...payload ,
120
- } as MulticastMessage ) ;
121
- } ) ;
146
+ } ) ;
147
+
148
+ describe ( 'subscribeToTopic' , ( ) => {
149
+ it ( 'should subscribe devices to a topic' , async ( ) => {
150
+ const tokens = [ 'token1' , 'token2' ] ;
151
+ const topic = 'test-topic' ;
152
+
153
+ const result = await service . subscribeToTopic ( tokens , topic ) ;
154
+ expect ( result ) . toEqual ( {
155
+ successCount : 1 ,
156
+ failureCount : 0 ,
157
+ errors : [ ] ,
158
+ } ) ;
159
+ expect ( mockMessaging . subscribeToTopic ) . toHaveBeenCalledWith (
160
+ tokens ,
161
+ topic ,
162
+ ) ;
122
163
} ) ;
123
-
124
- describe ( 'sendToTopic' , ( ) => {
125
- it ( 'should send a message to a topic' , async ( ) => {
126
- const topic = 'test-topic' ;
127
- const payload : Partial < TopicMessage > = {
128
- notification : {
129
- title : 'Test Title' ,
130
- body : 'Test Body' ,
131
- } ,
132
- } ;
133
-
134
- const result = await service . sendToTopic ( topic , payload ) ;
135
- expect ( result ) . toBe ( 'message-id' ) ;
136
- expect ( mockMessaging . send ) . toHaveBeenCalledWith ( {
137
- topic,
138
- ...payload ,
139
- } as TopicMessage ) ;
140
- } ) ;
164
+ } ) ;
165
+
166
+ describe ( 'unsubscribeFromTopic' , ( ) => {
167
+ it ( 'should unsubscribe devices from a topic' , async ( ) => {
168
+ const tokens = [ 'token1' , 'token2' ] ;
169
+ const topic = 'test-topic' ;
170
+
171
+ const result = await service . unsubscribeFromTopic ( tokens , topic ) ;
172
+ expect ( result ) . toEqual ( {
173
+ successCount : 1 ,
174
+ failureCount : 0 ,
175
+ errors : [ ] ,
176
+ } ) ;
177
+ expect ( mockMessaging . unsubscribeFromTopic ) . toHaveBeenCalledWith (
178
+ tokens ,
179
+ topic ,
180
+ ) ;
141
181
} ) ;
142
-
143
- describe ( 'subscribeToTopic' , ( ) => {
144
- it ( 'should subscribe devices to a topic' , async ( ) => {
145
- const tokens = [ 'token1' , 'token2' ] ;
146
- const topic = 'test-topic' ;
147
-
148
- const result = await service . subscribeToTopic ( tokens , topic ) ;
149
- expect ( result ) . toEqual ( {
150
- successCount : 1 ,
151
- failureCount : 0 ,
152
- errors : [ ] ,
153
- } ) ;
154
- expect ( mockMessaging . subscribeToTopic ) . toHaveBeenCalledWith ( tokens , topic ) ;
155
- } ) ;
156
- } ) ;
157
-
158
- describe ( 'unsubscribeFromTopic' , ( ) => {
159
- it ( 'should unsubscribe devices from a topic' , async ( ) => {
160
- const tokens = [ 'token1' , 'token2' ] ;
161
- const topic = 'test-topic' ;
162
-
163
- const result = await service . unsubscribeFromTopic ( tokens , topic ) ;
164
- expect ( result ) . toEqual ( {
165
- successCount : 1 ,
166
- failureCount : 0 ,
167
- errors : [ ] ,
168
- } ) ;
169
- expect ( mockMessaging . unsubscribeFromTopic ) . toHaveBeenCalledWith ( tokens , topic ) ;
170
- } ) ;
171
- } ) ;
172
- } ) ;
182
+ } ) ;
183
+ } ) ;
0 commit comments