<commit_before>vk = null
cleanVk = require "inject!../src/index"

beforeEach ->
  vk = cleanVk()


describe "authUrl", ->

  it "should use passed app id, permissions, api version, and window style", ->
    url = vk.authUrl "12345", [ "audio", "photos" ], "5.10", "popup"

    url.should.equal "https://oauth.vk.com/authorize?client_id=12345&scope=audio,photos&" +
    "redirect_uri=https%3A%2F%2Foauth.vk.com%2Fblank.html&display=popup&v=5.10&response_type=token"


  it "should use sensible defaults", ->
    url = vk.authUrl "12345", [ "audio", "photos" ]

    url.should.equal "https://oauth.vk.com/authorize?client_id=12345&scope=audio,photos&" +
    "redirect_uri=https%3A%2F%2Foauth.vk.com%2Fblank.html&display=popup&v=5.50&response_type=token"


fakeMethod = "fake-method"
fakeUrl = "https://api.vk.com/method/fake-method"


describe "method", ->
  beforeEach ->
    vk.accessToken = "fake-token"

  it "calls @request and calls callback(error, data)", ( done ) ->
    fakeData =
      error: "fake error"
      response:
        foo: "bar"

    vk.request = ( url, params, callback ) ->
      url.should.equal fakeUrl
      params.should.deep.equal foo: "bar", access_token: "fake-token"

      callback fakeData

    vk.method fakeMethod, foo: "bar", ( error, response ) ->
      expect( error ).to.equal fakeData.error
      response.should.equal fakeData.response

      done()


  it "supports promises", ( done ) ->
    vk.request = ( url, params, callback ) ->
      callback response: "foo"

    vk.method fakeMethod, foo: "bar"
    .then ( response ) ->
      response.should.equal "foo"
      done()

    , ( error ) ->
      done "rejected!"


  it "rejects the promise when data.error exists", ( done ) ->
    vk.request = ( url, params, callback ) ->
      callback response: "foo", error: "exists"

    vk.method fakeMethod, foo: "bar"
    .then ( response ) ->
      done "resolved!"

    , ( error ) ->
      expect( error ).to.equal "exists"
      done()


describe "request", ->

  fakeXhr = null

  beforeEach ->
    fakeXhr = sinon.useFakeXMLHttpRequest()
    fakeXhr.requests = []
    fakeXhr.onCreate = ( xhr ) ->
      fakeXhr.requests.push xhr
<commit_msg>refactor(test/index): move variables with fake values to suites where they're used<commit_after>vk = null
cleanVk = require "inject!../src/index"

beforeEach ->
  vk = cleanVk()


describe "authUrl", ->

  it "should use passed app id, permissions, api version, and window style", ->
    url = vk.authUrl "12345", [ "audio", "photos" ], "5.10", "popup"

    url.should.equal "https://oauth.vk.com/authorize?client_id=12345&scope=audio,photos&" +
    "redirect_uri=https%3A%2F%2Foauth.vk.com%2Fblank.html&display=popup&v=5.10&response_type=token"


  it "should use sensible defaults", ->
    url = vk.authUrl "12345", [ "audio", "photos" ]

    url.should.equal "https://oauth.vk.com/authorize?client_id=12345&scope=audio,photos&" +
    "redirect_uri=https%3A%2F%2Foauth.vk.com%2Fblank.html&display=popup&v=5.50&response_type=token"


describe "method", ->

  fakeMethod = "fake-method"
  fakeUrl = "https://api.vk.com/method/fake-method"

  beforeEach ->
    vk.accessToken = "fake-token"

  it "calls @request and calls callback(error, data)", ( done ) ->
    fakeData =
      error: "fake error"
      response:
        foo: "bar"

    vk.request = ( url, params, callback ) ->
      url.should.equal fakeUrl
      params.should.deep.equal foo: "bar", access_token: "<PASSWORD>"

      callback fakeData

    vk.method fakeMethod, foo: "bar", ( error, response ) ->
      expect( error ).to.equal fakeData.error
      response.should.equal fakeData.response

      done()


  it "supports promises", ( done ) ->
    vk.request = ( url, params, callback ) ->
      callback response: "foo"

    vk.method fakeMethod, foo: "bar"
    .then ( response ) ->
      response.should.equal "foo"
      done()

    , ( error ) ->
      done "rejected!"


  it "rejects the promise when data.error exists", ( done ) ->
    vk.request = ( url, params, callback ) ->
      callback response: "foo", error: "exists"

    vk.method fakeMethod, foo: "bar"
    .then ( response ) ->
      done "resolved!"

    , ( error ) ->
      expect( error ).to.equal "exists"
      done()


describe "request", ->

  fakeUrl = "https://api.vk.com/method/fake-method"

  fakeXhr = null

  beforeEach ->
    fakeXhr = sinon.useFakeXMLHttpRequest()
    fakeXhr.requests = []
    fakeXhr.onCreate = ( xhr ) ->
      fakeXhr.requests.push xhr

{'fraction_non_alphanumeric': 0.15348733896046202, 'fraction_numerical': 0.016437139049311416, 'mean_word_length': 2.922473867595819, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 44, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2310833', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1485, 'n_words': 334}