@wordpress/media-editor 0.7.1-next.v.202604201441.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (300) hide show
  1. package/build/components/media-editor-canvas/index.cjs +54 -0
  2. package/build/components/media-editor-canvas/index.cjs.map +7 -0
  3. package/build/components/media-editor-crop-panel/index.cjs +116 -0
  4. package/build/components/media-editor-crop-panel/index.cjs.map +7 -0
  5. package/build/components/media-editor-modal/build-modifiers.cjs +91 -0
  6. package/build/components/media-editor-modal/build-modifiers.cjs.map +7 -0
  7. package/build/components/media-editor-modal/index.cjs +497 -0
  8. package/build/components/media-editor-modal/index.cjs.map +7 -0
  9. package/build/components/media-editor-toolbar/index.cjs +146 -0
  10. package/build/components/media-editor-toolbar/index.cjs.map +7 -0
  11. package/build/image-editor/core/camera.cjs +194 -0
  12. package/build/image-editor/core/camera.cjs.map +7 -0
  13. package/build/image-editor/core/constants.cjs +81 -0
  14. package/build/image-editor/core/constants.cjs.map +7 -0
  15. package/build/image-editor/core/containment.cjs +245 -0
  16. package/build/image-editor/core/containment.cjs.map +7 -0
  17. package/build/image-editor/core/export/canvas-renderer.cjs +151 -0
  18. package/build/image-editor/core/export/canvas-renderer.cjs.map +7 -0
  19. package/build/image-editor/core/index.cjs +50 -0
  20. package/build/image-editor/core/index.cjs.map +7 -0
  21. package/build/image-editor/core/interaction-controller.cjs +608 -0
  22. package/build/image-editor/core/interaction-controller.cjs.map +7 -0
  23. package/build/image-editor/core/math/rotation.cjs +41 -0
  24. package/build/image-editor/core/math/rotation.cjs.map +7 -0
  25. package/build/image-editor/core/source-region.cjs +95 -0
  26. package/build/image-editor/core/source-region.cjs.map +7 -0
  27. package/build/image-editor/core/state.cjs +269 -0
  28. package/build/image-editor/core/state.cjs.map +7 -0
  29. package/build/image-editor/core/stencil-math.cjs +197 -0
  30. package/build/image-editor/core/stencil-math.cjs.map +7 -0
  31. package/build/image-editor/core/transform-style.cjs +46 -0
  32. package/build/image-editor/core/transform-style.cjs.map +7 -0
  33. package/build/image-editor/core/transforms/pipeline.cjs +43 -0
  34. package/build/image-editor/core/transforms/pipeline.cjs.map +7 -0
  35. package/build/image-editor/core/types.cjs +19 -0
  36. package/build/image-editor/core/types.cjs.map +7 -0
  37. package/build/image-editor/index.cjs +57 -0
  38. package/build/image-editor/index.cjs.map +7 -0
  39. package/build/image-editor/react/components/cropper-provider.cjs +50 -0
  40. package/build/image-editor/react/components/cropper-provider.cjs.map +7 -0
  41. package/build/image-editor/react/components/cropper.cjs +365 -0
  42. package/build/image-editor/react/components/cropper.cjs.map +7 -0
  43. package/build/image-editor/react/components/index.cjs +36 -0
  44. package/build/image-editor/react/components/index.cjs.map +7 -0
  45. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +58 -0
  46. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +7 -0
  47. package/build/image-editor/react/components/overlays/grid-overlay.cjs +91 -0
  48. package/build/image-editor/react/components/overlays/grid-overlay.cjs.map +7 -0
  49. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +351 -0
  50. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +7 -0
  51. package/build/image-editor/react/hooks/index.cjs +31 -0
  52. package/build/image-editor/react/hooks/index.cjs.map +7 -0
  53. package/build/image-editor/react/hooks/use-aria-announcer.cjs +68 -0
  54. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +7 -0
  55. package/build/image-editor/react/hooks/use-cropper-state.cjs +158 -0
  56. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +7 -0
  57. package/build/image-editor/react/hooks/use-interaction.cjs +161 -0
  58. package/build/image-editor/react/hooks/use-interaction.cjs.map +7 -0
  59. package/build/image-editor/react/hooks/use-transform-style.cjs +38 -0
  60. package/build/image-editor/react/hooks/use-transform-style.cjs.map +7 -0
  61. package/build/index.cjs +5 -2
  62. package/build/index.cjs.map +2 -2
  63. package/build/lock-unlock.cjs +37 -0
  64. package/build/lock-unlock.cjs.map +7 -0
  65. package/build/private-apis.cjs +38 -0
  66. package/build/private-apis.cjs.map +7 -0
  67. package/build/store/actions.cjs +45 -0
  68. package/build/store/actions.cjs.map +7 -0
  69. package/build/store/constants.cjs +31 -0
  70. package/build/store/constants.cjs.map +7 -0
  71. package/build/store/index.cjs +51 -0
  72. package/build/store/index.cjs.map +7 -0
  73. package/build/store/reducer.cjs +51 -0
  74. package/build/store/reducer.cjs.map +7 -0
  75. package/build/store/selectors.cjs +43 -0
  76. package/build/store/selectors.cjs.map +7 -0
  77. package/build-module/components/media-editor-canvas/index.mjs +33 -0
  78. package/build-module/components/media-editor-canvas/index.mjs.map +7 -0
  79. package/build-module/components/media-editor-crop-panel/index.mjs +100 -0
  80. package/build-module/components/media-editor-crop-panel/index.mjs.map +7 -0
  81. package/build-module/components/media-editor-modal/build-modifiers.mjs +66 -0
  82. package/build-module/components/media-editor-modal/build-modifiers.mjs.map +7 -0
  83. package/build-module/components/media-editor-modal/index.mjs +483 -0
  84. package/build-module/components/media-editor-modal/index.mjs.map +7 -0
  85. package/build-module/components/media-editor-toolbar/index.mjs +130 -0
  86. package/build-module/components/media-editor-toolbar/index.mjs.map +7 -0
  87. package/build-module/image-editor/core/camera.mjs +163 -0
  88. package/build-module/image-editor/core/camera.mjs.map +7 -0
  89. package/build-module/image-editor/core/constants.mjs +51 -0
  90. package/build-module/image-editor/core/constants.mjs.map +7 -0
  91. package/build-module/image-editor/core/containment.mjs +218 -0
  92. package/build-module/image-editor/core/containment.mjs.map +7 -0
  93. package/build-module/image-editor/core/export/canvas-renderer.mjs +120 -0
  94. package/build-module/image-editor/core/export/canvas-renderer.mjs.map +7 -0
  95. package/build-module/image-editor/core/index.mjs +24 -0
  96. package/build-module/image-editor/core/index.mjs.map +7 -0
  97. package/build-module/image-editor/core/interaction-controller.mjs +583 -0
  98. package/build-module/image-editor/core/interaction-controller.mjs.map +7 -0
  99. package/build-module/image-editor/core/math/rotation.mjs +15 -0
  100. package/build-module/image-editor/core/math/rotation.mjs.map +7 -0
  101. package/build-module/image-editor/core/source-region.mjs +69 -0
  102. package/build-module/image-editor/core/source-region.mjs.map +7 -0
  103. package/build-module/image-editor/core/state.mjs +242 -0
  104. package/build-module/image-editor/core/state.mjs.map +7 -0
  105. package/build-module/image-editor/core/stencil-math.mjs +169 -0
  106. package/build-module/image-editor/core/stencil-math.mjs.map +7 -0
  107. package/build-module/image-editor/core/transform-style.mjs +21 -0
  108. package/build-module/image-editor/core/transform-style.mjs.map +7 -0
  109. package/build-module/image-editor/core/transforms/pipeline.mjs +17 -0
  110. package/build-module/image-editor/core/transforms/pipeline.mjs.map +7 -0
  111. package/build-module/image-editor/core/types.mjs +1 -0
  112. package/build-module/image-editor/core/types.mjs.map +7 -0
  113. package/build-module/image-editor/index.mjs +30 -0
  114. package/build-module/image-editor/index.mjs.map +7 -0
  115. package/build-module/image-editor/react/components/cropper-provider.mjs +26 -0
  116. package/build-module/image-editor/react/components/cropper-provider.mjs.map +7 -0
  117. package/build-module/image-editor/react/components/cropper.mjs +337 -0
  118. package/build-module/image-editor/react/components/cropper.mjs.map +7 -0
  119. package/build-module/image-editor/react/components/index.mjs +9 -0
  120. package/build-module/image-editor/react/components/index.mjs.map +7 -0
  121. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +33 -0
  122. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +7 -0
  123. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs +66 -0
  124. package/build-module/image-editor/react/components/overlays/grid-overlay.mjs.map +7 -0
  125. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +330 -0
  126. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +7 -0
  127. package/build-module/image-editor/react/hooks/index.mjs +6 -0
  128. package/build-module/image-editor/react/hooks/index.mjs.map +7 -0
  129. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +43 -0
  130. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +7 -0
  131. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +137 -0
  132. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +7 -0
  133. package/build-module/image-editor/react/hooks/use-interaction.mjs +138 -0
  134. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +7 -0
  135. package/build-module/image-editor/react/hooks/use-transform-style.mjs +13 -0
  136. package/build-module/image-editor/react/hooks/use-transform-style.mjs.map +7 -0
  137. package/build-module/index.mjs +3 -1
  138. package/build-module/index.mjs.map +2 -2
  139. package/build-module/lock-unlock.mjs +11 -0
  140. package/build-module/lock-unlock.mjs.map +7 -0
  141. package/build-module/private-apis.mjs +13 -0
  142. package/build-module/private-apis.mjs.map +7 -0
  143. package/build-module/store/actions.mjs +19 -0
  144. package/build-module/store/actions.mjs.map +7 -0
  145. package/build-module/store/constants.mjs +6 -0
  146. package/build-module/store/constants.mjs.map +7 -0
  147. package/build-module/store/index.mjs +16 -0
  148. package/build-module/store/index.mjs.map +7 -0
  149. package/build-module/store/reducer.mjs +26 -0
  150. package/build-module/store/reducer.mjs.map +7 -0
  151. package/build-module/store/selectors.mjs +16 -0
  152. package/build-module/store/selectors.mjs.map +7 -0
  153. package/build-style/style-rtl.css +315 -0
  154. package/build-style/style.css +315 -0
  155. package/build-types/components/media-editor-canvas/index.d.ts +22 -0
  156. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -0
  157. package/build-types/components/media-editor-crop-panel/index.d.ts +45 -0
  158. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -0
  159. package/build-types/components/media-editor-modal/build-modifiers.d.ts +83 -0
  160. package/build-types/components/media-editor-modal/build-modifiers.d.ts.map +1 -0
  161. package/build-types/components/media-editor-modal/index.d.ts +20 -0
  162. package/build-types/components/media-editor-modal/index.d.ts.map +1 -0
  163. package/build-types/components/media-editor-toolbar/index.d.ts +21 -0
  164. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
  165. package/build-types/image-editor/core/camera.d.ts +107 -0
  166. package/build-types/image-editor/core/camera.d.ts.map +1 -0
  167. package/build-types/image-editor/core/constants.d.ts +46 -0
  168. package/build-types/image-editor/core/constants.d.ts.map +1 -0
  169. package/build-types/image-editor/core/containment.d.ts +61 -0
  170. package/build-types/image-editor/core/containment.d.ts.map +1 -0
  171. package/build-types/image-editor/core/export/canvas-renderer.d.ts +106 -0
  172. package/build-types/image-editor/core/export/canvas-renderer.d.ts.map +1 -0
  173. package/build-types/image-editor/core/index.d.ts +8 -0
  174. package/build-types/image-editor/core/index.d.ts.map +1 -0
  175. package/build-types/image-editor/core/interaction-controller.d.ts +178 -0
  176. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -0
  177. package/build-types/image-editor/core/math/rotation.d.ts +18 -0
  178. package/build-types/image-editor/core/math/rotation.d.ts.map +1 -0
  179. package/build-types/image-editor/core/source-region.d.ts +75 -0
  180. package/build-types/image-editor/core/source-region.d.ts.map +1 -0
  181. package/build-types/image-editor/core/state.d.ts +42 -0
  182. package/build-types/image-editor/core/state.d.ts.map +1 -0
  183. package/build-types/image-editor/core/stencil-math.d.ts +79 -0
  184. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -0
  185. package/build-types/image-editor/core/transform-style.d.ts +23 -0
  186. package/build-types/image-editor/core/transform-style.d.ts.map +1 -0
  187. package/build-types/image-editor/core/transforms/pipeline.d.ts +27 -0
  188. package/build-types/image-editor/core/transforms/pipeline.d.ts.map +1 -0
  189. package/build-types/image-editor/core/types.d.ts +211 -0
  190. package/build-types/image-editor/core/types.d.ts.map +1 -0
  191. package/build-types/image-editor/index.d.ts +16 -0
  192. package/build-types/image-editor/index.d.ts.map +1 -0
  193. package/build-types/image-editor/react/components/cropper-provider.d.ts +37 -0
  194. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -0
  195. package/build-types/image-editor/react/components/cropper.d.ts +59 -0
  196. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -0
  197. package/build-types/image-editor/react/components/index.d.ts +4 -0
  198. package/build-types/image-editor/react/components/index.d.ts.map +1 -0
  199. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +30 -0
  200. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -0
  201. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +30 -0
  202. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -0
  203. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +32 -0
  204. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -0
  205. package/build-types/image-editor/react/hooks/index.d.ts +3 -0
  206. package/build-types/image-editor/react/hooks/index.d.ts.map +1 -0
  207. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts +16 -0
  208. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -0
  209. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +68 -0
  210. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
  211. package/build-types/image-editor/react/hooks/use-interaction.d.ts +60 -0
  212. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -0
  213. package/build-types/image-editor/react/hooks/use-transform-style.d.ts +16 -0
  214. package/build-types/image-editor/react/hooks/use-transform-style.d.ts.map +1 -0
  215. package/build-types/image-editor/stories/rectangle-crop.story.d.ts +16 -0
  216. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -0
  217. package/build-types/index.d.ts +1 -0
  218. package/build-types/index.d.ts.map +1 -1
  219. package/build-types/lock-unlock.d.ts +2 -0
  220. package/build-types/lock-unlock.d.ts.map +1 -0
  221. package/build-types/private-apis.d.ts +2 -0
  222. package/build-types/private-apis.d.ts.map +1 -0
  223. package/build-types/store/actions.d.ts +27 -0
  224. package/build-types/store/actions.d.ts.map +1 -0
  225. package/build-types/store/constants.d.ts +2 -0
  226. package/build-types/store/constants.d.ts.map +1 -0
  227. package/build-types/store/index.d.ts +4 -0
  228. package/build-types/store/index.d.ts.map +1 -0
  229. package/build-types/store/reducer.d.ts +23 -0
  230. package/build-types/store/reducer.d.ts.map +1 -0
  231. package/build-types/store/selectors.d.ts +8 -0
  232. package/build-types/store/selectors.d.ts.map +1 -0
  233. package/package.json +21 -7
  234. package/src/components/media-editor-canvas/index.tsx +56 -0
  235. package/src/components/media-editor-canvas/style.scss +9 -0
  236. package/src/components/media-editor-crop-panel/index.tsx +143 -0
  237. package/src/components/media-editor-modal/build-modifiers.ts +164 -0
  238. package/src/components/media-editor-modal/index.tsx +639 -0
  239. package/src/components/media-editor-modal/style.scss +139 -0
  240. package/src/components/media-editor-modal/test/build-modifiers.test.ts +343 -0
  241. package/src/components/media-editor-toolbar/index.tsx +150 -0
  242. package/src/components/media-editor-toolbar/style.scss +21 -0
  243. package/src/image-editor/README.md +162 -0
  244. package/src/image-editor/core/camera.ts +352 -0
  245. package/src/image-editor/core/constants.ts +93 -0
  246. package/src/image-editor/core/containment.ts +435 -0
  247. package/src/image-editor/core/export/canvas-renderer.ts +253 -0
  248. package/src/image-editor/core/export/test/canvas-renderer.ts +813 -0
  249. package/src/image-editor/core/index.ts +35 -0
  250. package/src/image-editor/core/interaction-controller.ts +889 -0
  251. package/src/image-editor/core/math/rotation.ts +26 -0
  252. package/src/image-editor/core/source-region.ts +166 -0
  253. package/src/image-editor/core/state.ts +428 -0
  254. package/src/image-editor/core/stencil-math.ts +330 -0
  255. package/src/image-editor/core/test/camera.ts +593 -0
  256. package/src/image-editor/core/test/interaction-controller.ts +996 -0
  257. package/src/image-editor/core/test/preview-export-parity.ts +242 -0
  258. package/src/image-editor/core/test/render-camera-bridge.ts +348 -0
  259. package/src/image-editor/core/test/state.ts +762 -0
  260. package/src/image-editor/core/test/stencil-math.ts +321 -0
  261. package/src/image-editor/core/transform-style.ts +44 -0
  262. package/src/image-editor/core/transforms/pipeline.ts +43 -0
  263. package/src/image-editor/core/transforms/test/pipeline.ts +269 -0
  264. package/src/image-editor/core/types.ts +177 -0
  265. package/src/image-editor/docs/architecture.md +96 -0
  266. package/src/image-editor/docs/recipes.md +720 -0
  267. package/src/image-editor/docs/roadmap.md +66 -0
  268. package/src/image-editor/index.ts +43 -0
  269. package/src/image-editor/react/components/cropper-provider.tsx +73 -0
  270. package/src/image-editor/react/components/cropper.scss +195 -0
  271. package/src/image-editor/react/components/cropper.tsx +534 -0
  272. package/src/image-editor/react/components/index.ts +3 -0
  273. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +57 -0
  274. package/src/image-editor/react/components/overlays/grid-overlay.tsx +80 -0
  275. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +513 -0
  276. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +265 -0
  277. package/src/image-editor/react/components/test/cropper.tsx +140 -0
  278. package/src/image-editor/react/hooks/index.ts +2 -0
  279. package/src/image-editor/react/hooks/test/use-cropper-state.ts +913 -0
  280. package/src/image-editor/react/hooks/test/use-interaction.ts +218 -0
  281. package/src/image-editor/react/hooks/test/use-transform-style.ts +146 -0
  282. package/src/image-editor/react/hooks/use-aria-announcer.ts +64 -0
  283. package/src/image-editor/react/hooks/use-cropper-state.ts +238 -0
  284. package/src/image-editor/react/hooks/use-interaction.ts +223 -0
  285. package/src/image-editor/react/hooks/use-transform-style.ts +30 -0
  286. package/src/image-editor/stories/rectangle-crop.story.tsx +1085 -0
  287. package/src/image-editor/stories/style.css +94 -0
  288. package/src/image-editor/style.scss +1 -0
  289. package/src/index.ts +3 -0
  290. package/src/lock-unlock.ts +10 -0
  291. package/src/private-apis.ts +12 -0
  292. package/src/store/actions.ts +33 -0
  293. package/src/store/constants.ts +1 -0
  294. package/src/store/index.ts +20 -0
  295. package/src/store/reducer.ts +48 -0
  296. package/src/store/selectors.ts +16 -0
  297. package/src/store/test/reducer.ts +57 -0
  298. package/src/style.scss +4 -0
  299. package/tsconfig.json +10 -2
  300. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,913 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { renderHook, act } from '@testing-library/react';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import { useCropperState } from '../use-cropper-state';
