import { calculateRatingResult, calculatePercentPerBar } from "../ReviewSummary"

describe("ReviewSummary test", () => {

  it("should calculate bar result correctly", () => {

    const mockData = {
      ratingInfo: [
        {
          rating: 5,
          count: 5
        },
        {
          rating: 4,
          count: 19
        },
        {
          rating: 3,
          count: 3
        },
        {
          rating: 2,
          count: 0
        },
        {
          rating: 1,
          count: 0
        }
      ]
    }

    expect(calculateRatingResult(mockData.ratingInfo)).toEqual(4.07)

  })

  it("should not error if array is empty", () => {
    expect(calculateRatingResult([])).toEqual(0)
  })

  it("should calculate bar indicator level correctly", () => {
    const mockData = {
      ratingInfo: [
        {
          rating: 5,
          count: 5
        },
        {
          rating: 4,
          count: 20
        },
        {
          rating: 3,
          count: 5
        },
        {
          rating: 2,
          count: 0
        },
        {
          rating: 1,
          count: 0
        }
      ]
    }

    expect(Math.round(calculatePercentPerBar(mockData.ratingInfo).reduce((prev, current) => {
      return prev + (current.indicatorLevel)
    }, 0))).toEqual(1)
  })

})
