@@ -62,6 +62,41 @@ it('Alpha direction = vertical', async () => {
6262 if ( tree && ! Array . isArray ( tree ) ) {
6363 expect ( tree . type ) . toEqual ( 'div' ) ;
6464 expect ( tree . props . className ) . toEqual ( 'w-color-alpha w-color-alpha-vertical' ) ;
65+ expect ( tree . children ?. [ 0 ] ) . toMatchObject ( {
66+ type : 'div' ,
67+ props : {
68+ style : expect . objectContaining ( {
69+ background : 'linear-gradient(to bottom, hsla(0, 0%, 68%, 1) 0%, rgba(244, 67, 54, 0) 100%)' ,
70+ } ) ,
71+ } ,
72+ } ) ;
73+ }
74+ } ) ;
75+
76+ it ( 'Alpha reverse renders opposite horizontal gradient and pointer position' , async ( ) => {
77+ const component = TestRenderer . create ( < Alpha hsva = { { h : 0 , s : 0 , v : 68 , a : 0.25 } } reverse /> ) ;
78+ const tree = component . toJSON ( ) ;
79+ if ( tree && ! Array . isArray ( tree ) ) {
80+ expect ( tree . children ?. [ 0 ] ) . toMatchObject ( {
81+ type : 'div' ,
82+ props : {
83+ style : expect . objectContaining ( {
84+ background : 'linear-gradient(to right, hsla(0, 0%, 68%, 1) 0%, rgba(244, 67, 54, 0) 100%)' ,
85+ } ) ,
86+ } ,
87+ } ) ;
88+ expect ( tree . children ?. [ 1 ] ) . toMatchObject ( {
89+ type : 'div' ,
90+ children : expect . arrayContaining ( [
91+ expect . objectContaining ( {
92+ props : expect . objectContaining ( {
93+ style : expect . objectContaining ( {
94+ left : '75%' ,
95+ } ) ,
96+ } ) ,
97+ } ) ,
98+ ] ) ,
99+ } ) ;
65100 }
66101} ) ;
67102
@@ -100,7 +135,118 @@ it('Alpha direction = vertical & onChange', async () => {
100135 /> ,
101136 ) ;
102137 const elm = getByTitle ( 'test' ) ;
103- fireEvent ( elm , new FakeMouseEvent ( 'mousedown' , { pageX : 20 , pageY : 1 } ) ) ;
138+ jest . spyOn ( elm , 'getBoundingClientRect' ) . mockReturnValue ( {
139+ x : 0 ,
140+ y : 0 ,
141+ top : 0 ,
142+ left : 0 ,
143+ bottom : 100 ,
144+ right : 20 ,
145+ width : 20 ,
146+ height : 100 ,
147+ toJSON : ( ) => ( { } ) ,
148+ } ) ;
149+ fireEvent ( elm , new FakeMouseEvent ( 'mousedown' , { pageX : 10 , pageY : 1 } ) ) ;
104150 fireEvent ( elm , new FakeMouseEvent ( 'mousemove' , { pageX : 10 , pageY : 1 } ) ) ;
105- expect ( handleChange ) . toHaveReturnedWith ( { a : 1 , h : 0 , s : 0 , v : 68 } ) ;
151+ expect ( handleChange ) . toHaveBeenLastCalledWith ( { a : 0.99 , h : 0 , s : 0 , v : 68 } , expect . objectContaining ( { top : 0.01 } ) ) ;
152+ } ) ;
153+
154+ it ( 'Alpha direction = vertical maps top to 1 and bottom to 0' , async ( ) => {
155+ const handleChange = jest . fn ( ( newAlpha ) => newAlpha ) ;
156+ const { getByTitle } = render (
157+ < Alpha
158+ innerProps = { {
159+ title : 'vertical-alpha' ,
160+ } }
161+ direction = "vertical"
162+ onChange = { handleChange }
163+ hsva = { { h : 0 , s : 0 , v : 68 , a : 0.5 } }
164+ /> ,
165+ ) ;
166+
167+ const elm = getByTitle ( 'vertical-alpha' ) ;
168+ jest . spyOn ( elm , 'getBoundingClientRect' ) . mockReturnValue ( {
169+ x : 0 ,
170+ y : 0 ,
171+ top : 0 ,
172+ left : 0 ,
173+ bottom : 100 ,
174+ right : 20 ,
175+ width : 20 ,
176+ height : 100 ,
177+ toJSON : ( ) => ( { } ) ,
178+ } ) ;
179+
180+ fireEvent ( elm , new FakeMouseEvent ( 'mousedown' , { pageX : 10 , pageY : 0 } ) ) ;
181+ fireEvent ( elm , new FakeMouseEvent ( 'mousemove' , { pageX : 10 , pageY : 100 } ) ) ;
182+
183+ expect ( handleChange ) . toHaveBeenNthCalledWith ( 1 , { a : 1 , h : 0 , s : 0 , v : 68 } , expect . objectContaining ( { top : 0 } ) ) ;
184+ expect ( handleChange ) . toHaveBeenNthCalledWith ( 2 , { a : 0 , h : 0 , s : 0 , v : 68 } , expect . objectContaining ( { top : 1 } ) ) ;
185+ } ) ;
186+
187+ it ( 'Alpha reverse maps horizontal left to 1 and right to 0' , async ( ) => {
188+ const handleChange = jest . fn ( ( newAlpha ) => newAlpha ) ;
189+ const { getByTitle } = render (
190+ < Alpha
191+ innerProps = { {
192+ title : 'reverse-horizontal-alpha' ,
193+ } }
194+ reverse
195+ onChange = { handleChange }
196+ hsva = { { h : 0 , s : 0 , v : 68 , a : 0.5 } }
197+ /> ,
198+ ) ;
199+
200+ const elm = getByTitle ( 'reverse-horizontal-alpha' ) ;
201+ jest . spyOn ( elm , 'getBoundingClientRect' ) . mockReturnValue ( {
202+ x : 0 ,
203+ y : 0 ,
204+ top : 0 ,
205+ left : 0 ,
206+ bottom : 20 ,
207+ right : 100 ,
208+ width : 100 ,
209+ height : 20 ,
210+ toJSON : ( ) => ( { } ) ,
211+ } ) ;
212+
213+ fireEvent ( elm , new FakeMouseEvent ( 'mousedown' , { pageX : 0 , pageY : 10 } ) ) ;
214+ fireEvent ( elm , new FakeMouseEvent ( 'mousemove' , { pageX : 100 , pageY : 10 } ) ) ;
215+
216+ expect ( handleChange ) . toHaveBeenNthCalledWith ( 1 , { a : 1 , h : 0 , s : 0 , v : 68 } , expect . objectContaining ( { left : 0 } ) ) ;
217+ expect ( handleChange ) . toHaveBeenNthCalledWith ( 2 , { a : 0 , h : 0 , s : 0 , v : 68 } , expect . objectContaining ( { left : 1 } ) ) ;
218+ } ) ;
219+
220+ it ( 'Alpha reverse maps vertical top to 0 and bottom to 1' , async ( ) => {
221+ const handleChange = jest . fn ( ( newAlpha ) => newAlpha ) ;
222+ const { getByTitle } = render (
223+ < Alpha
224+ innerProps = { {
225+ title : 'reverse-vertical-alpha' ,
226+ } }
227+ direction = "vertical"
228+ reverse
229+ onChange = { handleChange }
230+ hsva = { { h : 0 , s : 0 , v : 68 , a : 0.5 } }
231+ /> ,
232+ ) ;
233+
234+ const elm = getByTitle ( 'reverse-vertical-alpha' ) ;
235+ jest . spyOn ( elm , 'getBoundingClientRect' ) . mockReturnValue ( {
236+ x : 0 ,
237+ y : 0 ,
238+ top : 0 ,
239+ left : 0 ,
240+ bottom : 100 ,
241+ right : 20 ,
242+ width : 20 ,
243+ height : 100 ,
244+ toJSON : ( ) => ( { } ) ,
245+ } ) ;
246+
247+ fireEvent ( elm , new FakeMouseEvent ( 'mousedown' , { pageX : 10 , pageY : 0 } ) ) ;
248+ fireEvent ( elm , new FakeMouseEvent ( 'mousemove' , { pageX : 10 , pageY : 100 } ) ) ;
249+
250+ expect ( handleChange ) . toHaveBeenNthCalledWith ( 1 , { a : 0 , h : 0 , s : 0 , v : 68 } , expect . objectContaining ( { top : 0 } ) ) ;
251+ expect ( handleChange ) . toHaveBeenNthCalledWith ( 2 , { a : 1 , h : 0 , s : 0 , v : 68 } , expect . objectContaining ( { top : 1 } ) ) ;
106252} ) ;
0 commit comments