require 'spec_helper'

describe Api::Fixxpert::SubtradesController do

  # GET index
  #----------------------------------------------------------------------

  describe "GET index.json" do

    let(:user) { create (:fixxpert) }

    before(:each) do
      %w(foo bar wadus).each do |name|
        subtrade = create(:subtrade, name: name)
        user.subtrades << subtrade
      end
    end

    context 'without a query' do
      it "should return all the subtrades" do
        login_user user
        get :index, format: :json
        records = Subtrade.all
        response.should be_success
        response.body.should eq records.map{|s| { id: s.id, name: s.name } }.to_json
      end
    end

    context 'witht a query' do
      it "should return the found subtrades" do
        login_user user
        get :index, q: 'wadus', format: :json
        records = Subtrade.where(name: 'wadus').all
        response.should be_success
        response.body.should eq records.map{|s| { id: s.id, name: s.name }}.to_json
      end
    end

  end

  # GET autocomplete
  #----------------------------------------------------------------------

  describe "GET autocomplete.json" do

    let(:user) { create (:fixxpert) }

    before(:each) do
      %w(foo bar wadus).each do |name|
        subtrade = create(:subtrade, name: name)
        user.subtrades << subtrade
      end
    end

    context 'without a query' do
      it "should return all the subtrades" do
        login_user user
        get :autocomplete, format: :json
        records = Subtrade.all
        response.should be_success
        response.body.should eq records.collect{|s| s.name }.to_json
      end
    end

    context 'witht a query' do
      it "should return the found subtrades" do
        login_user user
        get :autocomplete, term: 'wadus', format: :json
        records = Subtrade.where(name: 'wadus').all
        response.should be_success
        response.body.should eq records.map{|s| s.name }.to_json
      end
    end

  end

  # GET ids
  #----------------------------------------------------------------------

  describe "GET ids.json" do

    let(:user) { create (:fixxpert) }

    before(:each) do
      %w(foo bar wadus).each do |name|
        subtrade = create(:subtrade, name: name)
        user.subtrades << subtrade
      end
    end

    context 'without a query' do
      it "should return all the subtrades ids" do
        login_user user
        get :ids, format: :json
        records = Subtrade.all
        response.should be_success
        response.body.should eq records.map{|s| s.id }.to_json
      end
    end

    context 'witht a query' do
      it "should return the found subtrades ids" do
        login_user user
        get :ids, q: 'wadus', format: :json
        records = Subtrade.where(name: 'wadus').all
        response.should be_success
        response.body.should eq records.map{|s| s.id }.to_json
      end
    end

  end
end
