import mediaDeviceReducer, { selectCurrentMediaDeviceId, setSelectedMediaDevice } from './mediaDeviceSlice'
import { mockRootState } from '../../testUtils'

test('sets the selected media device for audio inputs', () => {
  const state = {
    selectedAudioDeviceId: null,
    selectedVideoDeviceId: null,
  }

  const newState = mediaDeviceReducer(
    state,
    setSelectedMediaDevice({ kind: 'audioinput', deviceId: 'testAudioDeviceId' })
  )

  expect(newState).toStrictEqual({
    selectedAudioDeviceId: 'testAudioDeviceId',
    selectedVideoDeviceId: null,
  })
})

test('sets the selected media device for video inputs', () => {
  const state = {
    selectedAudioDeviceId: null,
    selectedVideoDeviceId: null,
  }

  const newState = mediaDeviceReducer(
    state,
    setSelectedMediaDevice({ kind: 'videoinput', deviceId: 'testVideoDeviceId' })
  )

  expect(newState).toStrictEqual({
    selectedAudioDeviceId: null,
    selectedVideoDeviceId: 'testVideoDeviceId',
  })
})

test('selectCurrentMediaDeviceId selects the current audio device id', () => {
  const state = {
    selectedAudioDeviceId: 'testAudioDeviceId',
    selectedVideoDeviceId: null,
  }

  const selectedAudioDeviceId = selectCurrentMediaDeviceId({ ...mockRootState, mediaDevice: state }, 'audioinput')

  expect(selectedAudioDeviceId).toEqual('testAudioDeviceId')
})

test('selectCurrentMediaDeviceId selects the current video device id', () => {
  const state = {
    selectedAudioDeviceId: null,
    selectedVideoDeviceId: 'testVideoDeviceId',
  }

  const selectedVideoDeviceId = selectCurrentMediaDeviceId({ ...mockRootState, mediaDevice: state }, 'videoinput')

  expect(selectedVideoDeviceId).toEqual('testVideoDeviceId')
})
