1+ import { render , screen , waitFor , act } from '@testing-library/react' ;
2+ import '@testing-library/jest-dom/extend-expect' ;
3+ import userEvent from '@testing-library/user-event' ;
4+ import {
5+ ButtonLess ,
6+ ButtonMore ,
7+ DeleteLandingPacksButton ,
8+ DeletePacksButton ,
9+ Packs ,
10+ filterPackagesEqualOrGreatherToZero ,
11+ isGreatherToMax ,
12+ isInvalidInputValue ,
13+ isLessToMin ,
14+ } from '.' ;
15+ import * as Formik from 'formik' ;
16+ import { MAX_ECOAI_PACKAGE } from '../../../../doppler-types' ;
17+ import IntlProvider from '../../../../i18n/DopplerIntlProvider.double-with-ids-as-values' ;
18+
19+ describe ( 'Packs' , ( ) => {
20+ it ( 'should to execute handleSave when input value is changed' , async ( ) => {
21+ // Arrange
22+ const packs = [
23+ {
24+ packages : 1 ,
25+ packagesQty : 0 ,
26+ } ,
27+ ] ;
28+ const useFormikContextMock = jest . spyOn ( Formik , 'useFormikContext' ) ;
29+ useFormikContextMock . mockReturnValue ( {
30+ values : {
31+ packages : packs ,
32+ } ,
33+ setFieldValue : ( ) => null ,
34+ } ) ;
35+
36+ const handleSave = jest . fn ( ) ;
37+ render (
38+ < IntlProvider >
39+ < Packs
40+ packs = { packs }
41+ handleSave = { handleSave }
42+ formRef = { ( ) => null }
43+ showRemoveLandings = { true }
44+ />
45+ </ IntlProvider > ,
46+ ) ;
47+ expect ( handleSave ) . not . toHaveBeenCalled ( ) ;
48+ const user = userEvent . setup ( ) ;
49+
50+ // Choose 3 packs of 5 landings
51+ await act ( ( ) =>
52+ user . clear ( screen . getByRole ( 'spinbutton' , { name : / p a c k a g e s .0 .p a c k a g e s Q t y / i } ) ) ,
53+ ) ;
54+ await act ( ( ) =>
55+ user . type ( screen . getByRole ( 'spinbutton' , { name : / p a c k a g e s .0 .p a c k a g e s Q t y / i } ) , '1' ) ,
56+ ) ;
57+ expect ( screen . getByRole ( 'spinbutton' , { name : / p a c k a g e s .0 .p a c k a g e s Q t y / i } ) ) . toHaveValue (
58+ 1 ,
59+ ) ;
60+ screen . getByRole ( 'button' , { name : / a i _ a g e n t _ s e l e c t i o n .r e m o v e _ f r o m _ c a r t _ b u t t o n / i } ) ;
61+ await waitFor ( ( ) => {
62+ expect ( handleSave ) . toHaveBeenCalledWith ( [
63+ {
64+ packages : 1 ,
65+ packagesQty : 1 ,
66+ } ,
67+ ] ) ;
68+ } ) ;
69+ } ) ;
70+
71+ describe ( 'isInvalidInputValue function' , ( ) => {
72+ it ( 'should return "true" when is < 0 or is > allowed limit ' , async ( ) => {
73+ // Arrange
74+ const index = 0 ;
75+
76+ // case < 0
77+ let invalidPackageQty = isInvalidInputValue (
78+ {
79+ packages : [
80+ {
81+ packagesQty : - 1 ,
82+ } ,
83+ ] ,
84+ } ,
85+ index ,
86+ ) ;
87+
88+ console . log ( invalidPackageQty ) ;
89+
90+ expect ( invalidPackageQty ) . toBeTruthy ( ) ;
91+
92+ // case > MAX_ECOAI_PACKAGE
93+ invalidPackageQty = isInvalidInputValue (
94+ {
95+ packages : [
96+ {
97+ packagesQty : MAX_ECOAI_PACKAGE + 1 ,
98+ } ,
99+ ] ,
100+ } ,
101+ index ,
102+ ) ;
103+ expect ( invalidPackageQty ) . toBeTruthy ( ) ;
104+ } ) ;
105+
106+ it ( 'should return "false" when is >= 0 or is <= allowed limit ' , async ( ) => {
107+ // Arrange
108+ const index = 0 ;
109+
110+ // case >= 0 and <= MAX_ECOAI_PACKAGE
111+ let invalidPackageQty = isInvalidInputValue (
112+ {
113+ packages : [
114+ {
115+ packagesQty : 1 ,
116+ } ,
117+ ] ,
118+ } ,
119+ index ,
120+ ) ;
121+ expect ( invalidPackageQty ) . toBeFalsy ( ) ;
122+ } ) ;
123+ } ) ;
124+
125+ describe ( 'isGreatherToMax function' , ( ) => {
126+ it ( 'should return "true" when there is a pack greather to allowed limit' , async ( ) => {
127+ // Arrange
128+ const packsSet = [
129+ {
130+ packagesQty : 1 ,
131+ } ,
132+ {
133+ packagesQty : MAX_ECOAI_PACKAGE + 1 ,
134+ } ,
135+ ] ;
136+
137+ const finded = isGreatherToMax ( packsSet ) ;
138+ expect ( finded ) . toBeTruthy ( ) ;
139+ } ) ;
140+
141+ it ( 'should return "false" when there is not a pack greather to allowed limit' , async ( ) => {
142+ // Arrange
143+ const packsSet = [
144+ {
145+ packagesQty : 1 ,
146+ } ,
147+ {
148+ packagesQty : MAX_ECOAI_PACKAGE ,
149+ } ,
150+ ] ;
151+
152+ const finded = isGreatherToMax ( packsSet ) ;
153+ expect ( finded ) . not . toBeTruthy ( ) ;
154+ } ) ;
155+ } ) ;
156+
157+ describe ( 'isLessToMin function' , ( ) => {
158+ it ( 'should return "true" when there is a pack less to 0' , async ( ) => {
159+ // Arrange
160+ const packsSet = [
161+ {
162+ packagesQty : - 2 ,
163+ } ,
164+ ] ;
165+
166+ const finded = isLessToMin ( packsSet ) ;
167+ expect ( finded ) . toBeTruthy ( ) ;
168+ } ) ;
169+
170+ it ( 'should return "false" when there is not a pack less to 0' , async ( ) => {
171+ // Arrange
172+ const packsSet = [
173+ {
174+ packagesQty : 0 ,
175+ } ,
176+ ] ;
177+
178+ const finded = isLessToMin ( packsSet ) ;
179+ expect ( finded ) . not . toBeTruthy ( ) ;
180+ } ) ;
181+ } ) ;
182+
183+
184+ describe ( 'filterPackagesEqualOrGreatherToZero function' , ( ) => {
185+ it ( 'should return just the landings packs > 0' , async ( ) => {
186+ // Arrange
187+ const packsSet = [
188+ {
189+ packagesQty : 0 ,
190+ } ,
191+ {
192+ packagesQty : 1 ,
193+ } ,
194+ ] ;
195+
196+ const landingPacksFiltered = filterPackagesEqualOrGreatherToZero ( packsSet ) ;
197+ expect ( landingPacksFiltered ) . toEqual ( [
198+ {
199+ packagesQty : 1 ,
200+ } ,
201+ ] ) ;
202+ } ) ;
203+ } ) ;
204+
205+
206+ describe ( 'ButtonLess' , ( ) => {
207+ const useFormikContextMock = jest . spyOn ( Formik , 'useFormikContext' ) ;
208+
209+ beforeEach ( ( ) => {
210+ useFormikContextMock . mockReturnValue ( {
211+ values : {
212+ packages : [
213+ {
214+ packagesQty : 1 ,
215+ } ,
216+ ] ,
217+ } ,
218+ setFieldValue : ( ) => null ,
219+ } ) ;
220+ } ) ;
221+
222+ it ( 'should render ButtonLess component' , async ( ) => {
223+ const handleInputValue = jest . fn ( ) ;
224+ expect ( handleInputValue ) . not . toHaveBeenCalled ( ) ;
225+ render ( < ButtonLess handleInputValue = { handleInputValue } index = { 0 } /> ) ;
226+ const user = userEvent . setup ( ) ;
227+
228+ await user . click ( screen . getByRole ( 'button' , { name : / b u t t o n l e s s / i } ) ) ;
229+
230+ await waitFor ( ( ) => expect ( handleInputValue ) . toHaveBeenCalled ( ) ) ;
231+ } ) ;
232+ } ) ;
233+
234+ describe ( 'ButtonMore' , ( ) => {
235+ const useFormikContextMock = jest . spyOn ( Formik , 'useFormikContext' ) ;
236+
237+ beforeEach ( ( ) => {
238+ useFormikContextMock . mockReturnValue ( {
239+ values : {
240+ packages : [
241+ {
242+ packagesQty : 2 ,
243+ } ,
244+ ] ,
245+ } ,
246+ setFieldValue : ( ) => null ,
247+ } ) ;
248+ } ) ;
249+
250+ it ( 'should render ButtonMore component' , async ( ) => {
251+ const handleInputValue = jest . fn ( ) ;
252+ expect ( handleInputValue ) . not . toHaveBeenCalled ( ) ;
253+ render ( < ButtonMore handleInputValue = { handleInputValue } index = { 0 } /> ) ;
254+ const user = userEvent . setup ( ) ;
255+
256+ await user . click ( screen . getByRole ( 'button' , { name : / b u t t o n m o r e / i } ) ) ;
257+
258+ await waitFor ( ( ) => expect ( handleInputValue ) . toHaveBeenCalled ( ) ) ;
259+ } ) ;
260+ } ) ;
261+ } ) ;
0 commit comments