spec/model/parse_spec.rb in her-0.10.0 vs spec/model/parse_spec.rb in her-0.10.1

- old
+ new

@@ -22,10 +22,25 @@ parse_root_in_json true custom_post :admins end end + it "inherits attributes from parent class" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + end + + expect(Foo::ChildUser).to be_include_root_in_json + end + + it "allows `include_root_in_json` to be set to `false` on a child model" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + include_root_in_json false + end + + expect(Foo::ChildUser).to_not be_include_root_in_json + end + it "wraps params in the element name in `to_params`" do @new_user = Foo::User.new(fullname: "Tobias Fünke") expect(@new_user.to_params).to eq(user: { fullname: "Tobias Fünke" }) end @@ -33,10 +48,33 @@ @new_user = Foo::User.admins(fullname: "Tobias Fünke") expect(@new_user.fullname).to eq("Tobias Fünke") end end + context "to false" do + before do + spawn_model "Foo::User" do + include_root_in_json false + end + end + + it "inherits attributes from parent class" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + end + + expect(Foo::ChildUser).to_not be_include_root_in_json + end + + it "allows `include_root_in_json` to be set to `true` on a child model" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + include_root_in_json true + end + + expect(Foo::ChildUser).to be_include_root_in_json + end + end + context "to a symbol" do before do spawn_model "Foo::User" do include_root_in_json :person parse_root_in_json :person @@ -62,10 +100,58 @@ expect(@new_user.to_params).to eq(user: { fullname: "Tobias Fünke" }) end end end + context "when `request_new_object_on_build` is set" do + context "to true" do + before do + spawn_model "Foo::User" do + request_new_object_on_build true + end + end + + it "inherits attributes from parent class" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + end + + expect(Foo::ChildUser).to be_request_new_object_on_build + end + + it "allows `request_new_object_on_build` to be set to `false` on a child model" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + request_new_object_on_build false + end + + expect(Foo::ChildUser).to_not be_request_new_object_on_build + end + end + + context "to false" do + before do + spawn_model "Foo::User" do + request_new_object_on_build false + end + end + + it "inherits attributes from parent class" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + end + + expect(Foo::ChildUser).to_not be_request_new_object_on_build + end + + it "allows `request_new_object_on_build` to be set to `true` on a child model" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + request_new_object_on_build true + end + + expect(Foo::ChildUser).to be_request_new_object_on_build + end + end + end + context "when parse_root_in_json is set" do before do Her::API.setup url: "https://api.example.com" do |builder| builder.use Her::Middleware::FirstLevelParseJSON builder.use Faraday::Request::UrlEncoded @@ -86,10 +172,25 @@ parse_root_in_json true custom_get :admins end end + it "inherits attributes from parent class" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + end + + expect(Foo::ChildUser).to be_parse_root_in_json + end + + it "allows `parse_root_in_json` to be set to `false` on a child model" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + parse_root_in_json false + end + + expect(Foo::ChildUser).to_not be_parse_root_in_json + end + it "parse the data from the JSON root element after .create" do @new_user = Foo::User.create(fullname: "Lindsay Fünke") expect(@new_user.fullname).to eq("Lindsay Fünke") end @@ -114,10 +215,33 @@ @user.save expect(@user.fullname).to eq("Tobias Fünke Jr.") end end + context "to false" do + before do + spawn_model "Foo::User" do + parse_root_in_json false + end + end + + it "inherits attributes from parent class" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + end + + expect(Foo::ChildUser).to_not be_parse_root_in_json + end + + it "allows `parse_root_in_json` to be set to `true` on a child model" do + spawn_model "Foo::ChildUser", super_class: Foo::User do + parse_root_in_json true + end + + expect(Foo::ChildUser).to be_parse_root_in_json + end + end + context "to a symbol" do before do Her::API.default_api.connection.adapter :test do |stub| stub.post("/users") { [200, {}, { person: { id: 1, fullname: "Lindsay Fünke" } }.to_json] } end @@ -338,8 +462,35 @@ it "only sends the attributes that were modified" do user = Foo::User.find 1 user.first_name = "Someone" expect(user.to_params).to eql(user: { first_name: "Someone" }) + end + end + + context 'when passed a non-Her ActiveModel instance' do + before do + klass = Class.new do + include ActiveModel::Serialization + + def attributes + { 'name' => nil } + end + + def name + 'foo' + end + end + + @model = klass.new + + Her::API.setup + spawn_model 'Foo::User' + end + + it 'serializes the instance in `to_params`' do + attributes = { model: @model } + user = Foo::User.new(attributes) + expect(user.to_params).to eq(model: { name: 'foo' }) end end end