import { MAX_FILE_SIZE, computeResizedDimensions } from './imageResize';

describe('imageResize', () => {
  describe('MAX_FILE_SIZE', () => {
    it('shouldCapUploadsAtOneMegabyte', () => {
      expect(MAX_FILE_SIZE).toBe(1024 * 1024);
    });
  });

  describe('computeResizedDimensions', () => {
    it('shouldLeaveSmallerImagesUntouched', () => {
      expect(computeResizedDimensions(120, 80, 300, 300)).toEqual({ width: 120, height: 80 });
    });

    it('shouldNotEnlargeImagesSmallerThanTheMaximum', () => {
      const { width, height } = computeResizedDimensions(50, 50, 300, 300);

      expect(width).toBeLessThanOrEqual(50);
      expect(height).toBeLessThanOrEqual(50);
    });

    it('shouldKeepDimensionsThatExactlyMatchTheMaximum', () => {
      expect(computeResizedDimensions(300, 300, 300, 300)).toEqual({ width: 300, height: 300 });
    });

    it('shouldShrinkLandscapeImagesKeepingTheirRatio', () => {
      expect(computeResizedDimensions(1200, 600, 300, 300)).toEqual({ width: 300, height: 150 });
    });

    it('shouldShrinkPortraitImagesKeepingTheirRatio', () => {
      expect(computeResizedDimensions(600, 1200, 300, 300)).toEqual({ width: 150, height: 300 });
    });

    it('shouldNeverExceedTheMaximumForAnAwkwardRatio', () => {
      const { width, height } = computeResizedDimensions(1000, 333, 300, 300);

      expect(width).toBeLessThanOrEqual(300);
      expect(height).toBeLessThanOrEqual(300);
      expect(width).toBe(300);
      expect(height).toBe(100);
    });

    it('shouldReturnWholePixels', () => {
      const { width, height } = computeResizedDimensions(1001, 337, 300, 300);

      expect(Number.isInteger(width)).toBe(true);
      expect(Number.isInteger(height)).toBe(true);
    });
  });
});
