import { fromJS } from 'immutable'
import { TEMPORARY_BANK_ID } from '@instructure/quiz-common/lib/constants'
import { singleRelationCheck } from '../../../../tests/util/recordTests'
import QtiImport from '../QtiImport'

import Bank from '../Bank'

describe('Bank record', () => {
  it('converts string timestamps to Date', () => {
    const bank = new Bank({
      createdAt: '2017-02-12T17:36:00.000Z',
      updatedAt: '2017-03-13T17:36:00.000Z'
    })
    expect(bank.createdAt()).to.eql(new Date('2017-02-12T17:36:00.000Z'))
    expect(bank.updatedAt()).to.eql(new Date('2017-03-13T17:36:00.000Z'))
  })

  it('uses default id to indicate not loaded', () => {
    expect(new Bank().isLoaded()).to.eq(false)
    expect(new Bank({ id: 123 }).isLoaded()).to.eq(true)
  })

  describe('#isTemporary', () => {
    it('returns true if temporary id', () => {
      expect(new Bank({id: TEMPORARY_BANK_ID}).isTemporary()).to.be.true()
    })
  })

  describe('#canImport', () => {
    it('returns true if status is "waiting" and there are no bankEntries', () => {
      const bank = new Bank({status: 'waiting', entryCount: 0, bankEntries: new Set()})
      expect(bank.canImport()).to.be.true()
    })

    it('returns false if status is not "waiting"', () => {
      const bank = new Bank({status: 'importing', entryCount: 0, bankEntries: new Set()})
      expect(bank.canImport()).to.be.false()
    })

    it('returns false a bankEntry is associated to it', () => {
      const bank = new Bank({status: 'waiting', entryCount: 1, bankEntries: new Set('1')})
      expect(bank.canImport()).to.be.false()
    })
  })

  describe('#getLatestImport', () => {
    const bankId = '2'
    const laterImportData = {parentId: bankId, parentType: 'Bank', id: '2'}
    const exampleState = {
      qtiImports: {
        1: {parentId: bankId, parentType: 'Bank', id: '1'},
        2: laterImportData
      }
    }

    const options = {
      RelationRecordType: QtiImport,
      relationAttrs: laterImportData,
      recordAttributes: {id: bankId},
      necessaryState: fromJS(exampleState)
    }

    singleRelationCheck(Bank, 'getLatestImport', options)
  })
})
