返回 AiToEarn
account-group.repository.spec.ts
根目录 / project / aitoearn-backend / libs / mongodb / src / repositories / account-group.repository.spec.ts
1 import type { Model } from 'mongoose'
2 import type { AccountGroup } from '../schemas/account-group.schema'
3 import { describe, expect, it, vi } from 'vitest'
4 import { AccountGroupRepository } from './account-group.repository'
5
6 vi.mock('../schemas/account-group.schema', () => ({
7 AccountGroup: class AccountGroup {},
8 }))
9
10 describe('account group repository', () => {
11 it('returns a plain object when creating an account group', async () => {
12 const accountGroup = {
13 id: 'group-1',
14 userId: 'user-1',
15 name: 'group-name',
16 isDefault: false,
17 rank: 1,
18 }
19 const created = {
20 $__: {},
21 _doc: accountGroup,
22 toObject: vi.fn(() => accountGroup),
23 }
24 const model = {
25 create: vi.fn(async () => created),
26 }
27 const repository = new AccountGroupRepository(model as unknown as Model<AccountGroup>)
28
29 const result = await repository.createAccountGroup({
30 userId: 'user-1',
31 name: 'group-name',
32 })
33
34 expect(model.create).toHaveBeenCalledWith({
35 userId: 'user-1',
36 name: 'group-name',
37 })
38 expect(created.toObject).toHaveBeenCalled()
39 expect(result).toEqual(accountGroup)
40 expect(result).not.toHaveProperty('$__')
41 expect(result).not.toHaveProperty('_doc')
42 })
43 })
44
44 lines TYPESCRIPT