import ReactDOM from 'react-dom'
import ReactTestUtils from 'react-dom/test-utils'
import { fromJS } from 'immutable'
import ToggleDetails from '@instructure/ui-toggle-details/lib/components/ToggleDetails'

import QuizSessionEvent from '../../../../common/records/QuizSessionEvent'
import SessionStartEvent from '../SessionStartEvent'

describe('SessionStartEvent', () => {
  // testrail test_id: C2954596, C2954597, C2954603, C2954603 , C2954602
  const event = new QuizSessionEvent(fromJS({
    createdAt: '2016-09-28T18:45:27.700Z|232982772',
    eventType: 'SESSION_STARTED_OR_RESUMED_EVENT',
    eventData: {
      browserSessionId: 'guid',
      clientTimestamp: '2016-09-28T18:45:25.300Z',
      userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2881.0 Safari/537.36' // eslint-disable-line max-len
    }
  }))

  const testbed = createTestbed(SessionStartEvent, {
    event,
    startTime: new Date('2016-09-28T18:45:24.700Z|232982772')
  })

  const toggle = (index = 0) => {
    const toggle = ReactDOM.findDOMNode(testbed.findChildrenByType(ToggleDetails)[index])
    ReactTestUtils.Simulate.click(toggle.querySelector('button'))
  }

  shouldRenderComponent(testbed, {}, SessionStartEvent)

  it('should render a SessionStartEvent', () => {
    testbed.render()
    const sessionStartEventComponent = testbed.findChildrenByType(SessionStartEvent)[0]
    expect(sessionStartEventComponent).to.not.be.undefined()
  })

  context('when dropdown is closed', () => {
    it('should show the dropdown toggle', () => {
      testbed.render()
      const toggleComponent = testbed.findChildrenByType(ToggleDetails)[0]
      expect(toggleComponent).to.not.be.undefined()
    })

    it('should show the dropdown toggle summary', () => {
      testbed.render()
      const text = testbed.getText()
      expect(text).to.include('System details captured')
    })

    it('should not show the browser information', () => {
      testbed.render()
      const text = testbed.getText()
      expect(text).to.not.include('Browser')
    })
    it('should not show the operating system information', () => {
      testbed.render()
      const text = testbed.getText()
      expect(text).to.not.include('Operating System')
    })
  })

  context('when dropdown is open', () => {
    it('should show the browser information', () => {
      testbed.render()
      toggle()
      const text = testbed.getText()
      expect(text).to.include('BrowserChrome 55.0.2881.0')
    })
    it('should show the operating system information', () => {
      testbed.render()
      toggle()
      const text = testbed.getText()
      expect(text).to.include('Operating SystemOS X 10.11.6')
    })
  })
})
