Created
June 25, 2019 17:07
-
-
Save qWici/d304784cc3b8a8f7992baad4955083a5 to your computer and use it in GitHub Desktop.
Testing controller with jest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Test, TestingModule } from '@nestjs/testing'; | |
import { HeroController } from './hero.controller'; | |
import { HeroService } from './hero.service'; | |
import { Hero } from './hero.entity'; | |
import { PrimaryAttribute } from './hero.primary-attribute'; | |
import { Repository } from 'typeorm'; | |
import { NotFoundException } from '@nestjs/common'; | |
describe('Hero Controller', () => { | |
let controller: HeroController; | |
let service: HeroService; | |
beforeEach(async () => { | |
const module: TestingModule = await Test.createTestingModule({ | |
controllers: [HeroController], | |
providers: [ | |
{ | |
provide: HeroService, | |
useValue: new HeroService(new Repository<Hero>()), | |
}, | |
], | |
}).compile(); | |
controller = module.get<HeroController>(HeroController); | |
service = module.get<HeroService>(HeroService); | |
}); | |
const createHero = | |
(id: number, name: string, slug: string, primary_attr: PrimaryAttribute, icon: string, popularity: number, winrate: number) => { | |
const hero = new Hero(); | |
hero.id = id; | |
hero.name = name; | |
hero.slug = slug; | |
hero.primary_attr = primary_attr; | |
hero.icon = icon; | |
hero.popularity = popularity; | |
hero.winrate = winrate; | |
return hero; | |
}; | |
describe('bySlug', () => { | |
it('should return NotFoundException', async () => { | |
jest.spyOn(service, 'getBySlug').mockImplementation(() => Promise.resolve(undefined)); | |
expect(await controller.bySlug({ slug: 'axe' })).toThrow(NotFoundException); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Controller, | |
Get, | |
Request, | |
BadRequestException, | |
NotFoundException, | |
} from '@nestjs/common'; | |
import { HeroService } from './hero.service'; | |
import { ApiUseTags, ApiResponse } from '@nestjs/swagger'; | |
@Controller('api/hero') | |
@ApiUseTags('hero') | |
export class HeroController { | |
constructor(private readonly heroService: HeroService) {} | |
@Get('all') | |
@ApiResponse({ status: 200, description: 'Successful Response' }) | |
async all() { | |
return await this.heroService.all(); | |
} | |
@Get(':slug') | |
@ApiResponse({ status: 200, description: 'Successful Response' }) | |
@ApiResponse({ | |
status: 404, | |
description: 'Hero with provided slug not found', | |
}) | |
async bySlug(@Request() request): Promise<any> { | |
const hero = await this.heroService.getBySlug(request.slug); | |
if (!hero) { | |
throw new NotFoundException('Hero with provided slug not found'); | |
} | |
return hero; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, NotAcceptableException } from '@nestjs/common'; | |
import { InjectRepository } from '@nestjs/typeorm'; | |
import { Hero, HeroFillableFields } from './hero.entity'; | |
import { Repository } from 'typeorm'; | |
@Injectable() | |
export class HeroService { | |
constructor( | |
@InjectRepository(Hero) | |
private readonly heroRepository: Repository<Hero>, | |
) {} | |
async get(id: number) { | |
return this.heroRepository.findOne(id); | |
} | |
async getBySlug(slug: string) { | |
return await this.heroRepository | |
.createQueryBuilder('heroes') | |
.where('heroes.slug = :slug') | |
.setParameter('slug', slug) | |
.getOne(); | |
} | |
async all() { | |
return await this.heroRepository.find(); | |
} | |
async create(payload: HeroFillableFields) { | |
const hero = await this.getBySlug(payload.slug); | |
if (hero) { | |
throw new NotAcceptableException( | |
'Hero with provided slug already created', | |
); | |
} | |
return await this.heroRepository.save(this.heroRepository.create(payload)); | |
} | |
} |
Author
qWici
commented
Jun 25, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment