11// @vitest -environment happy-dom
22
33import React , { act } from "react" ;
4- import { createRoot } from "react-dom/client" ;
5- import { afterEach , describe , expect , it , vi } from "vitest" ;
4+ import { createRoot , type Root } from "react-dom/client" ;
5+ import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
6+ import { __resetDesignInputThrottle } from "../../utils/designInputTracking" ;
67import { ColorField } from "./propertyPanelColor" ;
78
9+ const trackStudioEvent = vi . hoisted ( ( ) => vi . fn ( ) ) ;
10+
11+ vi . mock ( "../../utils/studioTelemetry" , ( ) => ( {
12+ trackStudioEvent : ( ...args : unknown [ ] ) => trackStudioEvent ( ...args ) ,
13+ } ) ) ;
14+
815( globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT : boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
916
17+ const roots : Root [ ] = [ ] ;
18+
19+ beforeEach ( ( ) => {
20+ trackStudioEvent . mockReset ( ) ;
21+ __resetDesignInputThrottle ( ) ;
22+ } ) ;
23+
1024afterEach ( ( ) => {
25+ for ( const root of roots ) act ( ( ) => root . unmount ( ) ) ;
26+ roots . length = 0 ;
1127 document . body . innerHTML = "" ;
1228} ) ;
1329
14- function renderColorField ( onCommit : ( value : string ) => void ) : void {
30+ function renderColorField ( {
31+ value = "#333333" ,
32+ onPreview,
33+ onCommit = vi . fn ( ) ,
34+ } : {
35+ value ?: string ;
36+ onPreview ?: ( value : string ) => void ;
37+ onCommit ?: ( value : string ) => void ;
38+ } = { } ) : HTMLElement {
1539 const host = document . createElement ( "div" ) ;
1640 document . body . append ( host ) ;
1741 const root = createRoot ( host ) ;
42+ roots . push ( root ) ;
1843 act ( ( ) => {
19- root . render ( < ColorField flat label = "Color" value = "rgb(255, 176, 32)" onCommit = { onCommit } /> ) ;
44+ root . render (
45+ < ColorField flat label = "Color" value = { value } onPreview = { onPreview } onCommit = { onCommit } /> ,
46+ ) ;
2047 } ) ;
48+ return host ;
49+ }
50+
51+ function changeInput ( input : HTMLInputElement , value : string ) : void {
52+ const setter = Object . getOwnPropertyDescriptor ( HTMLInputElement . prototype , "value" ) ?. set ;
53+ if ( ! setter ) throw new Error ( "expected native input value setter" ) ;
54+ setter . call ( input , value ) ;
55+ input . dispatchEvent ( new Event ( "input" , { bubbles : true } ) ) ;
56+ }
57+
58+ function openHexInput ( host : HTMLElement ) : HTMLInputElement {
59+ const trigger = host . querySelector < HTMLButtonElement > ( '[data-flat-color-trigger="true"]' ) ;
60+ if ( ! trigger ) throw new Error ( "Color trigger was not rendered" ) ;
61+ act ( ( ) => trigger . click ( ) ) ;
62+ const input = document . querySelector < HTMLInputElement > ( 'input[spellcheck="false"]' ) ;
63+ if ( ! input ) throw new Error ( "Hex input was not rendered" ) ;
64+ return input ;
65+ }
66+
67+ function clickOutside ( ) : void {
68+ document . body . dispatchEvent ( new PointerEvent ( "pointerdown" , { bubbles : true } ) ) ;
2169}
2270
2371describe ( "ColorField flat trigger" , ( ) => {
2472 it ( "renders label and value inline with a small swatch, no boxed border" , ( ) => {
25- const host = document . createElement ( "div" ) ;
26- document . body . append ( host ) ;
27- const root = createRoot ( host ) ;
28- act ( ( ) => {
29- root . render ( < ColorField flat label = "Color" value = "rgb(255, 176, 32)" onCommit = { vi . fn ( ) } /> ) ;
30- } ) ;
73+ const host = renderColorField ( { value : "rgb(255, 176, 32)" } ) ;
3174 const trigger = host . querySelector < HTMLButtonElement > ( '[data-flat-color-trigger="true"]' ) ;
3275 expect ( trigger ) . not . toBeNull ( ) ;
3376 expect ( trigger ?. className ) . not . toContain ( "border-neutral-800" ) ;
3477 expect ( host . textContent ) . toContain ( "Color" ) ;
35- act ( ( ) => root . unmount ( ) ) ;
3678 } ) ;
3779
3880 it ( "persists one keyboard slider gesture on keyup" , ( ) => {
3981 const onCommit = vi . fn ( ) ;
40- renderColorField ( onCommit ) ;
82+ renderColorField ( { value : "rgb(255, 176, 32)" , onCommit } ) ;
4183 const trigger = document . querySelector < HTMLButtonElement > ( '[data-flat-color-trigger="true"]' ) ;
4284 if ( ! trigger ) throw new Error ( "Color trigger was not rendered" ) ;
4385 act ( ( ) => {
@@ -57,3 +99,107 @@ describe("ColorField flat trigger", () => {
5799 expect ( onCommit ) . toHaveBeenCalledOnce ( ) ;
58100 } ) ;
59101} ) ;
102+
103+ describe ( "ColorField hex editing" , ( ) => {
104+ it ( "allows #333333 to be backspaced to #3 without snapping" , ( ) => {
105+ const input = openHexInput ( renderColorField ( ) ) ;
106+
107+ for ( const value of [ "#33333" , "#3333" , "#333" , "#33" , "#3" ] ) {
108+ act ( ( ) => changeInput ( input , value ) ) ;
109+ expect ( input . value ) . toBe ( value ) ;
110+ }
111+ } ) ;
112+
113+ it ( "does not silently change #22CC66 to #2222CC while backspacing" , ( ) => {
114+ const input = openHexInput ( renderColorField ( { value : "#22CC66" } ) ) ;
115+
116+ for ( const value of [ "#22CC6" , "#22CC" , "#22C" ] ) {
117+ act ( ( ) => changeInput ( input , value ) ) ;
118+ expect ( input . value ) . toBe ( value ) ;
119+ expect ( input . value ) . not . toBe ( "#2222CC" ) ;
120+ }
121+ } ) ;
122+
123+ it ( "commits a full replacement after selecting the existing value" , ( ) => {
124+ const onCommit = vi . fn ( ) ;
125+ const input = openHexInput ( renderColorField ( { onCommit } ) ) ;
126+ input . focus ( ) ;
127+ input . select ( ) ;
128+
129+ act ( ( ) => changeInput ( input , "#12AB34" ) ) ;
130+ act ( ( ) => input . blur ( ) ) ;
131+
132+ expect ( onCommit ) . toHaveBeenCalledOnce ( ) ;
133+ expect ( onCommit ) . toHaveBeenCalledWith ( "rgb(18, 171, 52)" ) ;
134+ } ) ;
135+
136+ it ( "commits a complete pending hex on outside-click" , ( ) => {
137+ const onCommit = vi . fn ( ) ;
138+ const input = openHexInput ( renderColorField ( { onCommit } ) ) ;
139+
140+ act ( ( ) => changeInput ( input , "#12AB34" ) ) ;
141+ act ( clickOutside ) ;
142+
143+ expect ( onCommit ) . toHaveBeenCalledOnce ( ) ;
144+ expect ( onCommit ) . toHaveBeenCalledWith ( "rgb(18, 171, 52)" ) ;
145+ } ) ;
146+
147+ it ( "does not commit an incomplete pending hex on outside-click" , ( ) => {
148+ const onCommit = vi . fn ( ) ;
149+ const input = openHexInput ( renderColorField ( { onCommit } ) ) ;
150+
151+ act ( ( ) => changeInput ( input , "#12AB3" ) ) ;
152+ act ( clickOutside ) ;
153+
154+ expect ( onCommit ) . not . toHaveBeenCalled ( ) ;
155+ } ) ;
156+
157+ it ( "cancels a pending hex edit on Escape and restores the previous value" , ( ) => {
158+ const onPreview = vi . fn ( ) ;
159+ const onCommit = vi . fn ( ) ;
160+ const host = renderColorField ( { value : "#224466" , onPreview, onCommit } ) ;
161+ const input = openHexInput ( host ) ;
162+
163+ act ( ( ) => changeInput ( input , "#12AB34" ) ) ;
164+ act ( ( ) => document . dispatchEvent ( new KeyboardEvent ( "keydown" , { key : "Escape" } ) ) ) ;
165+
166+ expect ( onCommit ) . not . toHaveBeenCalled ( ) ;
167+ expect ( onPreview ) . toHaveBeenLastCalledWith ( "rgb(34, 68, 102)" ) ;
168+ expect ( openHexInput ( host ) . value ) . toBe ( "#224466" ) ;
169+ } ) ;
170+
171+ it ( "still commits a complete hex on Tab-blur" , ( ) => {
172+ const onCommit = vi . fn ( ) ;
173+ const input = openHexInput ( renderColorField ( { onCommit } ) ) ;
174+ input . focus ( ) ;
175+
176+ act ( ( ) => changeInput ( input , "#12AB34" ) ) ;
177+ act ( ( ) => input . blur ( ) ) ;
178+
179+ expect ( onCommit ) . toHaveBeenCalledOnce ( ) ;
180+ expect ( onCommit ) . toHaveBeenCalledWith ( "rgb(18, 171, 52)" ) ;
181+ } ) ;
182+
183+ it ( "live-previews only a complete six-digit hex" , ( ) => {
184+ const onPreview = vi . fn ( ) ;
185+ const input = openHexInput ( renderColorField ( { value : "#112233" , onPreview } ) ) ;
186+
187+ act ( ( ) => changeInput ( input , "#333" ) ) ;
188+ expect ( onPreview ) . not . toHaveBeenCalled ( ) ;
189+
190+ act ( ( ) => changeInput ( input , "#333333" ) ) ;
191+ expect ( onPreview ) . toHaveBeenCalledOnce ( ) ;
192+ expect ( onPreview ) . toHaveBeenCalledWith ( "rgb(51, 51, 51)" ) ;
193+ } ) ;
194+
195+ it ( "tracks exactly once per completed edit, not once per keystroke" , ( ) => {
196+ const input = openHexInput ( renderColorField ( ) ) ;
197+
198+ for ( const value of [ "#" , "#1" , "#12" , "#12A" , "#12AB" , "#12AB3" , "#12AB34" ] ) {
199+ act ( ( ) => changeInput ( input , value ) ) ;
200+ }
201+ act ( clickOutside ) ;
202+
203+ expect ( trackStudioEvent ) . toHaveBeenCalledOnce ( ) ;
204+ } ) ;
205+ } ) ;
0 commit comments