Compare commits
2 Commits
c02c0c7923
...
c95a28bab2
Author | SHA1 | Date | |
---|---|---|---|
c95a28bab2 | |||
6aa678fcfe |
@ -1,42 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { PostController } from './post.controller';
|
|
||||||
import { PostService } from './post.service';
|
|
||||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
||||||
import { Post } from './post.entity';
|
|
||||||
import { Comment } from '../comment/comment.entity';
|
|
||||||
|
|
||||||
const mockPost = new Post();
|
|
||||||
const mockComment = new Comment();
|
|
||||||
|
|
||||||
describe('PostController', () => {
|
|
||||||
let controller: PostController;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [PostController],
|
|
||||||
providers: [
|
|
||||||
PostService,
|
|
||||||
{
|
|
||||||
provide: getRepositoryToken(Post),
|
|
||||||
useValue: {
|
|
||||||
save: jest.fn().mockResolvedValue(mockPost),
|
|
||||||
find: jest.fn().mockResolvedValue([mockPost]),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: getRepositoryToken(Comment),
|
|
||||||
useValue: {
|
|
||||||
save: jest.fn().mockResolvedValue(mockComment),
|
|
||||||
find: jest.fn().mockResolvedValue([mockComment]),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
controller = module.get<PostController>(PostController);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
@ -1,19 +0,0 @@
|
|||||||
import { Controller, Get, Param } from '@nestjs/common';
|
|
||||||
import { PostService } from './post.service';
|
|
||||||
|
|
||||||
@Controller('posts')
|
|
||||||
export class PostController {
|
|
||||||
|
|
||||||
constructor(private readonly postService: PostService) {}
|
|
||||||
|
|
||||||
@Get()
|
|
||||||
async findAll() {
|
|
||||||
return this.postService.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get(':id/comments')
|
|
||||||
async findCommentsByPostId(@Param('id') postId: string) {
|
|
||||||
return this.postService.findCommentsByPostId(+postId);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
import { Post } from './post.entity';
|
|
||||||
|
|
||||||
describe('Post', () => {
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(new Post()).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
@ -1,19 +0,0 @@
|
|||||||
import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
|
|
||||||
import { Comment } from '../comment/comment.entity';
|
|
||||||
|
|
||||||
@Entity()
|
|
||||||
export class Post {
|
|
||||||
|
|
||||||
@PrimaryGeneratedColumn()
|
|
||||||
id: number;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
title: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
content: string;
|
|
||||||
|
|
||||||
@OneToMany(() => Comment, (comment) => comment.post)
|
|
||||||
comments: Comment[];
|
|
||||||
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { PostController } from './post.controller';
|
|
||||||
import { PostService } from './post.service';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
controllers: [PostController],
|
|
||||||
providers: [PostService]
|
|
||||||
})
|
|
||||||
export class PostModule {}
|
|
@ -1,42 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
|
||||||
import { PostService } from './post.service';
|
|
||||||
import { PostController } from './post.controller';
|
|
||||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
||||||
import { Post } from './post.entity';
|
|
||||||
import { Comment } from '../comment/comment.entity';
|
|
||||||
|
|
||||||
const mockPost = new Post();
|
|
||||||
const mockComment = new Comment();
|
|
||||||
|
|
||||||
describe('PostService', () => {
|
|
||||||
let service: PostService;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [PostController],
|
|
||||||
providers: [
|
|
||||||
PostService,
|
|
||||||
{
|
|
||||||
provide: getRepositoryToken(Post),
|
|
||||||
useValue: {
|
|
||||||
save: jest.fn().mockResolvedValue(mockPost),
|
|
||||||
find: jest.fn().mockResolvedValue([mockPost]),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: getRepositoryToken(Comment),
|
|
||||||
useValue: {
|
|
||||||
save: jest.fn().mockResolvedValue(mockComment),
|
|
||||||
find: jest.fn().mockResolvedValue([mockComment]),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}).compile();
|
|
||||||
|
|
||||||
service = module.get<PostService>(PostService);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
@ -1,27 +0,0 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
|
||||||
import { Repository } from 'typeorm';
|
|
||||||
import { Post } from './post.entity';
|
|
||||||
import { Comment } from '../comment/comment.entity';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class PostService {
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
@InjectRepository(Post)
|
|
||||||
private postRepository: Repository<Post>,
|
|
||||||
|
|
||||||
@InjectRepository(Comment)
|
|
||||||
private commentRepository: Repository<Comment>,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
async findAll(): Promise<Post[]> {
|
|
||||||
return this.postRepository.find();
|
|
||||||
}
|
|
||||||
|
|
||||||
async findCommentsByPostId(postId: number): Promise<Comment[]> {
|
|
||||||
return this.commentRepository.find({
|
|
||||||
where: { post: { id: postId } },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user