Skip to content

Commit

Permalink
Tests serializer for association
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Aug 10, 2017
1 parent 40ba409 commit a3a710a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
6 changes: 2 additions & 4 deletions app/models/dialog_field_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ def initialize(resource_action_serializer = ResourceActionSerializer.new)

def serialize(dialog_field, all_attributes = false)
serialized_resource_action = @resource_action_serializer.serialize(dialog_field.resource_action)

extra_attributes = {
"resource_action" => serialized_resource_action,
"trigger_associations" => dialog_field.trigger_associations.map(&:name)
"resource_action" => serialized_resource_action,
"dialog_field_responders" => dialog_field.dialog_field_responders.map(&:name)
}

if dialog_field.dynamic?
Expand All @@ -26,7 +25,6 @@ def serialize(dialog_field, all_attributes = false)
category = Category.find_by(:id => dialog_field.category)
dialog_field.options.merge!(:category_name => category.name, :category_description => category.description)
end

included_attributes(dialog_field.as_json(:methods => [:type, :values]), all_attributes).merge(extra_attributes)
end
end
30 changes: 23 additions & 7 deletions spec/models/dialog_field_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
let(:dialog_field_serializer) { described_class.new(resource_action_serializer) }

describe "#serialize" do
let(:dialog_field) { DialogFieldTextBox.new(expected_serialized_values.merge(:resource_action => resource_action)) }
let(:dialog_field) { DialogFieldTextBox.new(expected_serialized_values.merge(:resource_action => resource_action, :dialog_field_responders => dialog_field_responders)) }
let(:type) { "DialogFieldTextBox" }
let(:resource_action) { ResourceAction.new }
let(:dialog_field_responders) { [] }
let(:options) { {"options" => true} }
let(:expected_serialized_values) do
{
Expand Down Expand Up @@ -59,8 +60,9 @@
it 'serializes the dialog_field with the correct attributes' do
expect(dialog_field_serializer.serialize(dialog_field, all_attributes))
.to eq(expected_serialized_values.merge(
"resource_action" => "serialized resource action",
"values" => "dynamic values"
"resource_action" => "serialized resource action",
"values" => "dynamic values",
"dialog_field_responders" => dialog_field_responders
))
end
end
Expand Down Expand Up @@ -88,7 +90,8 @@
it "serializes the dialog_field with the correct values" do
expect(dialog_field_serializer.serialize(dialog_field, all_attributes))
.to eq(expected_serialized_values.merge(
"resource_action" => "serialized resource action"
"resource_action" => "serialized resource action",
"dialog_field_responders" => dialog_field_responders
))
end
end
Expand All @@ -103,12 +106,24 @@
'resource_action' => 'serialized resource action',
))
end

context 'with associations' do
let(:dialog_field_responders) { [FactoryGirl.build(:dialog_field_text_box)] }

it 'serializes the dialog_field with all attributes and non_empty associations' do
expect(dialog_field_serializer.serialize(dialog_field, dialog_field_responders))
.to include(expected_serialized_values.merge(
"resource_action" => "serialized resource action",
"dialog_field_responders" => ["Dialog Field"]
))
end
end
end
end

context "when the dialog_field is a tag control type" do
let(:dialog_field) do
DialogFieldTagControl.new(expected_serialized_values.merge(:resource_action => resource_action))
DialogFieldTagControl.new(expected_serialized_values.merge(:resource_action => resource_action, :dialog_field_responders => dialog_field_responders))
end

let(:type) { "DialogFieldTagControl" }
Expand All @@ -124,8 +139,9 @@
it "serializes the category name and description" do
expect(dialog_field_serializer.serialize(dialog_field))
.to eq(expected_serialized_values.merge(
"resource_action" => "serialized resource action",
"options" => {
"resource_action" => "serialized resource action",
"dialog_field_responders" => dialog_field_responders,
"options" => {
:category_id => "123",
:category_name => "best category ever",
:category_description => "best category ever"
Expand Down

0 comments on commit a3a710a

Please sign in to comment.