describe "MusicianSearchFilter", ->
  fixture.preload("musicianSearch.html")

  beforeEach =>
    @filterServer = sinon.fakeServer.create();
    window.jamClient = sinon.stub()
    @fixtures = fixture.load("musicianSearch.html", true)
    window.gon = {}
    window.gon.isNativeClient = true
    @searchMeta = {"per_page":10,"filter_keys":{"keys":["sort_order","instruments","interests","genres","concert_gigs","studio_sessions","skill_level","ages"],"multi":["instruments","genres","ages"],"single":["sort_order","interests","concert_gigs","studio_sessions","skill_level"]},"sort_order":{"keys":["latency","distance"],"map":{"latency":"Latency to Me","distance":"Distance to Me"}},"interests":{"keys":["any","virtual_band","traditional_band","paid_sessions","free_sessions","cowriting"],"map":{"any":"Any","virtual_band":"Virtual Band","traditional_band":"Traditional Band","paid_sessions":"Paid Sessions","free_sessions":"Free Sessions","cowriting":"Co-Writing"}},"ages":{"keys":[-1,10,20,30,40,50],"map":{"-1":"Any","10":"Teens","20":"20's","30":"30's","40":"40's","50":"50+"}}}
    window.gon.musician_search_meta = @searchMeta
    @defaultFilter = {sort_order: "latency", instruments: [], interests: "any", genres: [], concert_gigs: "-1", studio_sessions: "-1", skill_level: "-1", ages: [-1]}
    @genres = [{"description":"African","id":"african"},{"description":"Alternative Rock","id":"alternative rock"},{"description":"Ambient","id":"ambient"},{"description":"Americana","id":"americana"},{"description":"Asian","id":"asian"},{"description":"Avante-Garde","id":"avante-garde"},{"description":"Bluegrass","id":"bluegrass"},{"description":"Blues","id":"blues"},{"description":"Breakbeat","id":"breakbeat"},{"description":"Classical","id":"classical"},{"description":"Country","id":"country"},{"description":"Downtempo","id":"downtempo"},{"description":"Drum & Bass","id":"drum & bass"},{"description":"Electronic","id":"electronic"},{"description":"Folk","id":"folk"},{"description":"Hip Hop","id":"hip hop"},{"description":"House","id":"house"},{"description":"Industrial","id":"industrial"},{"description":"Jazz","id":"jazz"},{"description":"Latin","id":"latin"},{"description":"Metal","id":"metal"},{"description":"Other","id":"other"},{"description":"Pop","id":"pop"},{"description":"Psychedelic","id":"psychedelic"},{"description":"Punk","id":"punk"},{"description":"R&B","id":"r&b"},{"description":"Reggae","id":"reggae"},{"description":"Religious","id":"religious"},{"description":"Rock","id":"rock"},{"description":"Ska","id":"ska"},{"description":"Techno","id":"techno"},{"description":"Trance","id":"trance"}]
    @instruments = [{"id":"acoustic guitar","description":"Acoustic Guitar","popularity":3},{"id":"bass guitar","description":"Bass Guitar","popularity":3},{"id":"computer","description":"Computer","popularity":3},{"id":"drums","description":"Drums","popularity":3},{"id":"electric guitar","description":"Electric Guitar","popularity":3},{"id":"keyboard","description":"Keyboard","popularity":3},{"id":"voice","description":"Voice","popularity":3},{"id":"accordion","description":"Accordion","popularity":2},{"id":"banjo","description":"Banjo","popularity":2},{"id":"clarinet","description":"Clarinet","popularity":2},{"id":"flute","description":"Flute","popularity":2},{"id":"harmonica","description":"Harmonica","popularity":2},{"id":"piano","description":"Piano","popularity":2},{"id":"saxophone","description":"Saxophone","popularity":2},{"id":"trombone","description":"Trombone","popularity":2},{"id":"trumpet","description":"Trumpet","popularity":2},{"id":"upright bass","description":"Upright Bass","popularity":2},{"id":"violin","description":"Violin","popularity":2},{"id":"cello","description":"Cello","popularity":1},{"id":"euphonium","description":"Euphonium","popularity":1},{"id":"french horn","description":"French Horn","popularity":1},{"id":"mandolin","description":"Mandolin","popularity":1},{"id":"oboe","description":"Oboe","popularity":1},{"id":"other","description":"Other","popularity":1},{"id":"tuba","description":"Tuba","popularity":1},{"id":"ukulele","description":"Ukulele","popularity":1},{"id":"viola","description":"Viola","popularity":1}]

  afterEach =>
    @filterServer.restore()

  describe "displays empty search criteria", => 
    beforeEach =>
      @filterUI = new JK.MusicianSearchFilter()
      @filterUI.init()

    it "displays validator widget for default filter", =>
      @filterServer.respondWith("GET", "/api/search/musicians.json", [200, { "content-type": "application/json" }, JSON.stringify(@defaultFilter)])
      @filterServer.respondWith("GET", "/api/genres.json", [200, { "content-type": "application/json" }, JSON.stringify(@genres)])
      @filterServer.respondWith("GET", "/api/getInstruments.json", [200, { "content-type": "application/json" }, JSON.stringify(@instruments)])

      @filterUI.afterShow();
      @filterServer.respond()
      expect($('#search-filter-genres input').length).toEqual(@genres.length)
      expect($('#search-filter-instruments input').length).toEqual(@instruments.length)
      expect($('select[name="sort_order"]').val()).toEqual(@searchMeta.sort_order.keys[0])
      expect($('select[name="interests"]').val()).toEqual(@searchMeta.interests.keys[0])
      expect($('select[name="skill_level"]').val()).toEqual("-1")
      expect($('select[name="studio_sessions"]').val()).toEqual("-1")
      expect($('select[name="concert_gigs"]').val()).toEqual("-1")
      expect($('#search-filter-genres input[selected]').length).toEqual(0)
      expect($('#search-filter-ages input[selected]').length).toEqual(0)
      expect($('#search-filter-instruments input[selected]').length).toEqual(0)
      expect($('#search-filter-instruments select.proficiency_selector[value=2]').length).toEqual(0)

    xit "displays validator widget for edited filter", =>
      @defaultFilter = {sort_order: @searchMeta.sort_order.keys[1], instruments: [], interests: @searchMeta.interests.keys[1], genres: ['ambient','asian'], concert_gigs: "1", studio_sessions: "1", skill_level: "1", ages: [-1]}
      @filterServer.respondWith("GET", "/api/search/musicians.json", [200, { "content-type": "application/json" }, JSON.stringify(@defaultFilter)])
      sinon.spy()
      @filterServer.respondWith("GET", "/api/genres.json", [200, { "content-type": "application/json" }, JSON.stringify(@genres)])
      sinon.spy()
      @filterUI.afterShow();
      @filterServer.respond()
      expect($('select[name="sort_order"]').val()).toEqual(@searchMeta.sort_order.keys[1])
      expect($('select[name="interests"]').val()).toEqual(@searchMeta.interests.keys[1])
      expect($('select[name="skill_level"]').val()).toEqual(@defaultFilter.skill_level)
      expect($('select[name="studio_sessions"]').val()).toEqual(@defaultFilter.studio_sessions)
      expect($('select[name="concert_gigs"]').val()).toEqual(@defaultFilter.concert_gigs)
      expect($('#search-filter-genres input[checked]').length).toEqual(@defaultFilter.genres.length)

