import { focusInput, generateFocusBarStyle } from '../utils';

describe('focusInput', () => {
  it('focuses on passed in element', () => {
    const input = document.createElement('input');
    jest.spyOn(input, 'focus');
    focusInput(input);

    expect(input.focus).toHaveBeenCalledTimes(1);
  });

  it('does nothing when element is null', () => {
    expect(focusInput(null)).toBe(undefined);
  });
});

describe('generateFocusBarStyle', () => {
  it('returns width and left position for focus bar', () => {
    const input = document.createElement('input');
    input.style.width = '200px';
    input.style.paddingLeft = '8px';
    jest.spyOn(input, 'offsetLeft', 'get').mockImplementation(() => 8);

    expect(generateFocusBarStyle(input)).toEqual({
      width: '200px',
      left: 'calc(8px + 8px)',
    });
  });

  it('sets width to 0 when element is null', () => {
    expect(generateFocusBarStyle(null)).toEqual({
      width: '0px',
      left: 'calc(0px + 0px)',
    });
  });
});
