test/representer_test.rb in trailblazer-1.0.4 vs test/representer_test.rb in trailblazer-1.1.0.rc1
- old
+ new
@@ -121,6 +121,58 @@
# parse.
it do
res, op = JsonApiCreate.run(album: %{{"title":"Run For Cover"}})
op.contract.title.must_equal "Run For Cover"
end
+end
+
+class InternalRepresenterAPITest < MiniTest::Spec
+ Song = Struct.new(:id)
+
+ describe "#represented" do
+ class Show < Trailblazer::Operation
+ include Representer, Model
+ model Song, :create
+
+ representer do
+ property :class
+ end
+ end
+
+ it "uses #model as represented, per default" do
+ Show.present({}).to_json.must_equal '{"class":"InternalRepresenterAPITest::Song"}'
+ end
+
+ class ShowContract < Show
+ def represented
+ contract
+ end
+ end
+
+ it "can be overriden to use the contract" do
+ ShowContract.present({}).to_json.must_equal %{{"class":"#{ShowContract.contract_class}"}}
+ end
+ end
+
+ describe "#to_json" do
+ class OptionsShow < Trailblazer::Operation
+ include Representer
+
+ representer do
+ property :class
+ property :id
+ end
+
+ def to_json(*)
+ super(@params)
+ end
+
+ def model!(params)
+ Song.new(1)
+ end
+ end
+
+ it "allows to pass options to #to_json" do
+ OptionsShow.present(include: [:id]).to_json.must_equal '{"id":1}'
+ end
+ end
end
\ No newline at end of file