import ExtraTimeSettings from '..'
import ExtraHoursMinutes from '../../ExtraHoursMinutes'
import Select from '@instructure/ui-forms/lib/components/Select'

describe('ExtraTimeSettings', () => {
  const sandbox = sinon.createSandbox()
  const onChangeSpy = sandbox.spy()

  const defaultProps = {
    onChange: onChangeSpy,
    timerSettings: {
      disableTimer: false,
      extraTimeEnabled: false,
      extraTimeInSeconds: null,
      extraTimeUnit: null,
      extraTimeValue: null,
      increaseTimeAllowance: false,
      timerMultiplierEnabled: false,
      timerMultiplierValue: null,
    },
    maxTimeInSeconds: 72000,
    maxMultiplier: 20,
    minMultiplier: 1
  }


  const testbed = createTestbed(ExtraTimeSettings, defaultProps)

  const changeToAnswerType = (testbed, newType) => {
    const selectComponent = testbed.findChildrenByType(Select)[0]
    selectComponent.props.onChange({ target: { value: newType } }, { value: newType })
  }

  const getLabelByText = (testbed, labelText) => {
    const labels = testbed.findChildrenByTagName('label')
    const label = labels.filter(el => {
      return testbed.getText(el).indexOf(labelText) > -1
    })

    return label[0]
  }

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

  it('renders a Select', () => {
    testbed.render()
    expect(getLabelByText(testbed, 'Time adjustments')).to.exist()
  })

  it('should render an extra time dialog', () => {
    testbed.render({ timerSettings: {...defaultProps.timerSettings, extraTimeEnabled: true }})
    expect(getLabelByText(testbed, 'Hours')).to.exist()
    expect(getLabelByText(testbed, 'Minutes')).to.exist()
  })

  it('should render a time multiplier dialog', () => {
    testbed.render({ timerSettings: {...defaultProps.timerSettings, timerMultiplierEnabled: true }})
    expect(getLabelByText(testbed, 'Multiplier')).to.exist()
  })

  it('should call back values from inputs', () => {
    testbed.render()
    changeToAnswerType(testbed, 'timerMultiplierEnabled')
    expect(onChangeSpy).to.have.been.calledWithMatch({ timerMultiplierEnabled: true })
  })

  it('should call back values from children', () => {
    testbed.render({ timerSettings: {...defaultProps.timerSettings, extraTimeEnabled: true }})
    const child = testbed.findChildByType(ExtraHoursMinutes)
    child.props.onChange(180)
    expect(onChangeSpy).to.have.been.calledWithMatch({ extraTimeInSeconds: 180 })
  })

  it('should be accessible', (done) => {
    testbed.render()
    testbed.checkA11yStandards(done)
  })

})
