@@ -5,6 +5,7 @@ import React, { useState } from 'react';
55import TestRenderer from 'react-test-renderer' ;
66import { screen , render , fireEvent } from '@testing-library/react' ;
77import '@testing-library/jest-dom' ;
8+ import { hexToHsva , hsvaToHex } from '@uiw/color-convert' ;
89import {
910 getWheelDimensions ,
1011 getHandleRange ,
@@ -28,20 +29,25 @@ class FakeMouseEvent extends MouseEvent {
2829
2930it ( 'Wheel mousedown onChange' , async ( ) => {
3031 const handleChange = jest . fn ( ( color ) => color . hex ) ;
32+ const color = '#ff7300' ;
33+ const hsva = hexToHsva ( color ) ;
3134 const {
3235 container : { firstChild } ,
33- } = render ( < Wheel color = "#ff7300" onChange = { handleChange } /> ) ;
36+ } = render ( < Wheel color = { color } onChange = { handleChange } /> ) ;
3437 fireEvent ( firstChild ! , new FakeMouseEvent ( 'mousedown' , { pageX : 20 , pageY : 1 } ) ) ;
35- expect ( handleChange ) . toHaveReturnedWith ( '#ffd900' ) ;
38+ const result = getWheelValueFromInput ( { width : 200 , direction : 'anticlockwise' , angle : 180 } , 20 , 1 ) ;
39+ expect ( handleChange ) . toHaveReturnedWith ( hsvaToHex ( { ...hsva , ...result } ) ) ;
3640} ) ;
3741
3842it ( 'Wheel color=hsvaColor ' , async ( ) => {
3943 const handleChange = jest . fn ( ( color ) => color . hex ) ;
44+ const hsva = { h : 209 , s : 36 , v : 90 , a : 1 } ;
4045 const {
4146 container : { firstChild } ,
42- } = render ( < Wheel color = { { h : 209 , s : 36 , v : 90 , a : 1 } } onChange = { handleChange } /> ) ;
47+ } = render ( < Wheel color = { hsva } onChange = { handleChange } /> ) ;
4348 fireEvent ( firstChild ! , new FakeMouseEvent ( 'mousedown' , { pageX : 20 , pageY : 1 } ) ) ;
44- expect ( handleChange ) . toHaveReturnedWith ( '#e6c300' ) ;
49+ const result = getWheelValueFromInput ( { width : 200 , direction : 'anticlockwise' , angle : 180 } , 20 , 1 ) ;
50+ expect ( handleChange ) . toHaveReturnedWith ( hsvaToHex ( { ...hsva , ...result } ) ) ;
4551} ) ;
4652
4753it ( 'Wheel mousedown onChange 2' , async ( ) => {
@@ -62,6 +68,39 @@ it('Wheel direction="clockwise" onChange', async () => {
6268 expect ( handleChange ) . toHaveReturnedWith ( '#000000' ) ;
6369} ) ;
6470
71+ it ( 'Wheel direction and angle should affect pointer position' , ( ) => {
72+ const hsv = { h : 60 , s : 100 , v : 100 , a : 1 } ;
73+ const {
74+ container : { firstChild } ,
75+ container,
76+ } = render ( < Wheel color = { hsv } direction = "clockwise" angle = { 30 } /> ) ;
77+ expect ( firstChild ) . toBeTruthy ( ) ;
78+ const pointer = container . querySelector ( '.w-color-wheel-pointer' ) as HTMLDivElement ;
79+ const { x, y } = getWheelHandlePosition ( { width : 200 , direction : 'clockwise' , angle : 30 } , hsv ) ;
80+ expect ( pointer . style . transform ) . toContain ( `translate(${ x } px, ${ y } px)` ) ;
81+ } ) ;
82+
83+ it ( 'Wheel direction and angle should affect onChange hue' , async ( ) => {
84+ const handleChange = jest . fn ( ) ;
85+ const wheelProps = { width : 200 , direction : 'clockwise' as const , angle : 30 } ;
86+ const {
87+ container : { firstChild } ,
88+ } = render ( < Wheel color = { { h : 200 , s : 20 , v : 100 , a : 1 } } { ...wheelProps } onChange = { handleChange } /> ) ;
89+ fireEvent ( firstChild ! , new FakeMouseEvent ( 'mousedown' , { pageX : 20 , pageY : 1 } ) ) ;
90+ const expected = getWheelValueFromInput ( wheelProps , 20 , 1 ) . h ;
91+ expect ( handleChange ) . toHaveBeenCalled ( ) ;
92+ expect ( handleChange . mock . calls [ 0 ] [ 0 ] . hsva . h ) . toEqual ( expected ) ;
93+ } ) ;
94+
95+ it ( 'Wheel background should rotate with angle in the same coordinate system' , ( ) => {
96+ const {
97+ container : { firstChild } ,
98+ } = render ( < Wheel color = { { h : 0 , s : 100 , v : 100 , a : 1 } } angle = { 30 } /> ) ;
99+ expect ( firstChild ) . toBeTruthy ( ) ;
100+ const background = ( firstChild as HTMLDivElement ) . children [ 1 ] as HTMLDivElement ;
101+ expect ( background . style . transform ) . toBe ( 'rotateZ(60deg)' ) ;
102+ } ) ;
103+
65104it ( 'getWheelDimensions' , ( ) => {
66105 expect ( getWheelDimensions ( { width : 200 } ) ) . toEqual ( {
67106 cx : 100 ,
0 commit comments