diff --git a/app/javascript/alchemy_admin/picture_editors.js b/app/javascript/alchemy_admin/picture_editors.js index ae2a3679ca..c395054ece 100644 --- a/app/javascript/alchemy_admin/picture_editors.js +++ b/app/javascript/alchemy_admin/picture_editors.js @@ -7,7 +7,7 @@ const UPDATE_DELAY = 125 const IMAGE_PLACEHOLDER = '' const THUMBNAIL_SIZE = "160x120" -class PictureEditor { +export class PictureEditor { constructor(container) { this.container = container this.cropFromField = container.querySelector("[data-crop-from]") @@ -131,10 +131,10 @@ class PictureEditor { if (!this.imageCropperEnabled) return [] const mask = this.targetSize.split("x").map((n) => parseInt(n)) - const zoom = max([ + const zoom = max( mask[0] / this.imageFileWidth, mask[1] / this.imageFileHeight - ]) + ) return [Math.round(mask[0] / zoom), Math.round(mask[1] / zoom)] } diff --git a/spec/javascript/alchemy_admin/picture_editors.spec.js b/spec/javascript/alchemy_admin/picture_editors.spec.js new file mode 100644 index 0000000000..5731b24325 --- /dev/null +++ b/spec/javascript/alchemy_admin/picture_editors.spec.js @@ -0,0 +1,148 @@ +import { PictureEditor } from "alchemy_admin/picture_editors" + +jest.mock("alchemy_admin/image_loader", () => ({ + __esModule: true, + default: jest.fn().mockImplementation(() => ({ + load: jest.fn() + })) +})) + +describe("PictureEditor", () => { + describe("defaultCropSize", () => { + describe("when image cropper is enabled", () => { + beforeEach(() => { + document.body.innerHTML = ` +