import * as React from "react"
import { create } from "react-test-renderer"
import { RatingStarIndicator } from "../"
import { getStars } from "../RatingStarIndicator"
import { Icon } from "../../../icon"

it("should getStars calculate correctly", () => {
  const stars = [0, 1, 2, 3, 4, 5]
  const onClick = jest.fn()
  const onMouseEnter = jest.fn()

  for (const s of stars) {
    const starComponents = getStars(s, onClick, onMouseEnter)
    expect(starComponents).toBeDefined()

    const t = create(<div>{starComponents}</div>)

    const blank = t.root.findAll((node) => node.type === (Icon as any) && node.props.name === "star-o")
    const solid = t.root.findAll((node) => node.type === (Icon as any) && node.props.name === "star")

    expect(blank.length).toEqual(5 - s)
    expect(solid.length).toEqual(s)

  }

})

it("should getStars with 4.8 avg and return 5 fill stars", () => {
  const t = create(<RatingStarIndicator average={4.8} count={31}  />)
  const blank = t.root.findAll((node) => node.type === (Icon as any) && node.props.name === "star-o")
  const solid = t.root.findAll((node) => node.type === (Icon as any) && node.props.name === "star")
  expect(blank.length).toEqual(0)
  expect(solid.length).toEqual(5)
})
it("should getStars with 4.3 avg and return 5 fill stars", () => {

  const t = create(<RatingStarIndicator average={4.3} count={31}  />)
  const blank = t.root.findAll((node) => node.type === (Icon as any) && node.props.name === "star-o")
  const solid = t.root.findAll((node) => node.type === (Icon as any) && node.props.name === "star")
  expect(blank.length).toEqual(1)
  expect(solid.length).toEqual(4)
})

it("should have maximum 5 stars", () => {
  let t = create(<RatingStarIndicator average={4.8} count={31}  />)
  let stars = t.root.findAll((node) => node.type === (Icon as any))
  expect(stars.length).toBeLessThanOrEqual(5)

  t = create(<RatingStarIndicator average={5} count={31}  />)
  stars = t.root.findAll((node) => node.type === (Icon as any))
  expect(stars.length).toBeLessThanOrEqual(5)

  t = create(<RatingStarIndicator average={4} count={31}  />)
  stars = t.root.findAll((node) => node.type === (Icon as any))
  expect(stars.length).toBeLessThanOrEqual(5)
})
