Skip to content

Commit 7851ef9

Browse files
committed
v0.4.5
1 parent a333b7c commit 7851ef9

File tree

5 files changed

+348
-319
lines changed

5 files changed

+348
-319
lines changed

lib/modules/admin/admin.module.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ import type { AdminModuleOptions } from './types';
3030
useValue: randomStringGenerator(),
3131
},
3232
],
33-
exports: [
34-
AdminService,
35-
DatabaseService,
36-
MessagingService,
37-
],
33+
exports: [AdminService, DatabaseService, MessagingService],
3834
})
3935
export class AdminModule {
4036
static register(options: AdminModuleOptions): DynamicModule {

lib/modules/admin/admin.service.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ describe('AdminService', () => {
5656

5757
const mockApp = { name: 'app-name', options: {} };
5858
(firebaseAdmin.initializeApp as jest.Mock).mockReturnValue(mockApp);
59-
(firebaseAdmin.credential.cert as jest.Mock).mockReturnValue('mocked-credential');
59+
(firebaseAdmin.credential.cert as jest.Mock).mockReturnValue(
60+
'mocked-credential',
61+
);
6062

6163
const module: TestingModule = await Test.createTestingModule({
6264
providers: [
@@ -195,7 +197,7 @@ describe('AdminService', () => {
195197
const completeSpy = jest.fn();
196198

197199
service.initializeAppObservable().subscribe({
198-
next: () => { },
200+
next: () => {},
199201
complete: () => {
200202
completeSpy();
201203
expect(completeSpy).toHaveBeenCalledTimes(1);
@@ -210,7 +212,7 @@ describe('AdminService', () => {
210212

211213
it('must return a valid cleanup function when unsubscribing', () => {
212214
const observable = service.initializeAppObservable();
213-
const subscription = observable.subscribe(() => { });
215+
const subscription = observable.subscribe(() => {});
214216
expect(() => subscription.unsubscribe()).not.toThrow();
215217
});
216218
});
Lines changed: 162 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,183 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { MessagingService } from './messaging.service';
33
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';
510
import { FIREBASE_ADMIN_APP } from './admin.constants';
611

712
/**
813
* Extended App type that includes messaging functionality
914
*/
1015
interface AppWithMessaging extends App {
11-
messaging(): Messaging;
16+
messaging(): Messaging;
1217
}
1318

1419
// Mock firebase-admin/messaging
1520
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+
};
3641
});
3742

3843
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);
76103
});
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);
80126
});
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);
99145
});
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+
);
122163
});
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+
);
141181
});
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

Comments
 (0)