@@ -8,126 +8,150 @@ const {
88} = require ( '../interface-adapters/controllers/products/product-controller' ) ;
99
1010describe ( 'Product Controller Unit Tests' , ( ) => {
11- it ( 'should create a product (mocked)' , async ( ) => {
12- const createProductUseCaseHandler = jest . fn ( ) . mockResolvedValue ( { id : '123' , name : 'Test' , price : 10 , description : 'desc' , category : 'cat' , createdBy : 'u1' } ) ;
13- const dbProductHandler = { createProductDbHandler : jest . fn ( ) } ;
14- const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
15- const logEvents = jest . fn ( ) ;
16- const handler = createProductController ( {
17- createProductUseCaseHandler,
18- dbProductHandler,
19- errorHandlers,
20- logEvents,
21- } ) ;
22- const httpRequest = { body : { name : 'Test' , price : 10 , description : 'desc' , category : 'cat' , createdBy : 'u1' } } ;
23- const response = await handler ( httpRequest ) ;
24- expect ( [ 200 , 201 ] ) . toContain ( response . statusCode ) ;
25- expect ( response . data ) . toEqual ( { createdProduct : { id : '123' , name : 'Test' , price : 10 , description : 'desc' , category : 'cat' , createdBy : 'u1' } } ) ;
11+ it ( 'should create a product (mocked)' , async ( ) => {
12+ const createProductUseCaseHandler = jest . fn ( ) . mockResolvedValue ( {
13+ id : '123' ,
14+ name : 'Test' ,
15+ price : 10 ,
16+ description : 'desc' ,
17+ category : 'cat' ,
18+ createdBy : 'u1' ,
2619 } ) ;
20+ const dbProductHandler = { createProductDbHandler : jest . fn ( ) } ;
21+ const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
22+ const logEvents = jest . fn ( ) ;
23+ const handler = createProductController ( {
24+ createProductUseCaseHandler,
25+ dbProductHandler,
26+ errorHandlers,
27+ logEvents,
28+ } ) ;
29+ const httpRequest = {
30+ body : {
31+ name : 'Test' ,
32+ price : 10 ,
33+ description : 'desc' ,
34+ category : 'cat' ,
35+ createdBy : 'u1' ,
36+ } ,
37+ } ;
38+ const response = await handler ( httpRequest ) ;
39+ expect ( [ 200 , 201 ] ) . toContain ( response . statusCode ) ;
40+ expect ( response . data ) . toEqual ( {
41+ createdProduct : {
42+ id : '123' ,
43+ name : 'Test' ,
44+ price : 10 ,
45+ description : 'desc' ,
46+ category : 'cat' ,
47+ createdBy : 'u1' ,
48+ } ,
49+ } ) ;
50+ } ) ;
2751
28- it ( 'should return 400 if no product data provided' , async ( ) => {
29- const createProductUseCaseHandler = jest . fn ( ) ;
30- const dbProductHandler = { createProductDbHandler : jest . fn ( ) } ;
31- const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
32- const logEvents = jest . fn ( ) ;
33- const handler = createProductController ( {
34- createProductUseCaseHandler,
35- dbProductHandler,
36- errorHandlers,
37- logEvents,
38- } ) ;
39- const httpRequest = { body : { } } ;
40- const response = await handler ( httpRequest ) ;
41- expect ( response . statusCode ) . toBe ( 400 ) ;
42- expect ( response . errorMessage ) . toBe ( 'No product data provided' ) ;
52+ it ( 'should return 400 if no product data provided' , async ( ) => {
53+ const createProductUseCaseHandler = jest . fn ( ) ;
54+ const dbProductHandler = { createProductDbHandler : jest . fn ( ) } ;
55+ const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
56+ const logEvents = jest . fn ( ) ;
57+ const handler = createProductController ( {
58+ createProductUseCaseHandler,
59+ dbProductHandler,
60+ errorHandlers,
61+ logEvents,
4362 } ) ;
63+ const httpRequest = { body : { } } ;
64+ const response = await handler ( httpRequest ) ;
65+ expect ( response . statusCode ) . toBe ( 400 ) ;
66+ expect ( response . errorMessage ) . toBe ( 'No product data provided' ) ;
67+ } ) ;
4468
45- it ( 'should get all products (mocked)' , async ( ) => {
46- const findAllProductUseCaseHandler = jest . fn ( ) . mockResolvedValue ( [ { id : '1' } , { id : '2' } ] ) ;
47- const dbProductHandler = { findAllProductsDbHandler : jest . fn ( ) } ;
48- const logEvents = jest . fn ( ) ;
49- const handler = findAllProductController ( {
50- dbProductHandler,
51- findAllProductUseCaseHandler,
52- logEvents,
53- } ) ;
54- const httpRequest = { query : { } } ;
55- const response = await handler ( httpRequest ) ;
56- expect ( [ 200 , 201 ] ) . toContain ( response . statusCode ) ;
57- expect ( Array . isArray ( response . data . products ) ) . toBe ( true ) ;
69+ it ( 'should get all products (mocked)' , async ( ) => {
70+ const findAllProductUseCaseHandler = jest . fn ( ) . mockResolvedValue ( [ { id : '1' } , { id : '2' } ] ) ;
71+ const dbProductHandler = { findAllProductsDbHandler : jest . fn ( ) } ;
72+ const logEvents = jest . fn ( ) ;
73+ const handler = findAllProductController ( {
74+ dbProductHandler,
75+ findAllProductUseCaseHandler,
76+ logEvents,
5877 } ) ;
78+ const httpRequest = { query : { } } ;
79+ const response = await handler ( httpRequest ) ;
80+ expect ( [ 200 , 201 ] ) . toContain ( response . statusCode ) ;
81+ expect ( Array . isArray ( response . data . products ) ) . toBe ( true ) ;
82+ } ) ;
5983
60- it ( 'should get a product by id (mocked)' , async ( ) => {
61- const findOneProductUseCaseHandler = jest . fn ( ) . mockResolvedValue ( { id : '1' , name : 'Test' } ) ;
62- const dbProductHandler = { findOneProductDbHandler : jest . fn ( ) } ;
63- const logEvents = jest . fn ( ) ;
64- const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
65- const handler = findOneProductController ( {
66- dbProductHandler,
67- findOneProductUseCaseHandler,
68- logEvents,
69- errorHandlers,
70- } ) ;
71- const httpRequest = { params : { productId : '1' } } ;
72- const response = await handler ( httpRequest ) ;
73- expect ( [ 200 , 201 ] ) . toContain ( response . statusCode ) ;
74- expect ( response . data . product ) . toEqual ( { id : '1' , name : 'Test' } ) ;
84+ it ( 'should get a product by id (mocked)' , async ( ) => {
85+ const findOneProductUseCaseHandler = jest . fn ( ) . mockResolvedValue ( { id : '1' , name : 'Test' } ) ;
86+ const dbProductHandler = { findOneProductDbHandler : jest . fn ( ) } ;
87+ const logEvents = jest . fn ( ) ;
88+ const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
89+ const handler = findOneProductController ( {
90+ dbProductHandler,
91+ findOneProductUseCaseHandler,
92+ logEvents,
93+ errorHandlers,
7594 } ) ;
95+ const httpRequest = { params : { productId : '1' } } ;
96+ const response = await handler ( httpRequest ) ;
97+ expect ( [ 200 , 201 ] ) . toContain ( response . statusCode ) ;
98+ expect ( response . data . product ) . toEqual ( { id : '1' , name : 'Test' } ) ;
99+ } ) ;
76100
77- it ( 'should update a product (mocked)' , async ( ) => {
78- const updateProductUseCaseHandler = jest . fn ( ) . mockResolvedValue ( { id : '1' , name : 'Updated' } ) ;
79- const dbProductHandler = {
80- findOneProductDbHandler : jest . fn ( ) ,
81- updateProductDbHandler : jest . fn ( ) ,
82- } ;
83- const logEvents = jest . fn ( ) ;
84- const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
85- const handler = updateProductController ( {
86- dbProductHandler,
87- updateProductUseCaseHandler,
88- logEvents,
89- errorHandlers,
90- } ) ;
91- const httpRequest = { params : { productId : '1' } , body : { name : 'Updated' } } ;
92- const response = await handler ( httpRequest ) ;
93- expect ( [ 200 , 201 ] ) . toContain ( response . statusCode ) ;
94- expect ( response . data ) . toContain ( 'Updated' ) ;
101+ it ( 'should update a product (mocked)' , async ( ) => {
102+ const updateProductUseCaseHandler = jest . fn ( ) . mockResolvedValue ( { id : '1' , name : 'Updated' } ) ;
103+ const dbProductHandler = {
104+ findOneProductDbHandler : jest . fn ( ) ,
105+ updateProductDbHandler : jest . fn ( ) ,
106+ } ;
107+ const logEvents = jest . fn ( ) ;
108+ const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
109+ const handler = updateProductController ( {
110+ dbProductHandler,
111+ updateProductUseCaseHandler,
112+ logEvents,
113+ errorHandlers,
95114 } ) ;
115+ const httpRequest = { params : { productId : '1' } , body : { name : 'Updated' } } ;
116+ const response = await handler ( httpRequest ) ;
117+ expect ( [ 200 , 201 ] ) . toContain ( response . statusCode ) ;
118+ expect ( response . data ) . toContain ( 'Updated' ) ;
119+ } ) ;
96120
97- it ( 'should delete a product (mocked)' , async ( ) => {
98- const deleteProductUseCaseHandler = jest . fn ( ) . mockResolvedValue ( { deletedCount : 1 } ) ;
99- const dbProductHandler = {
100- findOneProductDbHandler : jest . fn ( ) ,
101- deleteProductDbHandler : jest . fn ( ) ,
102- } ;
103- const logEvents = jest . fn ( ) ;
104- const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
105- const handler = deleteProductController ( {
106- dbProductHandler,
107- deleteProductUseCaseHandler,
108- logEvents,
109- errorHandlers,
110- } ) ;
111- const httpRequest = { params : { productId : '1' } } ;
112- const response = await handler ( httpRequest ) ;
113- expect ( [ 200 , 201 ] ) . toContain ( response . statusCode ) ;
114- expect ( response . data . deletedCount ) . toBe ( 1 ) ;
121+ it ( 'should delete a product (mocked)' , async ( ) => {
122+ const deleteProductUseCaseHandler = jest . fn ( ) . mockResolvedValue ( { deletedCount : 1 } ) ;
123+ const dbProductHandler = {
124+ findOneProductDbHandler : jest . fn ( ) ,
125+ deleteProductDbHandler : jest . fn ( ) ,
126+ } ;
127+ const logEvents = jest . fn ( ) ;
128+ const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
129+ const handler = deleteProductController ( {
130+ dbProductHandler,
131+ deleteProductUseCaseHandler,
132+ logEvents,
133+ errorHandlers,
115134 } ) ;
135+ const httpRequest = { params : { productId : '1' } } ;
136+ const response = await handler ( httpRequest ) ;
137+ expect ( [ 200 , 201 ] ) . toContain ( response . statusCode ) ;
138+ expect ( response . data . deletedCount ) . toBe ( 1 ) ;
139+ } ) ;
116140
117- it ( 'should handle DB error on create' , async ( ) => {
118- const createProductUseCaseHandler = jest . fn ( ) . mockRejectedValue ( new Error ( 'DB error' ) ) ;
119- const dbProductHandler = { createProductDbHandler : jest . fn ( ) } ;
120- const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
121- const logEvents = jest . fn ( ) ;
122- const handler = createProductController ( {
123- createProductUseCaseHandler,
124- dbProductHandler,
125- errorHandlers,
126- logEvents,
127- } ) ;
128- const httpRequest = { body : { name : 'Test' } } ;
129- const response = await handler ( httpRequest ) ;
130- expect ( [ 200 , 201 , 400 , 500 ] ) . toContain ( response . statusCode ) ;
131- expect ( response . errorMessage ) . toBe ( 'DB error' ) ;
141+ it ( 'should handle DB error on create' , async ( ) => {
142+ const createProductUseCaseHandler = jest . fn ( ) . mockRejectedValue ( new Error ( 'DB error' ) ) ;
143+ const dbProductHandler = { createProductDbHandler : jest . fn ( ) } ;
144+ const errorHandlers = { UniqueConstraintError : Error , InvalidPropertyError : Error } ;
145+ const logEvents = jest . fn ( ) ;
146+ const handler = createProductController ( {
147+ createProductUseCaseHandler,
148+ dbProductHandler,
149+ errorHandlers,
150+ logEvents,
132151 } ) ;
152+ const httpRequest = { body : { name : 'Test' } } ;
153+ const response = await handler ( httpRequest ) ;
154+ expect ( [ 200 , 201 , 400 , 500 ] ) . toContain ( response . statusCode ) ;
155+ expect ( response . errorMessage ) . toBe ( 'DB error' ) ;
156+ } ) ;
133157} ) ;
0 commit comments