<commit_before>require 'spec_helper'

module Spree
  describe Api::StatesController, type: :controller do
    render_views

    let!(:state) { create(:state, name: "Victoria") }
    let(:attributes) { [:id, :name, :abbr, :country_id] }

    before do
      stub_authentication!
    end

    it "gets all states" do
      api_get :index
      expect(json_response["states"].first).to have_attributes(attributes)
      expect(json_response['states'].first['name']).to eq(state.name)
    end

    it "gets all the states for a particular country" do
      api_get :index, country_id: state.country.id
      expect(json_response["states"].first).to have_attributes(attributes)
      expect(json_response['states'].first['name']).to eq(state.name)
    end

    context "pagination" do
      before do
        expect(State).to receive(:accessible_by).and_return(@scope = double)
        allow(@scope).to receive_message_chain(:ransack, :result, :includes, :order).and_return(@scope)
      end

      it "does not paginate states results when asked not to do so" do
        expect(@scope).not_to receive(:page)
        expect(@scope).not_to receive(:per)
        api_get :index
      end

      it "paginates when page parameter is passed through" do
        expect(@scope).to receive(:page).with(1).and_return(@scope)
        expect(@scope).to receive(:per).with(nil)
        api_get :index, page: 1
      end

      it "paginates when per_page parameter is passed through" do
        expect(@scope).to receive(:page).with(nil).and_return(@scope)
        expect(@scope).to receive(:per).with(25)
        api_get :index, per_page: 25
      end
    end

    context "with two states" do
      before { create(:state, name: "New South Wales") }

      it "gets all states for a country" do
        country = create(:country, states_required: true)
        state.country = country
        state.save

        api_get :index, country_id: country.id
        expect(json_response["states"].first).to have_attributes(attributes)
        expect(json_response["states"].count).to eq(1)
        json_response["states_required"] = true
      end

      it "can view all states" do
        api_get :index
        expect(json_response["states"].first).to have_attributes(attributes)
      end<commit_msg>Avoid stubbing in test of pagination<commit_after>require 'spec_helper'

module Spree
  describe Api::StatesController, type: :controller do
    render_views

    let!(:state) { create(:state, name: "Victoria") }
    let(:attributes) { [:id, :name, :abbr, :country_id] }

    before do
      stub_authentication!
    end

    it "gets all states" do
      api_get :index
      expect(json_response["states"].first).to have_attributes(attributes)
      expect(json_response['states'].first['name']).to eq(state.name)
    end

    it "gets all the states for a particular country" do
      api_get :index, country_id: state.country.id
      expect(json_response["states"].first).to have_attributes(attributes)
      expect(json_response['states'].first['name']).to eq(state.name)
    end

    context "pagination" do
      it "can select the next page and control page size" do
        create(:state)
        api_get :index, page: 2, per_page: 1

        expect(json_response["states"].size).to eq(1)
        expect(json_response["pages"]).to eq(2)
        expect(json_response["current_page"]).to eq(2)
        expect(json_response["count"]).to eq(1)
      end
    end

    context "with two states" do
      before { create(:state, name: "New South Wales") }

      it "gets all states for a country" do
        country = create(:country, states_required: true)
        state.country = country
        state.save

        api_get :index, country_id: country.id
        expect(json_response["states"].first).to have_attributes(attributes)
        expect(json_response["states"].count).to eq(1)
        json_response["states_required"] = true
      end

      it "can view all states" do
        api_get :index
        expect(json_response["states"].first).to have_attributes(attributes)
      end
{'fraction_non_alphanumeric': 0.11718558470213288, 'fraction_numerical': 0.003432213777886737, 'mean_word_length': 3.08, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '2648983', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1181, 'n_words': 310}