-
-
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.
test(profile, user, reply): add tests for use cases and services
- Loading branch information
1 parent
8b43582
commit 54f7e5e
Showing
8 changed files
with
323 additions
and
43 deletions.
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
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
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
71 changes: 71 additions & 0 deletions
71
src/frameworks/profile/typeorm/tests/typeorm-profile.service.spec.ts
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { TypeOrmProfileService } from '../typeorm-profile.service'; | ||
import { Profile } from 'src/core'; | ||
import { Repository } from 'typeorm'; | ||
import { MockDatabaseModule } from 'src/services/mock-database/mock-database.module'; | ||
import { TypeOrmModule, getRepositoryToken } from '@nestjs/typeorm'; | ||
|
||
describe('TypeOrmProfileService', () => { | ||
let service: TypeOrmProfileService; | ||
let repo: Repository<Profile>; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [MockDatabaseModule, TypeOrmModule.forFeature([Profile])], | ||
providers: [TypeOrmProfileService], | ||
}).compile(); | ||
|
||
service = module.get<TypeOrmProfileService>(TypeOrmProfileService); | ||
repo = module.get<Repository<Profile>>(getRepositoryToken(Profile)); | ||
}); | ||
|
||
it('should return a profile for a given user ID', async () => { | ||
const userId = 1; | ||
const profile = new Profile(); | ||
jest.spyOn(repo, 'findOne').mockResolvedValue(profile); | ||
|
||
const result = await service.findByUser(userId); | ||
|
||
expect(result).toEqual(profile); | ||
expect(repo.findOne).toHaveBeenCalledWith({ | ||
where: { | ||
user: { | ||
id: userId, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should create a profile', async () => { | ||
const profile = new Profile(); | ||
jest.spyOn(repo, 'save').mockResolvedValue(profile); | ||
|
||
const result = await service.createProfile(profile); | ||
|
||
expect(result).toEqual(profile); | ||
expect(repo.save).toHaveBeenCalledWith(profile); | ||
}); | ||
|
||
it('should update a profile', async () => { | ||
const profileId = 1; | ||
const profile = new Profile(); | ||
jest.spyOn(repo, 'save').mockResolvedValue(profile); | ||
|
||
const result = await service.updateProfile(profileId, profile); | ||
|
||
expect(result).toEqual(profile); | ||
expect(repo.save).toHaveBeenCalledWith({ | ||
id: profileId, | ||
...profile, | ||
}); | ||
}); | ||
|
||
it('should delete a profile', async () => { | ||
const profileId = 1; | ||
jest.spyOn(repo, 'delete').mockResolvedValue(undefined); | ||
|
||
await service.deleteProfile(profileId); | ||
|
||
expect(repo.delete).toHaveBeenCalledWith(profileId); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
src/frameworks/reply/telegraf/tests/telegraf-reply.service.spec.ts
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { TelegrafReplyService } from '../telegraf-reply.service'; | ||
import { I18nService } from 'nestjs-i18n'; | ||
import { I18nTranslations } from 'src/generated/i18n.generated'; | ||
import { Context } from 'telegraf/typings'; | ||
import { Extra } from 'src/core/types'; | ||
import { PathImpl2 } from '@nestjs/config'; | ||
import { Language } from 'src/core/enums'; | ||
|
||
describe('TelegrafReplyService', () => { | ||
let service: TelegrafReplyService; | ||
let i18n: I18nService<I18nTranslations>; | ||
const ctx = { | ||
session: { | ||
lang: Language.UA, | ||
}, | ||
reply: jest.fn(), | ||
} as unknown as Context; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
TelegrafReplyService, | ||
{ | ||
provide: I18nService, | ||
useValue: { | ||
t: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<TelegrafReplyService>(TelegrafReplyService); | ||
i18n = module.get<I18nService<I18nTranslations>>(I18nService); | ||
}); | ||
|
||
it('should reply with a translated message', async () => { | ||
const msgCode: PathImpl2<I18nTranslations> = 'messages.help'; | ||
const message = 'This is a test message'; | ||
const extra: Extra = {}; | ||
|
||
jest.spyOn(i18n, 't').mockReturnValue(message); | ||
|
||
await service.reply(ctx, msgCode, extra); | ||
|
||
expect(i18n.t).toHaveBeenCalledWith(msgCode, { | ||
lang: Language.UA, | ||
}); | ||
expect(ctx.reply).toHaveBeenCalledWith(message, extra); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
src/frameworks/user/typeorm/tests/typeorm-user.service.spec.ts
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { TypeOrmUserService } from '../typeorm-user.service'; | ||
import { User } from 'src/core'; | ||
import { Repository } from 'typeorm'; | ||
import { MockDatabaseModule } from 'src/services/mock-database/mock-database.module'; | ||
import { TypeOrmModule, getRepositoryToken } from '@nestjs/typeorm'; | ||
|
||
describe('TypeOrmUserService', () => { | ||
let service: TypeOrmUserService; | ||
let repo: Repository<User>; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [MockDatabaseModule, TypeOrmModule.forFeature([User])], | ||
providers: [TypeOrmUserService], | ||
}).compile(); | ||
|
||
service = module.get<TypeOrmUserService>(TypeOrmUserService); | ||
repo = module.get<Repository<User>>(getRepositoryToken(User)); | ||
}); | ||
|
||
it('should return a user for a given Telegram ID', async () => { | ||
const tgId = 123; | ||
const user = User.create(); | ||
jest.spyOn(repo, 'findOne').mockResolvedValue(user); | ||
|
||
const result = await service.findByTgId(tgId); | ||
|
||
expect(result).toEqual(user); | ||
expect(repo.findOne).toHaveBeenCalledWith({ | ||
where: { | ||
userId: tgId, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should create a user', async () => { | ||
const user = User.create(); | ||
jest.spyOn(repo, 'save').mockResolvedValue(user); | ||
|
||
const result = await service.create(user); | ||
|
||
expect(result).toEqual(user); | ||
expect(repo.save).toHaveBeenCalledWith(user); | ||
}); | ||
|
||
it('should update a user', async () => { | ||
const userId = 1; | ||
const user = User.create(); | ||
jest | ||
.spyOn(repo, 'update') | ||
.mockImplementationOnce(() => Promise.resolve(undefined)); | ||
jest.spyOn(repo, 'findOneBy').mockResolvedValue(user); | ||
|
||
const result = await service.update(userId, user); | ||
|
||
expect(result).toEqual(user); | ||
expect(repo.update).toHaveBeenCalledWith(userId, user); | ||
expect(repo.findOneBy).toHaveBeenCalledWith({ | ||
id: user.id, | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.