Skip to content

Commit 52fb1f7

Browse files
committed
fix: not working resize API
1 parent e82c045 commit 52fb1f7

File tree

7 files changed

+363
-24
lines changed

7 files changed

+363
-24
lines changed

.changeset/quiet-wings-sip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zag-js/image-cropper": patch
3+
---
4+
5+
Fix not working resize API

e2e/image-cropper.e2e.ts

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,306 @@ test.describe("image-cropper / fixedCropArea", () => {
385385
})
386386
})
387387

388+
test.describe("image-cropper / resize API", () => {
389+
test.beforeEach(async ({ page }) => {
390+
I = new ImageCropperModel(page)
391+
await I.goto()
392+
await I.waitForImageLoad()
393+
})
394+
395+
test("[api] should grow selection using right handle", async () => {
396+
const initialRect = await I.getSelectionRect()
397+
398+
await I.selectResizeHandle("right")
399+
await I.setResizeStep(20)
400+
await I.clickGrow()
401+
await I.wait(100)
402+
403+
const newRect = await I.getSelectionRect()
404+
405+
expect(newRect.width).toBe(initialRect.width + 20)
406+
expect(newRect.height).toBe(initialRect.height)
407+
expect(newRect.x).toBe(initialRect.x)
408+
expect(newRect.y).toBe(initialRect.y)
409+
})
410+
411+
test("[api] should shrink selection using right handle", async () => {
412+
const initialRect = await I.getSelectionRect()
413+
414+
await I.selectResizeHandle("right")
415+
await I.setResizeStep(15)
416+
await I.clickShrink()
417+
await I.wait(100)
418+
419+
const newRect = await I.getSelectionRect()
420+
421+
expect(newRect.width).toBe(initialRect.width - 15)
422+
expect(newRect.height).toBe(initialRect.height)
423+
expect(newRect.x).toBe(initialRect.x)
424+
expect(newRect.y).toBe(initialRect.y)
425+
})
426+
427+
test("[api] should grow selection using left handle", async () => {
428+
const initialRect = await I.getSelectionRect()
429+
430+
await I.selectResizeHandle("left")
431+
await I.setResizeStep(20)
432+
await I.clickGrow()
433+
await I.wait(100)
434+
435+
const newRect = await I.getSelectionRect()
436+
437+
expect(newRect.width).toBe(initialRect.width + 20)
438+
expect(newRect.height).toBe(initialRect.height)
439+
expect(newRect.x).toBe(initialRect.x - 20)
440+
expect(newRect.y).toBe(initialRect.y)
441+
})
442+
443+
test("[api] should shrink selection using left handle", async () => {
444+
const initialRect = await I.getSelectionRect()
445+
446+
await I.selectResizeHandle("left")
447+
await I.setResizeStep(15)
448+
await I.clickShrink()
449+
await I.wait(100)
450+
451+
const newRect = await I.getSelectionRect()
452+
453+
expect(newRect.width).toBe(initialRect.width - 15)
454+
expect(newRect.height).toBe(initialRect.height)
455+
expect(newRect.x).toBe(initialRect.x + 15)
456+
expect(newRect.y).toBe(initialRect.y)
457+
})
458+
459+
test("[api] should grow selection using bottom handle", async () => {
460+
const initialRect = await I.getSelectionRect()
461+
462+
await I.selectResizeHandle("bottom")
463+
await I.setResizeStep(25)
464+
await I.clickGrow()
465+
await I.wait(100)
466+
467+
const newRect = await I.getSelectionRect()
468+
469+
expect(newRect.width).toBe(initialRect.width)
470+
expect(newRect.height).toBe(initialRect.height + 25)
471+
expect(newRect.x).toBe(initialRect.x)
472+
expect(newRect.y).toBe(initialRect.y)
473+
})
474+
475+
test("[api] should shrink selection using bottom handle", async () => {
476+
const initialRect = await I.getSelectionRect()
477+
478+
await I.selectResizeHandle("bottom")
479+
await I.setResizeStep(20)
480+
await I.clickShrink()
481+
await I.wait(100)
482+
483+
const newRect = await I.getSelectionRect()
484+
485+
expect(newRect.width).toBe(initialRect.width)
486+
expect(newRect.height).toBe(initialRect.height - 20)
487+
expect(newRect.x).toBe(initialRect.x)
488+
expect(newRect.y).toBe(initialRect.y)
489+
})
490+
491+
test("[api] should grow selection using top handle", async () => {
492+
const initialRect = await I.getSelectionRect()
493+
494+
await I.selectResizeHandle("top")
495+
await I.setResizeStep(25)
496+
await I.clickGrow()
497+
await I.wait(100)
498+
499+
const newRect = await I.getSelectionRect()
500+
501+
expect(newRect.width).toBe(initialRect.width)
502+
expect(newRect.height).toBe(initialRect.height + 25)
503+
expect(newRect.x).toBe(initialRect.x)
504+
expect(newRect.y).toBe(initialRect.y - 25)
505+
})
506+
507+
test("[api] should shrink selection using top handle", async () => {
508+
const initialRect = await I.getSelectionRect()
509+
510+
await I.selectResizeHandle("top")
511+
await I.setResizeStep(20)
512+
await I.clickShrink()
513+
await I.wait(100)
514+
515+
const newRect = await I.getSelectionRect()
516+
517+
expect(newRect.width).toBe(initialRect.width)
518+
expect(newRect.height).toBe(initialRect.height - 20)
519+
expect(newRect.x).toBe(initialRect.x)
520+
expect(newRect.y).toBe(initialRect.y + 20)
521+
})
522+
523+
test("[api] should grow selection using bottom-right corner handle", async () => {
524+
const initialRect = await I.getSelectionRect()
525+
526+
await I.selectResizeHandle("bottom-right")
527+
await I.setResizeStep(30)
528+
await I.clickGrow()
529+
await I.wait(100)
530+
531+
const newRect = await I.getSelectionRect()
532+
533+
expect(newRect.width).toBe(initialRect.width + 30)
534+
expect(newRect.height).toBe(initialRect.height + 30)
535+
expect(newRect.x).toBe(initialRect.x)
536+
expect(newRect.y).toBe(initialRect.y)
537+
})
538+
539+
test("[api] should shrink selection using bottom-right corner handle", async () => {
540+
const initialRect = await I.getSelectionRect()
541+
542+
await I.selectResizeHandle("bottom-right")
543+
await I.setResizeStep(25)
544+
await I.clickShrink()
545+
await I.wait(100)
546+
547+
const newRect = await I.getSelectionRect()
548+
549+
expect(newRect.width).toBe(initialRect.width - 25)
550+
expect(newRect.height).toBe(initialRect.height - 25)
551+
expect(newRect.x).toBe(initialRect.x)
552+
expect(newRect.y).toBe(initialRect.y)
553+
})
554+
555+
test("[api] should grow selection using top-left corner handle", async () => {
556+
const initialRect = await I.getSelectionRect()
557+
558+
await I.selectResizeHandle("top-left")
559+
await I.setResizeStep(30)
560+
await I.clickGrow()
561+
await I.wait(100)
562+
563+
const newRect = await I.getSelectionRect()
564+
565+
expect(newRect.width).toBe(initialRect.width + 30)
566+
expect(newRect.height).toBe(initialRect.height + 30)
567+
expect(newRect.x).toBe(initialRect.x - 30)
568+
expect(newRect.y).toBe(initialRect.y - 30)
569+
})
570+
571+
test("[api] should shrink selection using top-left corner handle", async () => {
572+
const initialRect = await I.getSelectionRect()
573+
574+
await I.selectResizeHandle("top-left")
575+
await I.setResizeStep(25)
576+
await I.clickShrink()
577+
await I.wait(100)
578+
579+
const newRect = await I.getSelectionRect()
580+
581+
expect(newRect.width).toBe(initialRect.width - 25)
582+
expect(newRect.height).toBe(initialRect.height - 25)
583+
expect(newRect.x).toBe(initialRect.x + 25)
584+
expect(newRect.y).toBe(initialRect.y + 25)
585+
})
586+
587+
test("[api] should grow selection using top-right corner handle", async () => {
588+
const initialRect = await I.getSelectionRect()
589+
590+
await I.selectResizeHandle("top-right")
591+
await I.setResizeStep(30)
592+
await I.clickGrow()
593+
await I.wait(100)
594+
595+
const newRect = await I.getSelectionRect()
596+
597+
expect(newRect.width).toBe(initialRect.width + 30)
598+
expect(newRect.height).toBe(initialRect.height + 30)
599+
expect(newRect.x).toBe(initialRect.x)
600+
expect(newRect.y).toBe(initialRect.y - 30)
601+
})
602+
603+
test("[api] should shrink selection using top-right corner handle", async () => {
604+
const initialRect = await I.getSelectionRect()
605+
606+
await I.selectResizeHandle("top-right")
607+
await I.setResizeStep(25)
608+
await I.clickShrink()
609+
await I.wait(100)
610+
611+
const newRect = await I.getSelectionRect()
612+
613+
expect(newRect.width).toBe(initialRect.width - 25)
614+
expect(newRect.height).toBe(initialRect.height - 25)
615+
expect(newRect.x).toBe(initialRect.x)
616+
expect(newRect.y).toBe(initialRect.y + 25)
617+
})
618+
619+
test("[api] should grow selection using bottom-left corner handle", async () => {
620+
const initialRect = await I.getSelectionRect()
621+
622+
await I.selectResizeHandle("bottom-left")
623+
await I.setResizeStep(30)
624+
await I.clickGrow()
625+
await I.wait(100)
626+
627+
const newRect = await I.getSelectionRect()
628+
629+
expect(newRect.width).toBe(initialRect.width + 30)
630+
expect(newRect.height).toBe(initialRect.height + 30)
631+
expect(newRect.x).toBe(initialRect.x - 30)
632+
expect(newRect.y).toBe(initialRect.y)
633+
})
634+
635+
test("[api] should shrink selection using bottom-left corner handle", async () => {
636+
const initialRect = await I.getSelectionRect()
637+
638+
await I.selectResizeHandle("bottom-left")
639+
await I.setResizeStep(25)
640+
await I.clickShrink()
641+
await I.wait(100)
642+
643+
const newRect = await I.getSelectionRect()
644+
645+
expect(newRect.width).toBe(initialRect.width - 25)
646+
expect(newRect.height).toBe(initialRect.height - 25)
647+
expect(newRect.x).toBe(initialRect.x + 25)
648+
expect(newRect.y).toBe(initialRect.y)
649+
})
650+
651+
test("[api] should respect minimum size when shrinking", async () => {
652+
await I.controls.num("minWidth", "100")
653+
await I.controls.num("minHeight", "80")
654+
await I.wait(100)
655+
656+
await I.selectResizeHandle("bottom-right")
657+
await I.setResizeStep(500)
658+
await I.clickShrink()
659+
await I.wait(100)
660+
661+
const { width, height } = await I.getSelectionRect()
662+
663+
expect(width).toBe(100)
664+
expect(height).toBe(80)
665+
})
666+
667+
test("[api] should respect maximum size when growing", async () => {
668+
await I.controls.num("maxWidth", "200")
669+
await I.controls.num("maxHeight", "150")
670+
await I.wait(100)
671+
672+
await I.selectResizeHandle("bottom-right")
673+
await I.setResizeStep(500)
674+
await I.clickGrow()
675+
await I.wait(100)
676+
677+
const selectionRect = await I.getSelectionRect()
678+
const viewportRect = await I.getViewportRect()
679+
680+
const expectedWidth = Math.min(200, viewportRect.width)
681+
const expectedHeight = Math.min(150, viewportRect.height)
682+
683+
expect(selectionRect.width).toBe(expectedWidth)
684+
expect(selectionRect.height).toBe(expectedHeight)
685+
})
686+
})
687+
388688
test.describe("image-cropper / circle", () => {
389689
test.beforeEach(async ({ page }) => {
390690
I = new ImageCropperModel(page)

e2e/models/image-cropper.model.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,36 @@ export class ImageCropperModel extends Model {
266266
await this.page.keyboard.up(mod)
267267
}
268268
}
269+
270+
get resizeHandleSelect() {
271+
return this.page.locator("[data-testid='resize-handle-select']")
272+
}
273+
274+
get resizeStepInput() {
275+
return this.page.locator("[data-testid='resize-step-input']")
276+
}
277+
278+
get growButton() {
279+
return this.page.locator("[data-testid='grow-button']")
280+
}
281+
282+
get shrinkButton() {
283+
return this.page.locator("[data-testid='shrink-button']")
284+
}
285+
286+
async selectResizeHandle(position: string) {
287+
await this.resizeHandleSelect.selectOption(position)
288+
}
289+
290+
async setResizeStep(step: number) {
291+
await this.resizeStepInput.fill(String(step))
292+
}
293+
294+
async clickGrow() {
295+
await this.growButton.click()
296+
}
297+
298+
async clickShrink() {
299+
await this.shrinkButton.click()
300+
}
269301
}

examples/next-ts/pages/image-cropper.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export default function Page() {
156156
<label>
157157
Resize handle:
158158
<select
159+
data-testid="resize-handle-select"
159160
value={selectedHandle}
160161
onChange={(event) => setSelectedHandle(event.currentTarget.value as imageCropper.HandlePosition)}
161162
>
@@ -169,6 +170,7 @@ export default function Page() {
169170
<label>
170171
Resize step (px):
171172
<input
173+
data-testid="resize-step-input"
172174
type="number"
173175
min={1}
174176
value={resizeStep}
@@ -179,10 +181,10 @@ export default function Page() {
179181
/>
180182
</label>
181183
<div>
182-
<button type="button" onClick={() => applyResize("grow")}>
184+
<button type="button" data-testid="grow-button" onClick={() => applyResize("grow")}>
183185
Grow selection
184186
</button>
185-
<button type="button" onClick={() => applyResize("shrink")}>
187+
<button type="button" data-testid="shrink-button" onClick={() => applyResize("shrink")}>
186188
Shrink selection
187189
</button>
188190
</div>

0 commit comments

Comments
 (0)