import React from 'react';
import { render, cleanup, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import ScrollToTop from '.';

describe('ScrollToTop Component', () => {
  test('render button', () => {
    const { container } = render(<ScrollToTop />);
    expect(container.querySelectorAll('button')).toHaveLength(1);
  });

  test('on click', () => {
    const { container } = render(<ScrollToTop />);
    const scrollToSpy = jest.fn();
    window.scrollTo = scrollToSpy;
    const btn = container.querySelector('button');
    fireEvent.click(btn);
    expect(scrollToSpy).toHaveBeenCalled();
  });

  test('on scroll', () => {
    render(<ScrollToTop scrollTo="50" />);
    fireEvent.scroll(window, { target: { pageYOffset: 501 } });
    fireEvent.scroll(window, { target: { pageYOffset: 301 } });
  });
});
afterEach(() => {
  cleanup();
});
