import React from 'react'
import { shallow } from 'enzyme'
import { PaginationButton } from '@instructure/ui-pagination/lib/components/Pagination'
import Paginator from '../index'

describe('Paginator', () => {
  const sandbox = sinon.createSandbox()
  const props = {
    currentPage: 2,
    onPageChange: sandbox.spy(),
    totalPages: 4
  }
  let wrapper

  beforeEach(() => {
    wrapper = shallow(<Paginator {...props}>PAGE</Paginator>, {disableLifecycleMethods: true})
  })

  afterEach(() => {
    sandbox.resetHistory()
  })

  it('renders children', () => {
    expect(wrapper.text()).to.include('PAGE')
  })

  it('renders pagination controls', () => {
    const buttons = wrapper.find(PaginationButton)
    expect(buttons.map((button) => button.children().text())).to.deep.equal(['1', '2', '3', '4'])
  })

  it('highlights the current page', () => {
    const selected = wrapper.find(PaginationButton).find({ current: true })
    expect(selected.children().text()).to.equal('2')
  })

  it('calls onPageChange when a pagination control is clicked', () => {
    wrapper.find(PaginationButton).at(3).simulate('click')
    expect(props.onPageChange).to.have.been.calledWith(4)
  })
})
