1+ const { validateInput } = require ( '../lambdas/validation' ) ;
2+ const { MESSAGES } = require ( '../lambdas/constants' ) ;
3+ const ProductBuilder = require ( '../utils/productBuilder' ) ;
4+
5+ describe ( 'validateInput function' , ( ) => {
6+ it ( 'should return null if input is valid' , ( ) => {
7+ const input = new ProductBuilder ( ) . build ( ) ;
8+ const result = validateInput ( input ) ;
9+ expect ( result ) . toBeNull ( ) ;
10+ } ) ;
11+
12+ describe . each ( [ { } , null , undefined , NaN , '' , '\t' , '\n' ] ) (
13+ 'should return INVALID_INPUT_TITLE if description is "%s"' ,
14+ ( title ) => {
15+ it ( `should return ${ MESSAGES . INVALID_INPUT_TITLE } ` , ( ) => {
16+ const input = new ProductBuilder ( ) . withTitle ( title ) . build ( ) ;
17+ const result = validateInput ( input ) ;
18+ expect ( result ) . toEqual ( MESSAGES . INVALID_INPUT_TITLE ) ;
19+ } ) ;
20+ }
21+ ) ;
22+
23+ describe . each ( [ { } , null , undefined , NaN , '' , '\t' , '\n' ] ) (
24+ 'should return INVALID_INPUT_DESCRIPTION if description is "%s"' ,
25+ ( description ) => {
26+ it ( `should return ${ MESSAGES . INVALID_INPUT_DESCRIPTION } ` , ( ) => {
27+ const input = new ProductBuilder ( ) . withDescription ( description ) . build ( ) ;
28+ const result = validateInput ( input ) ;
29+ expect ( result ) . toEqual ( MESSAGES . INVALID_INPUT_DESCRIPTION ) ;
30+ } ) ;
31+ }
32+ ) ;
33+
34+ describe . each ( [ - 2 , - 1 , { } , null , undefined , NaN , '' , '\t' , '\n' ] ) (
35+ 'should return INVALID_INPUT_PRICE if price is invalid: "%s"' ,
36+ ( price ) => {
37+ it ( 'should return INVALID_INPUT_PRICE' , ( ) => {
38+ const input = new ProductBuilder ( ) . withPrice ( price ) . build ( ) ;
39+ const result = validateInput ( input ) ;
40+ expect ( result ) . toEqual ( MESSAGES . INVALID_INPUT_PRICE ) ;
41+ } ) ;
42+ }
43+ ) ;
44+
45+ describe . each ( [ - 2 , - 1 , { } , null , undefined , NaN , '' , '\t' , '\n' ] ) (
46+ 'should return INVALID_INPUT_COUNT if count is less than zero: "%s"' ,
47+ ( count ) => {
48+ it ( 'should return INVALID_INPUT_COUNT' , ( ) => {
49+ const input = new ProductBuilder ( ) . withCount ( count ) . build ( ) ;
50+ const result = validateInput ( input ) ;
51+ expect ( result ) . toEqual ( MESSAGES . INVALID_INPUT_COUNT ) ;
52+ } ) ;
53+ }
54+ ) ;
55+ } ) ;
0 commit comments