-
Notifications
You must be signed in to change notification settings - Fork 228
/
crud.service.spec.ts
63 lines (50 loc) · 1.85 KB
/
crud.service.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { mockDeep, type DeepMockProxy } from 'jest-mock-extended';
import { Repository } from 'typeorm';
import { Sampletable1 } from '#entity/sampledb1';
import { CrudService } from './crud.service';
let moduleRef: TestingModule | undefined;
let repository: DeepMockProxy<Repository<Sampletable1>>;
let service: CrudService;
let mockValue: Sampletable1;
beforeAll(async () => {
moduleRef = await Test.createTestingModule({
providers: [
CrudService,
{
provide: getRepositoryToken(Sampletable1),
useValue: mockDeep<Repository<Sampletable1>>(),
},
],
}).compile();
repository = moduleRef.get(getRepositoryToken(Sampletable1));
service = moduleRef.get(CrudService);
});
test('create', async () => {
const data = { title: 'FooBar', content: 'Hello World', tags: ['new'] };
mockValue = { id: 1, ...data, created_at: new Date(), updated_at: new Date() };
repository.save.mockResolvedValue(mockValue);
const result = await service.create(data);
expect(result).toHaveProperty('id', 1);
});
test('read', async () => {
repository.findOneBy.mockResolvedValue(mockValue);
const result = await service.read(1);
expect(result).toEqual(mockValue);
});
test('update', async () => {
mockValue.title = 'Blahblahblah';
mockValue.tags = ['update'];
repository.update.mockResolvedValue({ raw: '-', affected: 1, generatedMaps: [] });
const result = await service.update(1, { title: mockValue.title, tags: mockValue.tags });
expect(result).toHaveProperty('affected', 1);
});
test('delete', async () => {
repository.delete.mockResolvedValue({ raw: '-', affected: 1 });
const result = await service.remove(1);
expect(result).toHaveProperty('affected', 1);
});
afterAll(async () => {
await moduleRef?.close();
});