describe "RecordingSource", ->

  $('<link/>',
    rel: 'stylesheet' 
    type: 'text/css' 
    href: '/assets/client/site_validator.css').appendTo 'head'
  fixture.preload("recordingSource.html")

  beforeEach ->
    @server = sinon.fakeServer.create();
    window.jamClient = sinon.stub()
    @fixtures = fixture.load("recordingSource.html", true)
    window.gon = {}
    window.gon.isNativeClient = true

  afterEach ->
    @server.restore()

  describe "youtube recording source", -> 

    beforeEach ->
      @recSource = new JK.RecordingSourceValidator('rec_youtube')
      sources = [{ url: 'https://www.youtube.com/watch?v=i_xFOmYxKYz', recording_id: 'i_xFOmYxKYz' }]
      @recSource.init(sources)
      @url = 'https://www.youtube.com/watch?v=i_xFOmYxKYz'

    it "displays validator widget", ->
      @recSource.data_input.val(@url)
      @recSource.siteIsValid()
      expect(@recSource.checkmark).toBeVisible()
  
    it "initializes sources properly", ->
      expect(@recSource.recording_sources.length).toEqual(1)
  
    it "adds source entries properly", ->
      @url = "https://www.youtube.com/watch?v=_wYtG7aQTHA"
      @server.respondWith("GET", '/api/data_validation?sitetype=rec_youtube&data=' + encodeURIComponent(@url),
        [200, { "content-type": "application/json" }, JSON.stringify({"message": "Valid Site", "data": @url, "recording_id" : "_wYtG7aQTHA"})])
      sinon.spy()
      @recSource.data_input.val(@url)
      @recSource.add_btn.click()
      @server.respond()
      expect(@recSource.state().state()).toEqual('resolved')
      expect(@recSource.containsRecordingUrl(@url)).toEqual(true)

    it "rejects duplicate source entries", ->
      rec_src_len = @recSource.recording_sources.length
      @server.respondWith("GET", '/api/data_validation?sitetype=url&data=' + encodeURIComponent(@url),
        [200, { "content-type": "application/json" }, JSON.stringify({"message": "Valid Site"})])
      sinon.spy()
      @recSource.data_input.val(@url)
      @recSource.add_btn.click()
      @server.respond()
      expect(@recSource.state().state()).toEqual('pending')
      expect(@recSource.recording_sources.length).toEqual(rec_src_len)

    it "removes sources", ->
      expect(@recSource.removeRecordingId('i_xFOmYxKYz')).toEqual(true)

