|
1 | 1 | import React, { createRef } from 'react' |
2 | | -import { render, fireEvent, waitFor } from '@testing-library/react' |
| 2 | +import { render, fireEvent, waitFor, act } from '@testing-library/react' |
3 | 3 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
4 | 4 | import AvatarEditor, { type AvatarEditorRef, useAvatarEditor } from '../index' |
5 | 5 |
|
| 6 | +// Makes every Image a 400x300 landscape image that loads as soon as src is set |
| 7 | +const mockImageLoad = () => |
| 8 | + vi.spyOn(globalThis, 'Image').mockImplementation(function ( |
| 9 | + this: HTMLImageElement, |
| 10 | + ) { |
| 11 | + const listeners: Record<string, (() => void)[]> = {} |
| 12 | + this.addEventListener = (type: string, fn: () => void) => { |
| 13 | + ;(listeners[type] ??= []).push(fn) |
| 14 | + } |
| 15 | + Object.defineProperty(this, 'src', { |
| 16 | + set: () => setTimeout(() => listeners['load']?.forEach((fn) => fn()), 0), |
| 17 | + }) |
| 18 | + Object.defineProperty(this, 'width', { value: 400 }) |
| 19 | + Object.defineProperty(this, 'height', { value: 300 }) |
| 20 | + return this |
| 21 | + } as unknown as typeof Image) |
| 22 | + |
6 | 23 | const cropCenter = (ref: React.RefObject<AvatarEditorRef | null>) => { |
7 | 24 | const rect = ref.current!.getCroppingRect() |
8 | 25 | return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 } |
@@ -356,22 +373,7 @@ describe('AvatarEditor', () => { |
356 | 373 |
|
357 | 374 | describe('with a loaded image', () => { |
358 | 375 | beforeEach(() => { |
359 | | - // 400x300 landscape image that loads as soon as src is set |
360 | | - vi.spyOn(globalThis, 'Image').mockImplementation(function ( |
361 | | - this: HTMLImageElement, |
362 | | - ) { |
363 | | - const listeners: Record<string, (() => void)[]> = {} |
364 | | - this.addEventListener = (type: string, fn: () => void) => { |
365 | | - ;(listeners[type] ??= []).push(fn) |
366 | | - } |
367 | | - Object.defineProperty(this, 'src', { |
368 | | - set: () => |
369 | | - setTimeout(() => listeners['load']?.forEach((fn) => fn()), 0), |
370 | | - }) |
371 | | - Object.defineProperty(this, 'width', { value: 400 }) |
372 | | - Object.defineProperty(this, 'height', { value: 300 }) |
373 | | - return this |
374 | | - } as unknown as typeof Image) |
| 376 | + mockImageLoad() |
375 | 377 | }) |
376 | 378 |
|
377 | 379 | afterEach(() => { |
@@ -613,4 +615,113 @@ describe('AvatarEditor', () => { |
613 | 615 | fireEvent.keyDown(canvas, { key: 'ArrowUp' }) |
614 | 616 | }) |
615 | 617 | }) |
| 618 | + // ------------------------------------------------------- |
| 619 | + // Responsive width / height (#399) |
| 620 | + // ------------------------------------------------------- |
| 621 | + describe('percentage width and height', () => { |
| 622 | + let parentWidth: number |
| 623 | + let resizeCallback: (() => void) | undefined |
| 624 | + |
| 625 | + beforeEach(() => { |
| 626 | + parentWidth = 500 |
| 627 | + resizeCallback = undefined |
| 628 | + vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockImplementation( |
| 629 | + () => parentWidth, |
| 630 | + ) |
| 631 | + vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockReturnValue( |
| 632 | + 300, |
| 633 | + ) |
| 634 | + vi.stubGlobal( |
| 635 | + 'ResizeObserver', |
| 636 | + class { |
| 637 | + constructor(cb: () => void) { |
| 638 | + resizeCallback = cb |
| 639 | + } |
| 640 | + observe() {} |
| 641 | + disconnect() {} |
| 642 | + }, |
| 643 | + ) |
| 644 | + }) |
| 645 | + |
| 646 | + afterEach(() => { |
| 647 | + vi.restoreAllMocks() |
| 648 | + vi.unstubAllGlobals() |
| 649 | + }) |
| 650 | + |
| 651 | + it('fills the given share of the parent, border included', () => { |
| 652 | + const { container } = render( |
| 653 | + <div> |
| 654 | + <AvatarEditor width="100%" height="50%" border={25} /> |
| 655 | + </div>, |
| 656 | + ) |
| 657 | + const canvas = container.querySelector('canvas')! |
| 658 | + expect(canvas.style.width).toBe('500px') |
| 659 | + expect(canvas.style.height).toBe('150px') |
| 660 | + expect(canvas.style.display).toBe('block') |
| 661 | + }) |
| 662 | + |
| 663 | + it('exports the crop area without the border', () => { |
| 664 | + const ref = createRef<AvatarEditorRef>() |
| 665 | + render( |
| 666 | + <div> |
| 667 | + <AvatarEditor ref={ref} width="100%" height={200} border={25} /> |
| 668 | + </div>, |
| 669 | + ) |
| 670 | + const exported = ref.current!.getImageScaledToCanvas() |
| 671 | + expect(exported.width).toBe(450) |
| 672 | + expect(exported.height).toBe(200) |
| 673 | + }) |
| 674 | + |
| 675 | + it('follows the parent when it resizes', () => { |
| 676 | + const { container } = render( |
| 677 | + <div> |
| 678 | + <AvatarEditor width="100%" height={200} border={0} /> |
| 679 | + </div>, |
| 680 | + ) |
| 681 | + const canvas = container.querySelector('canvas')! |
| 682 | + expect(canvas.style.width).toBe('500px') |
| 683 | + |
| 684 | + parentWidth = 320 |
| 685 | + act(() => resizeCallback?.()) |
| 686 | + expect(canvas.style.width).toBe('320px') |
| 687 | + expect(canvas.getAttribute('width')).toBe('320') |
| 688 | + }) |
| 689 | + |
| 690 | + it('keeps numeric sizes unchanged and inline', () => { |
| 691 | + const { container } = render( |
| 692 | + <div> |
| 693 | + <AvatarEditor width={200} height={200} border={25} /> |
| 694 | + </div>, |
| 695 | + ) |
| 696 | + const canvas = container.querySelector('canvas')! |
| 697 | + expect(canvas.style.width).toBe('250px') |
| 698 | + expect(canvas.style.display).toBe('') |
| 699 | + }) |
| 700 | + |
| 701 | + it('does not reload the image when the size changes', async () => { |
| 702 | + const imageSpy = mockImageLoad() |
| 703 | + const onLoadSuccess = vi.fn() |
| 704 | + const { container } = render( |
| 705 | + <div> |
| 706 | + <AvatarEditor |
| 707 | + image="https://example.com/photo.jpg" |
| 708 | + width="100%" |
| 709 | + height={200} |
| 710 | + border={0} |
| 711 | + position={{ x: 0.4, y: 0.5 }} |
| 712 | + onLoadSuccess={onLoadSuccess} |
| 713 | + /> |
| 714 | + </div>, |
| 715 | + ) |
| 716 | + await waitFor(() => expect(onLoadSuccess).toHaveBeenCalled()) |
| 717 | + const loads = imageSpy.mock.calls.length |
| 718 | + |
| 719 | + parentWidth = 300 |
| 720 | + act(() => resizeCallback?.()) |
| 721 | + |
| 722 | + expect(container.querySelector('canvas')!.style.width).toBe('300px') |
| 723 | + expect(imageSpy.mock.calls.length).toBe(loads) |
| 724 | + expect(onLoadSuccess).toHaveBeenCalledTimes(1) |
| 725 | + }) |
| 726 | + }) |
616 | 727 | }) |
0 commit comments