Sha256: 78e194308beffcaf0bd4962b407acd6c94356efe71488a31088a5836ecdd95b9

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

describe Grape::ExtraValidators::EndWith do
  module ValidationsSpec
    module EndWithValidatorSpec
      class API < Grape::API
        default_format :json

        params do
          optional :text, type: String, end_with: "JPY"
        end
        post "/not-array" do
          body false
        end

        params do
          optional :text, type: String, end_with: ["JPY", "USD"]
        end
        post "/array" do
          body false
        end
      end
    end
  end

  def app
    ValidationsSpec::EndWithValidatorSpec::API
  end

  let(:params) { { text: text } }
  let(:text) { nil }
  subject { last_response.status }

  describe "Specify a not array" do
    before { post "/not-array", params }

    context "when the string ends with the specific string" do
      let(:text) { "100JPY" }

      it { is_expected.to eq(204) }
    end

    context "when the string does not end with the specific string" do
      let(:text) { "100¥" }

      it { is_expected.to eq(400) }
    end

    context "when the parameter is nil" do
      it { is_expected.to eq(204) }
    end
  end

  describe "Specify an array" do
    before { |example| post "/array", params unless example.metadata[:skip_before_request_call] }

    context "when the string ends with a string one of the specific strings" do
      it "should not return any errors", skip_before_request_call: true do
        ["100JPY", "100USD"].each do |text|
          post "/array", { text: text }
          expect(last_response.status).to eq(204)
        end
      end
    end

    context "when the string does not end with a string one of the specific strings" do
      let(:text) { "100¥" }

      it { is_expected.to eq(400) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grape-extra_validators-2.0.0 spec/grape/extra_validators/end_with_spec.rb
grape-extra_validators-1.0.0 spec/grape/extra_validators/end_with_spec.rb