import {
  useSliderData,
  getValueByMouseEvent,
  getValueByKeyboard,
  addListener,
  removeListener,
} from '../utils';

describe('useSliderData', () => {
  it('returns correct value', () => {
    const data1 = {
      min: 0,
      max: 100,
      value: 50,
    };
    expect(useSliderData(data1).valueInNumber).toBe(50);
    expect(useSliderData(data1).valueInPercent).toBe(50);

    const data2 = {
      min: 20,
      max: 80,
      value: 40,
    };
    expect(useSliderData(data2).valueInNumber).toBe(40);
    expect(useSliderData(data2).valueInPercent).toBe((1 / 3) * 100);
  });
});

describe('getValueByMouseEvent', () => {
  it('returns value correctly', () => {
    const mouseEvent1 = {
      type: 'mousemove',
      clientX: 100,
      clientY: 0,
    };
    const mouseEvent2 = {
      type: 'mousemove',
      clientX: 150,
      clientY: 0,
    };
    expect(getValueByMouseEvent(mouseEvent1, 200, 0, 0, 100, 1)).toBe(50);
    expect(getValueByMouseEvent(mouseEvent2, 200, 0, 0, 100, 1)).toBe(75);
  });
});

describe('getValueByKeyboard', () => {
  it('returns value correctly', () => {
    expect(getValueByKeyboard(40, 5, 0, 100, 'ArrowRight')).toBe(45);
    expect(getValueByKeyboard(40, 5, 0, 100, 'ArrowLeft')).toBe(35);
    expect(getValueByKeyboard(40, 5, 0, 100, 'KeyA')).toBe(40);
  });
});

describe('addListener', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('must call document addEventListener properly', () => {
    const mockOnMouseMove = jest.fn();
    const mockOnMouseUp = jest.fn();

    const mockAddEventListener = jest.spyOn(document, 'addEventListener');

    addListener(mockOnMouseMove, mockOnMouseUp);

    expect(mockAddEventListener).toBeCalledWith('mousemove', mockOnMouseMove);
    expect(mockAddEventListener).toBeCalledWith('touchmove', mockOnMouseMove);
    expect(mockAddEventListener).toBeCalledWith('mouseup', mockOnMouseUp);
    expect(mockAddEventListener).toBeCalledWith('touchend', mockOnMouseUp);
  });
});

describe('removeListener', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });
  it('must call document removeEventListener properly', () => {
    const mockOnMouseMove = jest.fn();

    const mockRemoveEventListener = jest.spyOn(document, 'removeEventListener');

    removeListener(mockOnMouseMove)();

    expect(mockRemoveEventListener).toBeCalledWith(
      'mousemove',
      mockOnMouseMove
    );
    expect(mockRemoveEventListener).toBeCalledWith(
      'touchmove',
      mockOnMouseMove
    );
  });
});
