import store from '../../../reduxStore'
import formatMessage from '@instructure/quiz-i18n/lib/format-message'

import { SDKApp } from '../SDKApp'
import ConfirmationModal from '../ConfirmationModal'
import { setAPIEndpoint, setUserToken } from '../../actions/config'
import { setContextUuid, setOutcomesEndpoint, setOutcomesToken } from '../../actions/outcomes'

describe('SDKApp', () => {
  const alert = document.createElement('div')
  alert.setAttribute('role', 'alert')
  alert.setAttribute('aria-live', 'assertive')

  const sandbox = sinon.createSandbox()
  sandbox.stub(store, 'dispatch')

  beforeEach(() => {
    document.body.appendChild(alert)
  })

  afterEach(() => {
    document.body.removeChild(alert)
    sandbox.resetHistory()
  })

  const defaultProps = {
    apiEndpoint: ''
  }

  const testbed = createTestbed(SDKApp, defaultProps)

  shouldRenderComponent(testbed, {}, SDKApp)

  shouldRenderComponent(testbed, {}, ConfirmationModal)

  it('should dispatch actions for api endpoint change in props', () => {
    testbed.render({ apiEndpoint: 'foo' })
    expect(store.dispatch.getCall(0).args[0]).to.deep.include(setAPIEndpoint('foo'))

    testbed.render({ apiEndpoint: 'bar' })
    expect(store.dispatch).to.have.been.calledWith(setAPIEndpoint('bar'))
  })

  it('should dispatch actions for userToken change in props', () => {
    testbed.render({ apiEndpoint: 'stuff', userToken: 'foo' })
    expect(store.dispatch.getCall(0).args[0]).to.deep.include(setUserToken('foo'))

    testbed.render({ apiEndpoint: 'stuff', userToken: 'bar' })
    expect(store.dispatch).to.have.been.calledWith(setUserToken('bar'))
  })

  it('should dispatch actions for outcomes endpoint change in props', () => {
    testbed.render({ outcomesEndpoint: 'foo' })
    expect(store.dispatch.getCall(0).args[0]).to.deep.include(setOutcomesEndpoint('foo'))

    testbed.render({ outcomesEndpoint: 'bar' })
    expect(store.dispatch).to.have.been.calledWith(setOutcomesEndpoint('bar'))
  })

  it('should dispatch actions for outcomes token change in props', () => {
    testbed.render({ outcomesToken: 'foo' })
    expect(store.dispatch.getCall(0).args[0]).to.deep.include(setOutcomesToken('foo'))

    testbed.render({ outcomesToken: 'bar' })
    expect(store.dispatch).to.have.been.calledWith(setOutcomesToken('bar'))
  })

  it('should dispatch actions for context uuid change in props', () => {
    testbed.render({ contextUuid: 'foo' })
    expect(store.dispatch.getCall(0).args[0]).to.deep.include(setContextUuid('foo'))

    testbed.render({ contextUuid: 'bar' })
    expect(store.dispatch).to.have.been.calledWith(setContextUuid('bar'))
  })

  it('should support Etc/GMT timezones', () => {
    testbed.render({ timeZone: 'Etc/GMT+7' })
    // Unpatched, this will raise a RangeError
    // - Expected Area/Location(/Location)* for time zone, got Etc/GMT+7
    expect(
      formatMessage.date(new Date(), 'medium')
    ).to.not.be.empty()
  })

  it('should default to UTC for unrecognized time zones', () => {
    testbed.render({ timeZone: 'fakeZone' })
    expect(
      formatMessage.date(new Date(), 'medium')
    ).to.not.be.empty()
  })
})
