jest.mock("jamplay-common/client/utils")
jest.mock("react-dom")
import * as React from "react"
import { create } from "react-test-renderer"
import { createRenderer } from "react-test-renderer/shallow"

import ReviewList from "../ReviewList.component"
import ConfirmDeleteReviewDialog from "../ConfirmDeleteReviewDialog.component"
import { getReview } from "./index.stories"

import { isElementOnScreen } from "jamplay-common/client/utils"

Date.now = function now() {
  return 1530517594018
}
describe("ReviewList", () => {
  it("ConfirmDialog should render correctly", () => {
    const t = create(
      <ConfirmDeleteReviewDialog/>
    )
    expect(t.toJSON()).toMatchSnapshot()
    t.unmount()
  })
  it("should render reviews with loading state correctly", () => {
    const t = create(
      <ReviewList
        reviews={[getReview(), getReview(), getReview()]}
        loading={true}
        hasMore={true}
      />
    )
    expect(t.toJSON()).toMatchSnapshot()
    t.unmount()
  })
  it("should load more if on screen", () => {
    const renderer = createRenderer()
    const onLoadMore = jest.fn(() => Promise.resolve())
    renderer.render(
      <ReviewList
        reviews={[getReview(), getReview(), getReview()]}
        loading={false}
        hasMore={true}
        onLoadMore={onLoadMore}
      />
    )
    const inst = renderer.getMountedInstance() as any
    inst.loadMoreChecker = {}
    const mock: jest.Mock = isElementOnScreen as any
    mock.mockImplementationOnce(() => false)
    inst.handleScroll()
    expect(mock).toBeCalled()
    expect(onLoadMore).not.toBeCalled()
    mock.mockImplementationOnce(() => true)
    inst.handleScroll()
    expect(onLoadMore).toBeCalled()
  })
})
