Skip to content

Commit

Permalink
Add getTermGetController
Browse files Browse the repository at this point in the history
  • Loading branch information
mapeveri committed Nov 5, 2024
1 parent b909c84 commit c7caf06
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/languages/app/controllers/v1/terms/termGetController.ts
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 || {};
}
}
5 changes: 5 additions & 0 deletions src/languages/application/term/query/findTermQuery.ts
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) {}
}
54 changes: 54 additions & 0 deletions test/acceptance/term/getTermAcceptance.test.ts
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);
});
});
});
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);
});
});
});

0 comments on commit c7caf06

Please sign in to comment.