import { fromJS } from 'immutable'

import createTestbed from '../../../../../../../tests/util/react-testbed'
import withContext from '../../../../../../../tests/util/withContext'
import Stimulus from '../../../../../records/Stimulus'
import StimulusEditInfo from '../presenter'

describe('StimulusEditInfo', () => {
  const modifyStub = sinon.stub()

  const stimulus = new Stimulus({
    id: 55,
    title: 'Title',
    instructions: 'Instructions',
    body: 'Body',
    sourceUrl: 'Source URL'
  })

  const defaultProps = {
    stimulus,
    workingStimulus: stimulus,
    fileUpload: sinon.stub(),
    modifyStimulus: modifyStub,
    progressValue: 0,
    setUploadStatus: sinon.stub()
  }

  const testbed = createTestbed(withContext(StimulusEditInfo), defaultProps)

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

  it('renders an error when there is a size error', () => {
    sinon.stub(stimulus, 'getErrors').returns(fromJS({ onSelf: ['some error'] }))
    testbed.render({ errorsShowing: true })
    expect(testbed.getText()).to.include('some error')
  })

  describe('modifications', () => {
    const changeField = (inputNum, field) => {
      testbed.render()
      const inputs = testbed.findChildrenByTagName('input')
      testbed.change(inputs[inputNum], 'hello')
      expect(modifyStub.firstCall.args[0].get(field)).to.eq('hello')
    }

    it('changes the title', () => {
      changeField(0, 'title')
    })

    it('changes the instructions', () => {
      changeField(1, 'instructions')
    })

    it('changes the source URL', () => {
      changeField(2, 'sourceUrl')
    })
  })

  describe('a11y tests', () => {
    it('should meet a11y standards', (done) => {
      testbed.render()
      // ignoring button-name because of the textcolor tinymce plugin
      testbed.checkA11yStandards(done, {
        ignores: [
          'aria-allowed-role', // TODO: remove this when canvas-rce is fixed
          'button-name',
          'label',
          'label-title-only'
        ]
      })
    })
  })
})
