-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/languages/app/controllers/v1/terms/termGetController.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Controller, Get, HttpCode, Inject, Param, UseGuards } from '@nestjs/common'; | ||
import { NestJwtAuthGuard } from '@src/shared/guards/nestJwtAuthGuard'; | ||
import { | ||
ApiBadRequestResponse, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
import { TermsResponse } from './termsResponse'; | ||
import { TermResponse } from '@src/languages/app/controllers/v1/terms/termResponse'; | ||
import { QUERY_BUS, QueryBus } from '@src/shared/domain/bus/queryBus/queryBus'; | ||
import FindTermQuery from '@src/languages/application/term/query/findTermQuery'; | ||
|
||
@ApiTags('Terms') | ||
@Controller() | ||
export default class TermGetController { | ||
public constructor(@Inject(QUERY_BUS) private queryBus: QueryBus) {} | ||
|
||
@Get('terms/:id') | ||
@HttpCode(200) | ||
@ApiOkResponse({ type: TermsResponse }) | ||
@ApiBadRequestResponse({ description: 'Bad Request.' }) | ||
@ApiUnauthorizedResponse({ description: 'Unauthorized.' }) | ||
@ApiInternalServerErrorResponse({ description: 'Internal Server Error.' }) | ||
@UseGuards(NestJwtAuthGuard) | ||
async run(@Param('id') id: string): Promise<TermResponse> { | ||
const response = await this.queryBus.ask(new FindTermQuery(id)); | ||
return response.content || {}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Query } from '@src/shared/domain/bus/queryBus/query'; | ||
|
||
export default class FindTermQuery implements Query { | ||
constructor(public readonly id: string) {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { beforeAll, describe, beforeEach, afterAll, it } from '@jest/globals'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import request = require('supertest'); | ||
import { MikroORM } from '@mikro-orm/core'; | ||
import { createApplication, truncateTables } from '@test/acceptance/createApplication'; | ||
import WordMother from '@test/unit/languages/domain/term/word/wordMother'; | ||
import { TermIdMother } from '@test/unit/languages/domain/term/termIdMother'; | ||
|
||
describe('Given a TermGetController to handle', () => { | ||
let app: INestApplication; | ||
let orm: MikroORM; | ||
|
||
const prepareApp = async () => { | ||
const setup = await createApplication(); | ||
app = setup.app; | ||
orm = setup.orm; | ||
}; | ||
|
||
const closeApp = async () => { | ||
await orm.close(true); | ||
await app.close(); | ||
}; | ||
|
||
beforeAll(async () => { | ||
await prepareApp(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await closeApp(); | ||
}); | ||
|
||
describe('As a user I want to get a term', () => { | ||
const termId = TermIdMother.random().toString(); | ||
|
||
async function startScenario() { | ||
await truncateTables(orm); | ||
|
||
const word = WordMother.random({ id: TermIdMother.random(termId) }).toPrimitives(); | ||
const wordData = { | ||
countryId: word.countryId, | ||
languageId: word.languageId, | ||
terms: word.terms, | ||
id: word.id, | ||
}; | ||
await request(app.getHttpServer()).post('/words').set('Authorization', 'Bearer mock-token').send(wordData); | ||
} | ||
|
||
beforeEach(startScenario); | ||
|
||
it('should return the term', async () => { | ||
await request(app.getHttpServer()).get(`/term/${termId}`).set('Authorization', 'Bearer mock-token').expect(200); | ||
}); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
test/unit/languages/app/controllers/v1/terms/termGetController.test.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { beforeAll, describe, it, expect, jest } from '@jest/globals'; | ||
import TermGetController from '@src/languages/app/controllers/v1/terms/termGetController'; | ||
import WordMother from '@test/unit/languages/domain/term/word/wordMother'; | ||
import { QueryBus } from '@src/shared/domain/bus/queryBus/queryBus'; | ||
|
||
describe('Given a TermGetController to handle', () => { | ||
let sut: TermGetController; | ||
let queryBusMock: { ask: jest.Mock }; | ||
|
||
const prepareDependencies = () => { | ||
queryBusMock = { | ||
ask: jest.fn(), | ||
}; | ||
}; | ||
|
||
const initHandler = () => { | ||
sut = new TermGetController(queryBusMock as QueryBus); | ||
|
||
jest.useFakeTimers(); | ||
}; | ||
|
||
beforeAll(() => { | ||
prepareDependencies(); | ||
initHandler(); | ||
}); | ||
|
||
describe('When received a request', () => { | ||
const word = WordMother.random().toPrimitives(); | ||
|
||
it('should return a term', async () => { | ||
queryBusMock.ask.mockReturnValue({ content: word }); | ||
const response = await sut.run(word.id); | ||
|
||
expect(response).toEqual(word); | ||
}); | ||
}); | ||
}); |