describe "SiteValidator", ->

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

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

  afterEach ->
    @server.restore()

  describe "url site types", -> 
    beforeEach ->
      @siteValidator = new JK.SiteValidator('url')
      @siteValidator.init()
      @url = 'http://www.jamkazam.com'

    it "displays validator widget", ->
      @siteValidator.data_input.val(@url)
      @siteValidator.siteIsValid()
      expect(@siteValidator.checkmark).toBeVisible()
  
    it "displays error for bad url", ->
      @url = @url.replace('http://', '')
      @siteValidator.data_input.val(@url)
      @siteValidator.didBlur()
      expect(@siteValidator.checkmark).toBeHidden()
  
    it "validates a good url", ->
      @server.respondWith("GET", '/api/data_validation?sitetype=url&data=' + encodeURIComponent(@url),
        [200, { "content-type": "application/json" }, JSON.stringify({"message": "Valid Site"})])
      sinon.spy()
      @siteValidator.data_input.val(@url)
      @siteValidator.didBlur()
      @server.respond()
      expect(@siteValidator.checkmark).toBeVisible()
  
    it "invalidates a bad url", ->
      @url += "xx"
      @server.respondWith("GET", '/api/data_validation?sitetype=url&data=' + encodeURIComponent(@url),
        [200, { "content-type": "application/json" }, JSON.stringify({"message": "Invalid Site"})])
      sinon.spy()
      @siteValidator.data_input.val(@url)
      @siteValidator.didBlur()
      @server.respond()
      expect(@siteValidator.checkmark).toBeHidden()
      expect(@siteValidator.site_status).toEqual(false)

    it "responds to static state query properly", -> 
      @siteValidator.siteIsValid()
      dfr = @siteValidator.state()
      expect(dfr.state()).toEqual("resolved")
      @siteValidator.siteIsInvalid()
      dfr = @siteValidator.state()
      expect(dfr.state()).toEqual("rejected")

    it "responds to deferred state query properly", -> 
      @server.respondWith("GET", '/api/data_validation?sitetype=url&data=' + encodeURIComponent(@url),
        [200, { "content-type": "application/json" }, JSON.stringify({"message": "Invalid Site"})])
      sinon.spy()
      @siteValidator.data_input.val(@url)
      expect(@siteValidator.site_status).toEqual(null)
      dfr = @siteValidator.state()
      expect(dfr.state()).toEqual("pending")
      @server.respond()
      expect(dfr.state()).toEqual("rejected")

  describe "twitter site types", -> 
    beforeEach ->
      @siteValidator = new JK.SiteValidator('twitter')
      @siteValidator.init()
      @uname = 'jamkazam'

    it "displays validator widget", ->
      @siteValidator.data_input.val(@uname)
      @siteValidator.siteIsValid()
      expect(@siteValidator.checkmark).toBeVisible()
  
    it "validates a good username", ->
      @server.respondWith("GET", '/api/data_validation?sitetype=twitter&data=' + encodeURIComponent(@uname),
        [200, { "content-type": "application/json" }, JSON.stringify({"message": "Valid Site"})])
      sinon.spy()
      @siteValidator.data_input.val(@uname)
      @siteValidator.didBlur()
      @server.respond()
      expect(@siteValidator.checkmark).toBeVisible()
  
    it "invalidates a bad url", ->
      @uname += "xx"
      @server.respondWith("GET", '/api/data_validation?sitetype=twitter&data=' + encodeURIComponent(@uname),
        [200, { "content-type": "application/json" }, JSON.stringify({"message": "Invalid Site"})])
      sinon.spy()
      @siteValidator.data_input.val(@uname)
      @siteValidator.didBlur()
      @server.respond()
      expect(@siteValidator.checkmark).toBeHidden()
      expect(@siteValidator.site_status).toEqual(false)