10
+ import { DEFAULT_STATE } from '../../../core/constants';
11
+
12
+ describe( 'useCropperState', () => {
13
+ it( 'should initialize with DEFAULT_STATE', () => {
14
+ const { result } = renderHook( () => useCropperState() );
15
+
16
+ expect( result.current.state ).toEqual( DEFAULT_STATE );
17
+ } );
18
+
19
+ it( 'should initialize with merged partial state', () => {
20
+ const { result } = renderHook( () =>
21
+ useCropperState( { zoom: 2, rotation: 45 } )
22
+ );
23
+
24
+ expect( result.current.state.zoom ).toBe( 2 );
25
+ expect( result.current.state.rotation ).toBe( 45 );
26
+ // Non-overridden fields should match defaults.
27
+ expect( result.current.state.pan ).toEqual( DEFAULT_STATE.pan );
28
+ } );
29
+
30
+ it( 'should dispatch SET_CROP via setCrop', () => {
31
+ const { result } = renderHook( () => useCropperState() );
32
+
33
+ // At zoom=1, crop is clamped to (0,0) — no panning possible.
34
+ // Zoom in first to allow panning.
35
+ act( () => {
36
+ result.current.setZoom( 3 );
37
+ } );
38
+
39
+ act( () => {
40
+ result.current.setPan( { x: 0.3, y: 0.2 } );
41
+ } );
42
+
43
+ expect( result.current.state.pan.x ).toBeCloseTo( 0.3 );
44
+ expect( result.current.state.pan.y ).toBeCloseTo( 0.2 );
45
+ } );
46
+
47
+ it( 'should dispatch SET_ZOOM via setZoom', () => {
48
+ const { result } = renderHook( () => useCropperState() );
49
+
50
+ act( () => {
51
+ result.current.setZoom( 3 );
52
+ } );
53
+
54
+ expect( result.current.state.zoom ).toBe( 3 );
55
+ } );
56
+
57
+ it( 'should dispatch SET_ZOOM_AT_POINT via setZoomAtPoint', () => {
58
+ const { result } = renderHook( () => useCropperState() );
59
+
60
+ act( () => {
61
+ result.current.setZoomAtPoint( 3, { x: 0.2, y: 0.1 } );
62
+ } );
63
+
64
+ expect( result.current.state.zoom ).toBe( 3 );
65
+ expect( result.current.state.pan ).toEqual( { x: 0.2, y: 0.1 } );
66
+ } );
67
+
68
+ it( 'should clamp zoom to valid range via SET_ZOOM', () => {
69
+ const { result } = renderHook( () => useCropperState() );
70
+
71
+ act( () => {
72
+ result.current.setZoom( 15 );
73
+ } );
74
+
75
+ expect( result.current.state.zoom ).toBe( 10 );
76
+
77
+ act( () => {
78
+ result.current.setZoom( 0.5 );
79
+ } );
80
+
81
+ expect( result.current.state.zoom ).toBe( 1 );
82
+ } );
83
+
84
+ it( 'should dispatch SET_ROTATION via setRotation', () => {
85
+ const { result } = renderHook( () => useCropperState() );
86
+
87
+ act( () => {
88
+ result.current.setRotation( 90 );
89
+ } );
90
+
91
+ expect( result.current.state.rotation ).toBe( 90 );
92
+ } );
93
+
94
+ it( 'should normalize rotation via SET_ROTATION', () => {
95
+ const { result } = renderHook( () => useCropperState() );
96
+
97
+ act( () => {
98
+ result.current.setRotation( -90 );
99
+ } );
100
+
101
+ expect( result.current.state.rotation ).toBe( 270 );
102
+
103
+ act( () => {
104
+ result.current.setRotation( 450 );
105
+ } );
106
+
107
+ expect( result.current.state.rotation ).toBe( 90 );
108
+ } );
109
+
110
+ it( 'should dispatch SET_FLIP via setFlip', () => {
111
+ const { result } = renderHook( () => useCropperState() );
112
+
113
+ act( () => {
114
+ result.current.setFlip( {
115
+ horizontal: true,
116
+ vertical: false,
117
+ } );
118
+ } );
119
+
120
+ expect( result.current.state.flip ).toEqual( {
121
+ horizontal: true,
122
+ vertical: false,
123
+ } );
124
+ } );
125
+
126
+ it( 'should dispatch SET_CROP_RECT via setCropRect', () => {
127
+ const { result } = renderHook( () => useCropperState() );
128
+
129
+ const rect = { x: 0.1, y: 0.2, width: 0.5, height: 0.6 };
130
+ act( () => {
131
+ result.current.setCropRect( rect );
132
+ } );
133
+
134
+ expect( result.current.state.cropRect ).toEqual( rect );
135
+ } );
136
+
137
+ it( 'should dispatch APPLY_OPERATION via applyOperation', () => {
138
+ const { result } = renderHook( () => useCropperState() );
139
+
140
+ act( () => {
141
+ result.current.applyOperation( {
142
+ type: 'rotate',
143
+ degrees: 90,
144
+ } );
145
+ } );
146
+
147
+ expect( result.current.state.rotation ).toBe( 90 );
148
+ } );
149
+
150
+ it( 'should handle APPLY_OPERATION for flip', () => {
151
+ const { result } = renderHook( () => useCropperState() );
152
+
153
+ act( () => {
154
+ result.current.applyOperation( {
155
+ type: 'flip',
156
+ direction: 'horizontal',
157
+ } );
158
+ } );
159
+
160
+ expect( result.current.state.flip.horizontal ).toBe( true );
161
+ expect( result.current.state.flip.vertical ).toBe( false );
162
+ } );
163
+
164
+ it( 'should accumulate rotation via APPLY_OPERATION', () => {
165
+ const { result } = renderHook( () => useCropperState() );
166
+
167
+ act( () => {
168
+ result.current.applyOperation( {
169
+ type: 'rotate',
170
+ degrees: 90,
171
+ } );
172
+ } );
173
+
174
+ act( () => {
175
+ result.current.applyOperation( {
176
+ type: 'rotate',
177
+ degrees: 45,
178
+ } );
179
+ } );
180
+
181
+ expect( result.current.state.rotation ).toBe( 135 );
182
+ } );
183
+
184
+ it( 'should handle APPLY_OPERATION for crop', () => {
185
+ const { result } = renderHook( () => useCropperState() );
186
+
187
+ const rect = { x: 0.1, y: 0.2, width: 0.5, height: 0.6 };
188
+ act( () => {
189
+ result.current.applyOperation( {
190
+ type: 'crop',
191
+ rect,
192
+ } );
193
+ } );
194
+
195
+ expect( result.current.state.cropRect ).toEqual( rect );
196
+ } );
197
+
198
+ it( 'should handle APPLY_OPERATION for zoom', () => {
199
+ const { result } = renderHook( () => useCropperState() );
200
+
201
+ act( () => {
202
+ result.current.applyOperation( {
203
+ type: 'zoom',
204
+ factor: 3,
205
+ } );
206
+ } );
207
+
208
+ expect( result.current.state.zoom ).toBe( 3 );
209
+ } );
210
+
211
+ it( 'should reset to DEFAULT_STATE', () => {
212
+ const { result } = renderHook( () => useCropperState() );
213
+
214
+ act( () => {
215
+ result.current.setZoom( 5 );
216
+ result.current.setRotation( 180 );
217
+ } );
218
+
219
+ expect( result.current.state.zoom ).toBe( 5 );
220
+
221
+ act( () => {
222
+ result.current.reset();
223
+ } );
224
+
225
+ expect( result.current.state ).toEqual( DEFAULT_STATE );
226
+ } );
227
+
228
+ it( 'should reset to a custom state', () => {
229
+ const { result } = renderHook( () => useCropperState() );
230
+
231
+ act( () => {
232
+ result.current.setZoom( 5 );
233
+ } );
234
+
235
+ act( () => {
236
+ result.current.reset( { zoom: 2 } );
237
+ } );
238
+
239
+ expect( result.current.state.zoom ).toBe( 2 );
240
+ expect( result.current.state.pan ).toEqual( DEFAULT_STATE.pan );
241
+ } );
242
+
243
+ describe( 'isDirty', () => {
244
+ it( 'should be false initially', () => {
245
+ const { result } = renderHook( () => useCropperState() );
246
+
247
+ expect( result.current.isDirty ).toBe( false );
248
+ } );
249
+
250
+ it( 'should be true after changing crop', () => {
251
+ const { result } = renderHook( () => useCropperState() );
252
+
253
+ // Zoom in first so crop change isn't clamped to (0,0).
254
+ act( () => {
255
+ result.current.setZoom( 3 );
256
+ } );
257
+
258
+ act( () => {
259
+ result.current.setPan( { x: 0.1, y: 0.2 } );
260
+ } );
261
+
262
+ expect( result.current.isDirty ).toBe( true );
263
+ } );
264
+
265
+ it( 'should be true after changing zoom', () => {
266
+ const { result } = renderHook( () => useCropperState() );
267
+
268
+ act( () => {
269
+ result.current.setZoom( 2 );
270
+ } );
271
+
272
+ expect( result.current.isDirty ).toBe( true );
273
+ } );
274
+
275
+ it( 'should be true after changing rotation', () => {
276
+ const { result } = renderHook( () => useCropperState() );
277
+
278
+ act( () => {
279
+ result.current.setRotation( 45 );
280
+ } );
281
+
282
+ expect( result.current.isDirty ).toBe( true );
283
+ } );
284
+
285
+ it( 'should be true after changing flip', () => {
286
+ const { result } = renderHook( () => useCropperState() );
287
+
288
+ act( () => {
289
+ result.current.setFlip( {
290
+ horizontal: true,
291
+ vertical: false,
292
+ } );
293
+ } );
294
+
295
+ expect( result.current.isDirty ).toBe( true );
296
+ } );
297
+
298
+ it( 'should be false after reset', () => {
299
+ const { result } = renderHook( () => useCropperState() );
300
+
301
+ act( () => {
302
+ result.current.setZoom( 5 );
303
+ } );
304
+
305
+ expect( result.current.isDirty ).toBe( true );
306
+
307
+ act( () => {
308
+ result.current.reset();
309
+ } );
310
+
311
+ expect( result.current.isDirty ).toBe( false );
312
+ } );
313
+
314
+ it( 'should be false after manually resetting fields to initial values', () => {
315
+ const { result } = renderHook( () => useCropperState() );
316
+
317
+ // Zoom in and pan.
318
+ act( () => {
319
+ result.current.setZoom( 3 );
320
+ } );
321
+
322
+ act( () => {
323
+ result.current.setPan( { x: 0.2, y: 0.2 } );
324
+ } );
325
+
326
+ expect( result.current.isDirty ).toBe( true );
327
+
328
+ // Reset each field back to its initial value manually.
329
+ act( () => {
330
+ result.current.setZoom( 1 );
331
+ } );
332
+
333
+ act( () => {
334
+ result.current.setPan( { x: 0, y: 0 } );
335
+ } );
336
+
337
+ expect( result.current.isDirty ).toBe( false );
338
+ } );
339
+
340
+ // Regression: loading an image runs the new state through
341
+ // enforceContainment, which can nudge pan/zoom by float ulp on
342
+ // non-square images. The initial snapshot must be refreshed so
343
+ // isDirty still reports false at a "clean" post-load state.
344
+ it( 'should be false after setImage on a non-square image', () => {
345
+ const { result } = renderHook( () => useCropperState() );
346
+
347
+ act( () => {
348
+ result.current.setImage( {
349
+ src: 'test.jpg',
350
+ naturalWidth: 1600,
351
+ naturalHeight: 900,
352
+ } );
353
+ } );
354
+
355
+ expect( result.current.isDirty ).toBe( false );
356
+ } );
357
+
358
+ // Regression: reset() on a loaded image must produce a "clean"
359
+ // snapshot that matches what the reducer actually stored
360
+ // (preserving the image, re-enforcing containment). Otherwise
361
+ // isDirty reports true right after reset.
362
+ it( 'should be false after reset on a loaded image', () => {
363
+ const { result } = renderHook( () => useCropperState() );
364
+
365
+ act( () => {
366
+ result.current.setImage( {
367
+ src: 'test.jpg',
368
+ naturalWidth: 1600,
369
+ naturalHeight: 900,
370
+ } );
371
+ } );
372
+
373
+ act( () => {
374
+ result.current.setZoom( 2 );
375
+ result.current.setRotation( 45 );
376
+ } );
377
+
378
+ expect( result.current.isDirty ).toBe( true );
379
+
380
+ act( () => {
381
+ result.current.reset();
382
+ } );
383
+
384
+ expect( result.current.isDirty ).toBe( false );
385
+ } );
386
+ } );
387
+
388
+ describe( 'containment enforcement', () => {
389
+ // For these tests, we need a loaded image so the reducer can
390
+ // compute minZoomForCover and restrict position.
391
+ function setupWithImage() {
392
+ const view = renderHook( () => useCropperState() );
393
+ act( () => {
394
+ view.result.current.setImage( {
395
+ src: 'test.jpg',
396
+ naturalWidth: 1000,
397
+ naturalHeight: 500,
398
+ } );
399
+ } );
400
+ return view;
401
+ }
402
+
403
+ it( 'should restrict position when SET_CROP is dispatched', () => {
404
+ const { result } = setupWithImage();
405
+
406
+ // At zoom=1, no panning is possible — the image exactly covers
407
+ // the crop area. Any pan value should be clamped to (0, 0).
408
+ act( () => {
409
+ result.current.setPan( { x: 0.5, y: 0.5 } );
410
+ } );
411
+
412
+ expect( result.current.state.pan.x ).toBeCloseTo( 0, 5 );
413
+ expect( result.current.state.pan.y ).toBeCloseTo( 0, 5 );
414
+ } );
415
+
416
+ it( 'should allow limited panning at zoom > 1', () => {
417
+ const { result } = setupWithImage();
418
+
419
+ act( () => {
420
+ result.current.setZoom( 2 );
421
+ } );
422
+
423
+ act( () => {
424
+ result.current.setPan( { x: 0.3, y: 0.3 } );
425
+ } );
426
+
427
+ // Should be within bounds (not clamped to 0)
428
+ expect( result.current.state.pan.x ).toBeGreaterThan( 0 );
429
+ expect( result.current.state.pan.y ).toBeGreaterThan( 0 );
430
+
431
+ // But if we try an extreme pan, it should be clamped
432
+ act( () => {
433
+ result.current.setPan( { x: 100, y: 100 } );
434
+ } );
435
+
436
+ expect( result.current.state.pan.x ).toBeLessThan( 1 );
437
+ expect( result.current.state.pan.y ).toBeLessThan( 1 );
438
+ } );
439
+
440
+ it( 'should bump zoom to cover crop during rotation', () => {
441
+ const { result } = setupWithImage();
442
+
443
+ expect( result.current.state.zoom ).toBe( 1 );
444
+
445
+ // Rotate 45 degrees via simple SET_ROTATION (no container).
446
+ // The crop rect is unchanged and zoom is bumped to cover.
447
+ act( () => {
448
+ result.current.setRotation( 45 );
449
+ } );
450
+
451
+ expect( result.current.state.rotation ).toBe( 45 );
452
+ expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 1 );
453
+ } );
454
+
455
+ it( 'should preserve zoom across rotation changes', () => {
456
+ const { result } = setupWithImage();
457
+
458
+ act( () => {
459
+ result.current.setZoom( 2 );
460
+ } );
461
+
462
+ act( () => {
463
+ result.current.setRotation( 45 );
464
+ } );
465
+
466
+ // Zoom should be at least 2 (may be higher for 45° coverage).
467
+ expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 2 );
468
+
469
+ act( () => {
470
+ result.current.setRotation( 0 );
471
+ } );
472
+
473
+ // Zoom should not drop below the user's explicit level.
474
+ // It only ratchets up; the user can zoom out explicitly if
475
+ // they want.
476
+ expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 2 );
477
+ } );
478
+
479
+ it( 'should reset pan when rotation changes', () => {
480
+ const { result } = setupWithImage();
481
+
482
+ // Zoom in and pan
483
+ act( () => {
484
+ result.current.setZoom( 3 );
485
+ } );
486
+ act( () => {
487
+ result.current.setPan( { x: 0.2, y: 0.2 } );
488
+ } );
489
+
490
+ expect( result.current.state.pan.x ).toBeCloseTo( 0.2 );
491
+
492
+ // Rotate — position should be re-restricted
493
+ act( () => {
494
+ result.current.setRotation( 45 );
495
+ } );
496
+
497
+ // The position should be within the new valid range
498
+ const { pan, zoom } = result.current.state;
499
+ // Verify the position is valid for the new state
500
+ expect( Math.abs( pan.x ) ).toBeLessThanOrEqual( zoom );
501
+ expect( Math.abs( pan.y ) ).toBeLessThanOrEqual( zoom );
502
+ } );
503
+
504
+ it( 'should not allow zoom below minZoomForCover', () => {
505
+ const { result } = setupWithImage();
506
+
507
+ // At rotation=0, minZoom is 1 for a full crop rect.
508
+ act( () => {
509
+ result.current.setZoom( 0.5 );
510
+ } );
511
+
512
+ expect( result.current.state.zoom ).toBeGreaterThanOrEqual( 1 );
513
+
514
+ // Zoom above maximum should be clamped to 10.
515
+ act( () => {
516
+ result.current.setZoom( 15 );
517
+ } );
518
+
519
+ expect( result.current.state.zoom ).toBe( 10 );
520
+ } );
521
+
522
+ it( 'should re-restrict position after zoom change', () => {
523
+ const { result } = setupWithImage();
524
+
525
+ // Zoom in and pan to the edge
526
+ act( () => {
527
+ result.current.setZoom( 4 );
528
+ } );
529
+ act( () => {
530
+ result.current.setPan( { x: 1, y: 1 } );
531
+ } );
532
+
533
+ const panAtZoom4 = { ...result.current.state.pan };
534
+
535
+ // Zoom out — pan should be reduced to stay within bounds
536
+ act( () => {
537
+ result.current.setZoom( 2 );
538
+ } );
539
+
540
+ expect(
541
+ Math.abs( result.current.state.pan.x )
542
+ ).toBeLessThanOrEqual( Math.abs( panAtZoom4.x ) );
543
+ } );
544
+
545
+ it( 'should rescale crop and bump zoom at 90-degree rotation for non-square image', () => {
546
+ const { result } = setupWithImage();
547
+ // Image is 1000x500 (2:1). At 90° the visual box flips from
548
+ // landscape to portrait. The crop rect is rescaled to preserve
549
+ // pixel size, and zoom is bumped to cover it.
550
+ act( () => {
551
+ result.current.setRotation( 90 );
552
+ } );
553
+
554
+ const { cropRect, zoom } = result.current.state;
555
+ // Crop pixel dimensions preserved — the normalized values
556
+ // change because the visual box dimensions swapped.
557
+ expect( cropRect.width ).toBeGreaterThan( 0 );
558
+ expect( cropRect.height ).toBeGreaterThan( 0 );
559
+ // Zoom bumps to cover the rescaled crop.
560
+ expect( zoom ).toBeGreaterThanOrEqual( 1 );
561
+ } );
562
+
563
+ it( 'should not reduce zoom when SET_CROP_RECT shrinks the crop', () => {
564
+ const { result } = setupWithImage();
565
+
566
+ // Zoom in explicitly.
567
+ act( () => {
568
+ result.current.setZoom( 2 );
569
+ } );
570
+ expect( result.current.state.zoom ).toBe( 2 );
571
+
572
+ // Rotate so that containment matters.
573
+ act( () => {
574
+ result.current.setRotation( 45 );
575
+ } );
576
+ const zoomAfterRotation = result.current.state.zoom;
577
+
578
+ // Now shrink the crop rect — zoom should not decrease.
579
+ act( () => {
580
+ result.current.setCropRect( {
581
+ x: 0.1,
582
+ y: 0.1,
583
+ width: 0.3,
584
+ height: 0.3,
585
+ } );
586
+ } );
587
+
588
+ expect( result.current.state.zoom ).toBeGreaterThanOrEqual(
589
+ zoomAfterRotation
590
+ );
591
+ } );
592
+
593
+ it( 'should bump zoom when SET_CROP_RECT requires it at rotation', () => {
594
+ const { result } = setupWithImage();
595
+
596
+ act( () => {
597
+ result.current.setRotation( 45 );
598
+ } );
599
+
600
+ // Zoom was already bumped by rotation to cover full crop.
601
+ const zoomAfterRotation = result.current.state.zoom;
602
+ expect( zoomAfterRotation ).toBeGreaterThan( 1 );
603
+
604
+ // Setting a smaller crop rect then a full one should keep zoom
605
+ // at the level needed for the full rect.
606
+ act( () => {
607
+ result.current.setCropRect( {
608
+ x: 0.2,
609
+ y: 0.2,
610
+ width: 0.6,
611
+ height: 0.6,
612
+ } );
613
+ } );
614
+
615
+ act( () => {
616
+ result.current.setCropRect( {
617
+ x: 0,
618
+ y: 0,
619
+ width: 1,
620
+ height: 1,
621
+ } );
622
+ } );
623
+
624
+ // Zoom should be at least what rotation required.
625
+ expect( result.current.state.zoom ).toBeGreaterThanOrEqual(
626
+ zoomAfterRotation
627
+ );
628
+ expect( result.current.state.cropRect.width ).toBeCloseTo( 1, 1 );
629
+ expect( result.current.state.cropRect.height ).toBeCloseTo( 1, 1 );
630
+ } );
631
+ } );
632
+
633
+ describe( 'SETTLE_CROP', () => {
634
+ function setupWithImage() {
635
+ const view = renderHook( () => useCropperState() );
636
+ act( () => {
637
+ view.result.current.setImage( {
638
+ src: 'test.jpg',
639
+ naturalWidth: 1000,
640
+ naturalHeight: 500,
641
+ } );
642
+ } );
643
+ return view;
644
+ }
645
+
646
+ it( 'expands a small crop to fill height and centers it', () => {
647
+ const { result } = setupWithImage();
648
+
649
+ // Shrink the crop rect to a small centered area.
650
+ act( () => {
651
+ result.current.setCropRect( {
652
+ x: 0.25,
653
+ y: 0.25,
654
+ width: 0.5,
655
+ height: 0.5,
656
+ } );
657
+ } );
658
+
659
+ act( () => {
660
+ result.current.settleCrop();
661
+ } );
662
+
663
+ const { cropRect } = result.current.state;
664
+ // Should expand to fill height (height=1) or width (width=1).
665
+ expect(
666
+ Math.abs( cropRect.height - 1 ) < 0.01 ||
667
+ Math.abs( cropRect.width - 1 ) < 0.01
668
+ ).toBe( true );
669
+ // Should be centered.
670
+ const cx = cropRect.x + cropRect.width / 2;
671
+ const cy = cropRect.y + cropRect.height / 2;
672
+ expect( cx ).toBeCloseTo( 0.5, 1 );
673
+ expect( cy ).toBeCloseTo( 0.5, 1 );
674
+ } );
675
+
676
+ it( 'preserves the image selection (visible content center does not jump)', () => {
677
+ const { result } = setupWithImage();
678
+
679
+ // Zoom in so there is room to pan.
680
+ act( () => {
681
+ result.current.setZoom( 3 );
682
+ } );
683
+
684
+ // Resize crop to a smaller centered area (center at 0.5, 0.5).
685
+ act( () => {
686
+ result.current.setCropRect( {
687
+ x: 0.25,
688
+ y: 0.25,
689
+ width: 0.5,
690
+ height: 0.5,
691
+ } );
692
+ } );
693
+
694
+ // Record pre-settle state.
695
+ const preCropRect = result.current.state.cropRect;
696
+ const preZoom = result.current.state.zoom;
697
+
698
+ act( () => {
699
+ result.current.settleCrop();
700
+ } );
701
+
702
+ const postCropRect = result.current.state.cropRect;
703
+ const postZoom = result.current.state.zoom;
704
+
705
+ // The zoom should scale by the expansion factor.
706
+ const s = postCropRect.height / preCropRect.height;
707
+ expect( postZoom ).toBeCloseTo( preZoom * s, 1 );
708
+
709
+ // The crop center should remain at (0.5, 0.5).
710
+ const postCx = postCropRect.x + postCropRect.width / 2;
711
+ const postCy = postCropRect.y + postCropRect.height / 2;
712
+ expect( postCx ).toBeCloseTo( 0.5, 1 );
713
+ expect( postCy ).toBeCloseTo( 0.5, 1 );
714
+ } );
715
+
716
+ it( 'is a no-op when crop is already full and centered', () => {
717
+ const { result } = setupWithImage();
718
+
719
+ // State is already default: full crop, centered.
720
+ const stateBefore = result.current.state;
721
+
722
+ act( () => {
723
+ result.current.settleCrop();
724
+ } );
725
+
726
+ const stateAfter = result.current.state;
727
+ expect( stateAfter.cropRect ).toEqual( stateBefore.cropRect );
728
+ expect( stateAfter.zoom ).toEqual( stateBefore.zoom );
729
+ expect( stateAfter.pan.x ).toBeCloseTo( stateBefore.pan.x, 5 );
730
+ expect( stateAfter.pan.y ).toBeCloseTo( stateBefore.pan.y, 5 );
731
+ } );
732
+ } );
733
+
734
+ describe( 'SNAP_ROTATE_90', () => {
735
+ function setupWithImage() {
736
+ const view = renderHook( () => useCropperState() );
737
+ act( () => {
738
+ view.result.current.setImage( {
739
+ src: 'test.jpg',
740
+ naturalWidth: 1000,
741
+ naturalHeight: 500,
742
+ } );
743
+ } );
744
+ return view;
745
+ }
746
+
747
+ it( 'after snap rotate CW, crop width and height are swapped', () => {
748
+ const { result } = setupWithImage();
749
+
750
+ // Set a non-square crop so the swap is visible.
751
+ act( () => {
752
+ result.current.setCropRect( {
753
+ x: 0.1,
754
+ y: 0.2,
755
+ width: 0.6,
756
+ height: 0.4,
757
+ } );
758
+ } );
759
+
760
+ const widthBefore = result.current.state.cropRect.width;
761
+ const heightBefore = result.current.state.cropRect.height;
762
+
763
+ act( () => {
764
+ result.current.snapRotate90( 1 );
765
+ } );
766
+
767
+ // After snap rotate, width and height should be swapped
768
+ // (though enforceContainment may adjust slightly).
769
+ const { cropRect } = result.current.state;
770
+ expect( cropRect.width ).toBeCloseTo( heightBefore, 1 );
771
+ expect( cropRect.height ).toBeCloseTo( widthBefore, 1 );
772
+ expect( result.current.state.rotation ).toBe( 90 );
773
+ } );
774
+
775
+ it( 'after snap rotate CCW, crop width and height are swapped', () => {
776
+ const { result } = setupWithImage();
777
+
778
+ act( () => {
779
+ result.current.setCropRect( {
780
+ x: 0.1,
781
+ y: 0.2,
782
+ width: 0.6,
783
+ height: 0.4,
784
+ } );
785
+ } );
786
+
787
+ const widthBefore = result.current.state.cropRect.width;
788
+ const heightBefore = result.current.state.cropRect.height;
789
+
790
+ act( () => {
791
+ result.current.snapRotate90( -1 );
792
+ } );
793
+
794
+ const { cropRect } = result.current.state;
795
+ expect( cropRect.width ).toBeCloseTo( heightBefore, 1 );
796
+ expect( cropRect.height ).toBeCloseTo( widthBefore, 1 );
797
+ expect( result.current.state.rotation ).toBe( 270 );
798
+ } );
799
+
800
+ it( 'after two snap rotates CW (180 degrees), dimensions return to original', () => {
801
+ const { result } = setupWithImage();
802
+
803
+ act( () => {
804
+ result.current.setCropRect( {
805
+ x: 0.1,
806
+ y: 0.2,
807
+ width: 0.6,
808
+ height: 0.4,
809
+ } );
810
+ } );
811
+
812
+ const widthBefore = result.current.state.cropRect.width;
813
+ const heightBefore = result.current.state.cropRect.height;
814
+
815
+ act( () => {
816
+ result.current.snapRotate90( 1 );
817
+ } );
818
+
819
+ act( () => {
820
+ result.current.snapRotate90( 1 );
821
+ } );
822
+
823
+ const { cropRect } = result.current.state;
824
+ expect( cropRect.width ).toBeCloseTo( widthBefore, 1 );
825
+ expect( cropRect.height ).toBeCloseTo( heightBefore, 1 );
826
+ expect( result.current.state.rotation ).toBe( 180 );
827
+ } );
828
+ } );
829
+
830
+ describe( 'public controller contract', () => {
831
+ it( 'does not expose the raw reducer dispatch', () => {
832
+ const { result } = renderHook( () => useCropperState() );
833
+
834
+ expect( '__dispatch' in result.current ).toBe( false );
835
+ } );
836
+ } );
837
+
838
+ describe( 'isStateDirty coverage guard', () => {
839
+ // This test ensures that isStateDirty checks ALL fields in CropperState.
840
+ // If you add a field to CropperState/DEFAULT_STATE and this test fails,
841
+ // you MUST also update isStateDirty() in use-cropper-state.ts.
842
+ it( 'should track all CropperState fields', () => {
843
+ // Flatten DEFAULT_STATE keys into dot-notation paths.
844
+ // e.g., { pan: { x: 0, y: 0 } } → ['crop.x', 'crop.y']
845
+ function flattenKeys(
846
+ obj: Record< string, unknown >,
847
+ prefix = ''
848
+ ): string[] {
849
+ const keys: string[] = [];
850
+ for ( const key of Object.keys( obj ) ) {
851
+ const fullKey = prefix ? `${ prefix }.${ key }` : key;
852
+ const value = obj[ key ];
853
+ if (
854
+ value !== null &&
855
+ typeof value === 'object' &&
856
+ ! Array.isArray( value )
857
+ ) {
858
+ keys.push(
859
+ ...flattenKeys(
860
+ value as Record< string, unknown >,
861
+ fullKey
862
+ )
863
+ );
864
+ } else {
865
+ keys.push( fullKey );
866
+ }
867
+ }
868
+ return keys.sort();
869
+ }
870
+
871
+ // The fields that isStateDirty currently checks.
872
+ // Keep this in sync — if you add a field to CropperState,
873
+ // add it here AND in isStateDirty().
874
+ const dirtyCheckedFields = [
875
+ 'pan.x',
876
+ 'pan.y',
877
+ 'cropRect.height',
878
+ 'cropRect.width',
879
+ 'cropRect.x',
880
+ 'cropRect.y',
881
+ 'flip.horizontal',
882
+ 'flip.vertical',
883
+ 'rotation',
884
+ 'zoom',
885
+ ].sort();
886
+
887
+ // 'image' is intentionally excluded — it's set once on load
888
+ // and doesn't represent a user edit.
889
+ // 'image' is set once on load.
890
+ // 'basePan', 'baseZoom', 'baseRotation' mirror the user's
891
+ // committed pose and are redundant for dirty tracking — if
892
+ // any base field differs from initial, the corresponding
893
+ // live field (crop/zoom/rotation) will also differ.
894
+ const excludedFields = [
895
+ 'image',
896
+ 'basePan',
897
+ 'baseZoom',
898
+ 'baseRotation',
899
+ ];
900
+
901
+ const stateKeys = flattenKeys(
902
+ DEFAULT_STATE as unknown as Record< string, unknown >
903
+ ).filter(
904
+ ( key ) =>
905
+ ! excludedFields.some(
906
+ ( ex ) => key === ex || key.startsWith( ex + '.' )
907
+ )
908
+ );
909
+
910
+ expect( stateKeys ).toEqual( dirtyCheckedFields );
911
+ } );
912
+ } );
913
+ } );