1
0
mirror of https://github.com/sadfsdfdsa/allbot.git synced 2024-11-19 00:31:42 +02:00

test: cover jest tests for bot, db and repository

This commit is contained in:
Artem Shuvaev 2023-08-30 15:17:17 +05:00
parent f754d0e057
commit ce695bbd26
8 changed files with 3831 additions and 4 deletions

14
core/bot.test.ts Normal file
View File

@ -0,0 +1,14 @@
import { Telegraf } from 'telegraf'
import { createBot } from './bot.js'
describe('bot', () => {
describe('#createBot', () => {
test('should throw error if not token passed', () => {
expect(() => createBot(undefined)).toThrow()
})
test('should return instance of bot', () => {
expect(createBot('123') instanceof Telegraf).toBeTruthy()
})
})
})

47
core/db.test.ts Normal file
View File

@ -0,0 +1,47 @@
import { createDB } from './db.js'
import * as redis from 'redis'
import { anyFunction, mock } from 'jest-mock-extended'
describe('db', () => {
describe('#createDB', () => {
test('should throw error if not uri passed', async () => {
await expect(async () => {
await createDB(undefined)
}).rejects.toThrow()
})
test('should return correct redis client', async () => {
const clientMock = mock<Awaited<ReturnType<typeof createDB>>>()
const mockFn = jest
.spyOn(redis, 'createClient')
.mockReturnValue(clientMock)
const uri = 'asd'
const client = await createDB(uri)
expect(mockFn).toBeCalledWith({ url: uri })
expect(client).toEqual(clientMock)
expect(client.connect).toBeCalledTimes(1)
})
test('should handle redis client error and reconnect', async () => {
const handler = jest.fn()
const clientMock = mock<Awaited<ReturnType<typeof createDB>>>({
on: handler,
isOpen: false,
})
clientMock.connect.mockResolvedValue()
jest.spyOn(redis, 'createClient').mockReturnValue(clientMock)
handler.mockImplementation((_, errorHandler) => {
errorHandler('testError')
})
const uri = 'asd'
const client = await createDB(uri)
expect(client).toEqual(clientMock)
expect(handler).toBeCalledWith('error', anyFunction())
expect(client.connect).toBeCalledTimes(2)
})
})
})

76
core/repository.test.ts Normal file
View File

@ -0,0 +1,76 @@
import { mock } from 'jest-mock-extended'
import type { RedisClientType } from 'redis'
import { UserRepository } from './repository.js'
import { User } from 'telegraf/types'
describe('repository', () => {
let dbMock = mock<RedisClientType<any, any, any>>()
beforeEach(() => {
dbMock = mock<RedisClientType<any, any, any>>()
})
describe('#addUsers', () => {
test('should add correct users and filter bots', async () => {
const instance = new UserRepository(dbMock)
const chatId = 1
const userOne = mock<User>({
id: 1,
username: 'test_username',
is_bot: false,
})
const userTwo = mock<User>({
id: 2,
username: 'test_username_2',
is_bot: false,
})
const users: User[] = [
mock<User>({ username: undefined }),
mock<User>({ is_bot: true }),
userOne,
userTwo,
]
await instance.addUsers(chatId, users)
expect(dbMock.hSet).toBeCalledWith(chatId.toString(), {
[userOne.id.toString()]: userOne.username,
[userTwo.id.toString()]: userTwo.username,
})
})
})
describe('#getUsernamesByChatId', () => {
test('should correct call db and return data', async () => {
const instance = new UserRepository(dbMock)
const data = {
id1: 'username1',
id2: 'username2',
}
dbMock.hGetAll.mockResolvedValue(data)
const chatId = 1
const result = await instance.getUsernamesByChatId(chatId)
expect(dbMock.hGetAll).toBeCalledWith(chatId.toString())
expect(result).toStrictEqual(Object.values(data))
})
})
describe('#deleteUser', () => {
test('should correct call db for user removing', async () => {
const instance = new UserRepository(dbMock)
const userId = 2
const chatId = 1
await instance.deleteUser(chatId, userId)
expect(dbMock.hDel).toBeCalledWith(chatId.toString(), userId.toString())
})
})
})

View File

@ -1,5 +1,5 @@
import { RedisClientType } from 'redis'
import { Chat, User } from 'telegraf/types'
import type { RedisClientType } from 'redis'
import type { Chat, User } from 'telegraf/types'
export class UserRepository {
constructor(private readonly db: RedisClientType<any, any, any>) {

16
jest.config.mjs Normal file
View File

@ -0,0 +1,16 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/?(*.)+(spec|test).[jt]s'],
moduleNameMapper: {
'^(\\..*)\\.js$': '$1',
},
moduleFileExtensions: ['js', 'ts'],
transform: {
'.ts': [
'ts-jest',
{ tsconfig: 'jest.tsconfig.json', isolatedModules: true, useESM: true },
],
},
}

3
jest.tsconfig.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}

3667
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,17 +5,21 @@
"main": "index.ts",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"start": "node ./dist/index.js",
"dev": "npm run build && npm run start",
"release": "git push evennode main",
"patch": "npm version patch"
"patch": "npm version patch",
"test": "jest --detectOpenHandles"
},
"author": "Artem Shuvaev <shuvaevlol@gmail.com>",
"license": "ISC",
"devDependencies": {
"@types/jest": "^29.5.4",
"@types/node": "^20.5.3",
"jest": "^29.6.4",
"jest-mock-extended": "^3.0.5",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
},
"dependencies": {