import { addIndex, forEach } from 'ramda';

import getImageUrlByWidth from './getImageUrlByWidth';

describe('getImageUrlByWidth', () => {
  const testCases = [
    { width: 1024,
      filteredSizes: {
        '1024x1022': { source_url: 'path/to/1024x1022', width: 1024 },
        '1024x1024': { source_url: 'path/to/1024x1024', width: 1024 },
        '1024x1026': { source_url: 'path/to/1024x1026', width: 1024 } },
      expected: 'path/to/1024x1022',
    },
    {
      width: 640,
      filteredSizes: {
        '640x360': { source_url: 'path/to/640x360', width: 640 },
        '320x260': { source_url: 'path/to/320x260', width: 320 },
        '1024x100': { source_url: 'path/to/1024x100', width: 1024 },
      },
      expected: 'path/to/640x360',
    },
  ];

  addIndex(forEach)(({ width, filteredSizes, expected }, index) => {
    it(`gets correct source_url for the passed width #${index + 1}`, () => {
      expect(getImageUrlByWidth(width, filteredSizes)).toBe(expected);
    });
  }, testCases);

  it('gets largest image closest to width if available', () => {
    const filteredSizes = {
      '1024x100': { source_url: 'path/to/1024x100', width: 1024 },
      '640x360': { source_url: 'path/to/640x360', width: 640 },
      '320x260': { source_url: 'path/to/320x260', width: 320 },
    };
    expect(getImageUrlByWidth(400, filteredSizes))
      .toBe('path/to/640x360');
  });

  it('gets the smallest image closest to width if no large one exist', () => {
    const filteredSizes = {
      '320x260': { source_url: 'path/to/320x260', width: 320 },
      '120x60': { source_url: 'path/to/320x260', width: 120 },
    };

    expect(getImageUrlByWidth(640, filteredSizes))
      .toBe('path/to/320x260');
  });

  it("returns undefined if it can't find any image", () => {
    expect(getImageUrlByWidth(300, {})).toBeUndefined();
  });
});
