import { List, Map } from 'immutable'
import PeopleList from '../peopleList'
import { CAN_EDIT } from '@instructure/quiz-common/lib/constants'

describe('PeopleList', () => {
  const onPermissionChange = sinon.stub()
  const onPersonRemove = sinon.stub()

  const props = {
    onPermissionChange,
    onPersonRemove,
    peopleToRemove: [],
    people: List(
      [Map({
        id: '0',
        userUuid: '0',
        fullName: 'first fullName',
        avatar: 'http://avatars.com/avatar1',
        permission: 'canEdit'
      }),
      Map({
        id: '1',
        userUuid: '1',
        fullName: 'second fullName',
        avatar: 'http://avatars.com/avatar2',
        permission: 'readOnly'
      })]
    )
  }

  const testbed = createTestbed(PeopleList, props)

  afterEach(() => {
    onPermissionChange.resetHistory()
    onPersonRemove.resetHistory()
  })

  shouldRenderComponent(testbed, {}, PeopleList)

  it('should render Avatars for each person', () => {
    const shallowTestbed = testbed.renderShallowly()
    const avatars = shallowTestbed.find('Avatar')

    expect(avatars.length).to.equal(2)
    expect(avatars.at(0).props().src).to.equal(props.people.get(0).get('avatar'))
    expect(avatars.at(1).props().src).to.equal(props.people.get(1).get('avatar'))
    expect(avatars.at(0).props().name).to.equal(props.people.get(0).get('fullName'))
    expect(avatars.at(1).props().name).to.equal(props.people.get(1).get('fullName'))
  })

  it('should render the remove buttons', () => {
    const shallowTestbed = testbed.renderShallowly()
    const buttons = shallowTestbed.find('Button')

    expect(buttons.length).to.equal(2)
    expect(typeof buttons.at(0).props().onClick).to.equal('function')
    expect(typeof buttons.at(1).props().onClick).to.equal('function')
  })

  it('should NOT render the remove button when theres only one person', () => {
    const shallowTestbed = testbed.renderShallowly({
      people: List([Map({ userUuid: '0', fullName: 'first fullName', avatar: 'http://avatars.com/avatar1', permission: CAN_EDIT })])
    })
    const buttons = shallowTestbed.find('Button')

    expect(buttons.length).to.equal(0)
  })

  // re-enable this test once we're using the select for permissions
  xit('should render Selects for each person', () => {
    const shallowTestbed = testbed.renderShallowly()
    const selects = shallowTestbed.find('Select')

    expect(selects.length).to.equal(2)

    expect(selects.at(0).props().value).to.equal(props.people.get(0).get('permission'))
    expect(selects.at(1).props().value).to.equal(props.people.get(1).get('permission'))
  })

  it('should allow to remove people', () => {
    const shallowTestbed = testbed.renderShallowly()
    const removeButton = shallowTestbed.find('Button').at(0)

    removeButton.simulate('click')

    expect(onPersonRemove.firstCall.args).to.be.eql([props.people.get(0).get('id'), false])
  })

  it('should have different SR text for remove button if already removed', () => {
    const shallowTestbed = testbed.renderShallowly({
      peopleToRemove: ['0']
    })
    const buttons = shallowTestbed.find('Button')
    const buttonTexts = [
      buttons.at(0).prop('children').props.children,
      buttons.at(1).prop('children').props.children
    ]
    expect(buttonTexts).to.deep.equal([
      'Undo remove for first fullName',
      'Remove second fullName'
    ])
  })
})
