-
Notifications
You must be signed in to change notification settings - Fork 5
/
sample.controller.spec.ts
64 lines (51 loc) · 1.76 KB
/
sample.controller.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
64
import { Test, TestingModule } from '@nestjs/testing';
import { Memo } from '@prisma/client';
import { PrismaService } from 'nestjs-prisma';
import { afterAll, beforeAll, expect, test } from 'vitest';
import { DeepMockProxy } from 'vitest-mock-extended';
import { SampleController } from './sample.controller';
import { SampleService } from './sample.service';
import { MockPrismaModule } from '../../test/mock';
let app: TestingModule | undefined;
let prisma: DeepMockProxy<PrismaService>;
let sample: SampleController;
let mockValue: Memo;
beforeAll(async () => {
app = await Test.createTestingModule({
imports: [MockPrismaModule],
controllers: [SampleController],
providers: [SampleService],
}).compile();
prisma = app.get(PrismaService);
sample = app.get(SampleController);
});
test('create memo', async () => {
mockValue = {
id: 1,
title: 'FooBar',
content: 'Hello World',
updatedAt: new Date(),
createdAt: new Date(),
};
prisma.memo.create.mockResolvedValueOnce(mockValue);
const result = await sample.create({ title: 'FooBar', content: 'Hello World' });
expect(result).toHaveProperty('id', 1);
});
test('read memo', async () => {
prisma.memo.findUnique.mockResolvedValueOnce(mockValue);
expect(await sample.read(1)).toHaveProperty('id', 1);
});
test('update memo', async () => {
mockValue.title = 'Blahblahblah';
prisma.memo.update.mockResolvedValueOnce(mockValue);
const result = await sample.update(1, { title: mockValue.title });
expect(result).toHaveProperty('success', true);
});
test('delete memo', async () => {
prisma.memo.delete.mockResolvedValueOnce(mockValue);
const result = await sample.remove(1);
expect(result).toHaveProperty('success', true);
});
afterAll(async () => {
await app?.close();
});