-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BE] test : notification.service 테스트 코드 작성
- Loading branch information
1 parent
a20e44b
commit 094ab83
Showing
1 changed file
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,102 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { NotificationService } from './notification.service'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { RegistrationTokenRepository } from './registrationToken.repository'; | ||
import { RegistrationTokenEntity } from '../entities/registrationToken.entity'; | ||
import { PushMessage } from '../common/fcmHandler'; | ||
|
||
const mockRepository = { | ||
save: jest.fn(), | ||
delete: jest.fn(), | ||
findOne: jest.fn(), | ||
update: jest.fn(), | ||
}; | ||
|
||
const mockAdmin = jest.requireMock('firebase-admin'); | ||
jest.mock('firebase-admin'); | ||
mockAdmin.apps = []; | ||
|
||
describe('NotificationService', () => { | ||
let service: NotificationService; | ||
let repository; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [NotificationService], | ||
providers: [ | ||
NotificationService, | ||
{ | ||
provide: RegistrationTokenRepository, | ||
useValue: mockRepository, | ||
}, | ||
{ | ||
provide: ConfigService, | ||
useValue: { get: jest.fn((key: string) => 'mocked-value') }, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<NotificationService>(NotificationService); | ||
repository = module.get(RegistrationTokenRepository); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('getRegistrationToken', function () { | ||
it('should return null when token does not exist', async function () { | ||
repository.findOne.mockResolvedValue(null); | ||
const res = await service.getRegistrationToken('user'); | ||
expect(res).toEqual(null); | ||
}); | ||
|
||
it('should return registration token', async function () { | ||
const registrationToken = new RegistrationTokenEntity(); | ||
registrationToken.registration_token = 'test'; | ||
repository.findOne.mockResolvedValue(registrationToken); | ||
const res = await service.getRegistrationToken('user'); | ||
expect(res).toEqual('test'); | ||
}); | ||
}); | ||
|
||
describe('registerToken', function () { | ||
it('should create token', async function () { | ||
repository.findOne.mockResolvedValue(null); | ||
await service.registerToken('user', 'token'); | ||
expect(repository.save).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should update token', async function () { | ||
repository.findOne.mockResolvedValue(new RegistrationTokenEntity()); | ||
await service.registerToken('user', 'token'); | ||
expect(repository.update).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('removeRegistrationToken', function () { | ||
it('should remove', async function () { | ||
await service.removeRegistrationToken('userId'); | ||
expect(repository.delete).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('createChatNotificationMessage', function () { | ||
it('should return message', function () { | ||
const pushMessage: PushMessage = { | ||
body: 'message', | ||
data: { | ||
room_id: '123', | ||
}, | ||
title: 'nickname', | ||
}; | ||
const result = service.createChatNotificationMessage( | ||
'token', | ||
pushMessage, | ||
); | ||
expect(result.token).toEqual('token'); | ||
expect(result.notification.title).toEqual('nickname'); | ||
expect(result.notification.body).toEqual('message'); | ||
expect(result.data.room_id).toEqual('123'); | ||
}); | ||
}); | ||
}); |