import * as eventTypes from '../eventTypes'

const {
  RESPONSE, ...otherEvents
} = eventTypes

const fakeData = {
  accountUuid: 'uuid',
  eventCreationToken: 'jwt_token',
  quizSessionId: '1'
}

const fakeResponseData = Object.assign({}, fakeData, {
  eventData: {
    itemId: 'item_1',
    position: 'position_1',
    userResponse: 'response_1'
  }
})

function testGenericEvent (type, data) {
  it('has the correct root-level keys', () => {
    expect(data).to.have.keys(['Data', 'PartitionKey'])
    expect(data.PartitionKey).to.eq(`${fakeData.quizSessionId}|${fakeData.accountUuid}`)
  })

  it('has the correct payload', () => {
    expect(JSON.parse(data.Data)).to.contain.all.keys([
      'created_at',
      'event_creation_token'
    ])
  })
}

describe('eventTypes', () => {
  Object.keys(otherEvents).forEach((type) => {
    const data = otherEvents[type](fakeData)
    describe(type, () => {
      testGenericEvent(type, data)
    })
  })

  describe(RESPONSE.name, () => {
    const data = RESPONSE(fakeResponseData)

    testGenericEvent(RESPONSE, data)

    it('modifies the payload correctly', () => {
      expect(JSON.parse(data.Data)).to.contain.all.keys(['event_data', 'item_id', 'position', 'user_response'])
    })
  })
})
