import { fromJS } from 'immutable'

import { callProgress, progressValue } from '../index'

import {
  CREATE_QTI_IMPORT,
  FILE_UPLOAD_URL_CALL,
  FILE_UPLOAD_CALL
} from '@instructure/quiz-common/lib/constants'

const calls = (call1 = 'success', call2 = 'success', call3 = 'success') => {
  return fromJS({
    [CREATE_QTI_IMPORT]: { requestStatus: call1 },
    [FILE_UPLOAD_URL_CALL]: { requestStatus: call2 },
    [FILE_UPLOAD_CALL]: { requestStatus: call3 }
  })
}
const callsToCheck = [CREATE_QTI_IMPORT, FILE_UPLOAD_CALL, FILE_UPLOAD_URL_CALL]

describe('.callProgress', () => {
  it('returns 1 if all calls are made', () => {
    const callRatio = callProgress(callsToCheck, calls())
    expect(callRatio).to.eq(1)
  })

  it('returns 0 if all calls are not made', () => {
    const callRatio = callProgress(callsToCheck, calls(false, false, false))
    expect(callRatio).to.eq(0, 0.1)
  })

  it('returns 0 if all calls are not made', () => {
    const callRatio = callProgress(callsToCheck, calls(false, false, false))
    expect(callRatio).to.eq(0, 0.1)
  })

  it('returns a non-zero number if calls are not made', () => {
    const callRatio = callProgress(callsToCheck, calls(false))
    expect(callRatio).to.be.closeTo(0.6, 0.1)
  })
})

describe('.progressValue', () => {
  it('returns the value of callProgress * 50 when progressNumber is 0', () => {
    const pvNum = progressValue(0, calls(false), callsToCheck)
    expect(pvNum).to.be.closeTo(0.6 * 50, 5)
  })

  it('returns the value of 1 + qtiImport.progressNumber * 50 when progressNumber is not 0', () => {
    const pvNum = progressValue(0.3, calls(), callsToCheck)
    expect(pvNum).to.be.closeTo(1.3 * 50, 0.9)
  })
})
