1+ import express , { Application } from "express" ;
2+ import { afterEach , beforeAll , describe , expect , test , vi } from "vitest" ;
3+ import { MockProxy , mock , mockReset } from "vitest-mock-extended" ;
4+ import { RecuperarCategoriaPorIdExpressController } from "./controllers/recuperar-categoria-por-id.express.controller" ;
5+ import { InserirCategoriaExpressController } from "./controllers/inserir-categoria.express.controller" ;
6+ import { CriarCategoriaProps , ICategoria } from "@modules/catalogo/domain/categoria/categoria.types" ;
7+ import request from 'supertest' ;
8+
9+ let appMock : Application ;
10+ let recuperarCategoriaPorIdControllerMock : MockProxy < RecuperarCategoriaPorIdExpressController > ;
11+ let inserirCategoriaControllerMock : MockProxy < InserirCategoriaExpressController > ;
12+
13+ describe ( '[REST] Rotas Express: Categoria' , ( ) => {
14+
15+ beforeAll ( async ( ) => {
16+ appMock = express ( ) ;
17+ recuperarCategoriaPorIdControllerMock = mock < RecuperarCategoriaPorIdExpressController > ( ) ;
18+ inserirCategoriaControllerMock = mock < InserirCategoriaExpressController > ( ) ;
19+ } ) ;
20+
21+ afterEach ( ( ) => {
22+ vi . restoreAllMocks ( ) ;
23+ mockReset ( recuperarCategoriaPorIdControllerMock ) ;
24+ mockReset ( inserirCategoriaControllerMock ) ;
25+ } ) ;
26+
27+ describe ( 'GET api/v1/categorias/:id' , ( ) => {
28+
29+ test ( 'Deve Retornar Status 200 e um Objeto do Tipo ICategoria no formato JSON' , async ( ) => {
30+
31+ //Dado (Given)
32+ const categoriaInputDTO : ICategoria = {
33+ id : "80830927-8c3e-4db9-9ddf-30ea191f139b" ,
34+ nome : "Cama"
35+ }
36+
37+ recuperarCategoriaPorIdControllerMock . recuperar . mockImplementation ( async ( request , response , next ) => {
38+ response . status ( 200 ) . json ( categoriaInputDTO ) ;
39+ } ) ;
40+
41+ appMock . use ( '/api/v1/categorias/:id' , recuperarCategoriaPorIdControllerMock . recuperar ) ;
42+
43+ //Quando (When)
44+ const response = await request ( appMock )
45+ . get ( '/api/v1/categorias/80830927-8c3e-4db9-9ddf-30ea191f139b' )
46+
47+ //Então (Then
48+ expect ( response . status ) . toEqual ( 200 ) ;
49+ expect ( response . headers [ "content-type" ] ) . toMatch ( / j s o n / ) ;
50+ expect ( response . body ) . toEqual ( categoriaInputDTO ) ;
51+
52+ } ) ;
53+
54+ } ) ;
55+
56+ describe ( 'POST api/v1/categorias' , ( ) => {
57+
58+ test ( 'Deve Retornar Status 200 e um Objeto do Tipo ICategoria no formato JSON' , async ( ) => {
59+
60+ //Dado (Given)
61+ const categoriaInputDTO : CriarCategoriaProps = {
62+ nome : "Cama"
63+ } ;
64+
65+ const categoriaOutputDTO : ICategoria = {
66+ id : "80830927-8c3e-4db9-9ddf-30ea191f139b" ,
67+ nome : "Cama"
68+ }
69+
70+ inserirCategoriaControllerMock . inserir . mockImplementation ( async ( request , response , next ) => {
71+ response . status ( 200 ) . json ( categoriaOutputDTO ) ;
72+ } ) ;
73+
74+ appMock . use ( '/api/v1/categorias' , inserirCategoriaControllerMock . inserir ) ; // Cast to any for mocking purposes
75+
76+ //Quando (When)
77+ const response = await request ( appMock )
78+ . post ( '/api/v1/categorias' )
79+ . send ( categoriaInputDTO )
80+
81+ //Então (Then
82+ expect ( response . status ) . toEqual ( 200 ) ;
83+ expect ( response . headers [ "content-type" ] ) . toMatch ( / j s o n / ) ;
84+ expect ( response . body ) . toEqual ( categoriaOutputDTO ) ;
85+
86+ } ) ;
87+
88+ } ) ;
89+
90+ } ) ;
0 commit comments