
import { Map } from 'immutable'
import SessionItem from '../../../../../records/SessionItem'
import Stimulus from '../../../../../../common/records/Stimulus'
import ResultStimulus from '../index'
import styles from '../styles.css'

describe('ResultStimulus', () => {
  const renderItemStub = sinon.stub()

  const emptyStimulus = new Stimulus()
  const stimulus = new Stimulus({
    sourceUrl: 'google.com',
    instructions: 'my awesome instructions'
  })
  const sessionItem = new SessionItem()

  sinon.stub(sessionItem, 'getNestedEntries').returns(Map({
    1: new SessionItem({position: 1}),
    2: new SessionItem({position: 2}),
    3: new SessionItem({position: 3})
  }))

  const defaultProps = {
    renderItem: renderItemStub,
    stimulus: stimulus,
    sessionItem: sessionItem
  }

  const testbed = createTestbed(ResultStimulus, defaultProps)
  shouldRenderComponent(testbed, {}, ResultStimulus)

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

  it('should render the correct number of items', () => {
    testbed.render()
    const items = testbed.findChildrenByClass(styles.stimulusItem)
    expect(items.length).to.be.equal(3)
  })

  it('should call the renderItem prop when rendering', () => {
    testbed.render()
    expect(renderItemStub).to.be.called()
  })

  it('should render instructions if present', () => {
    testbed.render()
    const instructions = testbed.findChildrenByClass(styles.instructions)[0]
    expect(instructions).to.not.be.undefined()
  })

  it('should not render instructions if not present', () => {
    testbed.render({stimulus: emptyStimulus})
    const instructions = testbed.findChildrenByType(styles.instructions)[0]
    expect(instructions).to.be.undefined()
  })
})
