import BankEntry from '../index'

import { fromJS } from 'immutable'
import { withTestFeatures } from '../../../../common/util/featureCheck'
import { setFakeStore, unsetFakeStore } from '../../../../reduxStore'

describe('connected BankEntry', () => {
  const connectedTestbed = createTestbed(
    withContext(BankEntry, {
      banks: { 1: { title: 'Very distinctive name', updatedAt: Date.now() } },
      bankEntries: {
        5: { entry: { id: '100' }, entryType: 'Item' },
        6: { entry: { id: '101' }, entryType: 'Stimulus' }
      },
      items: {
        100: { id: '100' }
      },
      stimuli: {
        101: { id: '101' }
      },
      tagAssociations: { Item: { 100: fromJS([{ id: 1, tag_value: 'waldo' }]) } }
    }), {
      bankId: '1',
      bankEntryId: '5',
      navigateToBanks: () => {},
      navigateToBank: () => {}
    })

  const newItemTestbed = createTestbed(
    withContext(BankEntry, {
      modifications: {
        bankEntries: {
          TEMPORARY_BANK_ENTRY: {
            entry: 'TEMPORARY_ITEM',
            entryType: 'Item'
          }
        }
      },
      banks: { 1: { title: 'Very distinctive name', updatedAt: Date.now() } }
    }), {
      bankId: '1',
      bankEntryId: 'new',
      navigateToBanks: () => {},
      navigateToBank: () => {}
    })

  const fakeState = fromJS({
    items: { 100: { id: '100' } },
    stimuli: { 101: { id: '101' } }
  })

  beforeEach(() => setFakeStore(fakeState))

  afterEach(() => unsetFakeStore())

  const fetchStub = sinon.stub()
  useFakeFetching({dispatchStub: sinon.stub(),
    getStateStub: sinon.stub(),
    fetchStub})

  it('fetches records on mount', () => {
    connectedTestbed.render()
    expect(fetchStub.calledWith('/api/banks/1/bank_entries/5')).to.be.true()
  })

  it('finds bank in store', () => {
    connectedTestbed.render()
    expect(connectedTestbed.getText()).to.include('Very distinctive name')
  })

  it('renders item', () => {
    connectedTestbed.render()
    expect(connectedTestbed.getText()).to.include('Item 100')
  })

  it('renders new item', () => {
    newItemTestbed.render()
  })

  describe('with tagging', () => {
    withTestFeatures(['item_bank_tagging'])

    it('renders tag', () => {
      connectedTestbed.render({ bankEntryId: '5' })
      expect(connectedTestbed.getText()).to.include('waldo')
    })
  })
})
