Last active
August 29, 2015 14:22
-
-
Save aaronmcadam/8b243db769a3be33244e to your computer and use it in GitHub Desktop.
Testing PORO view models with view_context injected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IconTag | |
def initialize(helper:, icon:, project_id:, task_type:, tenant:) | |
@helper = helper | |
@icon = icon | |
@project_id = project_id | |
@task_type = task_type | |
@tenant = tenant | |
end | |
def tag | |
helper.image_tag(source, alt: description, width: "70") | |
end | |
private | |
attr_reader :helper, :icon, :project_id, :task_type, :tenant | |
def source | |
if icon.nil? | |
"kokoro/themes/#{tenant}/slices/tasks/#{task_type}.png" | |
else | |
helper.project_asset_path(project_id, icon) | |
end | |
end | |
def description | |
if icon.nil? | |
"#{task_type.capitalize} Task" | |
else | |
File.basename(icon, File.extname(icon)).titleize | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TaskDecorator < SimpleDelegator | |
def initialize(task:, view_context:) | |
super(task) | |
@view_context = view_context | |
end | |
def icon_tag | |
IconTag.new( | |
helper: view_context, | |
icon: icon, | |
project_id: project_id, | |
task_type: type, | |
tenant: view_context.tenant_name | |
).tag | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe TaskDecorator do | |
describe "#icon_tag" do | |
context "with no icon set" do | |
it "returns an image element for the given Task type" do | |
task = double("task", icon: nil, type: "survey") | |
view_context = double( | |
"view_context", | |
tenant_name: "crowdlab", | |
params: {}, | |
image_tag: nil | |
) | |
decorator = create_decorator(task: task, view_context: view_context) | |
decorator.icon_tag | |
expected_source = "kokoro/themes/crowdlab/slices/tasks/survey.png" | |
expect(view_context) | |
.to have_received(:image_tag) | |
.with(expected_source, alt: "Survey Task", width: "70") | |
end | |
end | |
context "with an icon set" do | |
it "returns an image element" do | |
icon = "task_icon.png" | |
task = double("task", icon: icon, type: "survey") | |
project_id = "1" | |
expected_source = "/projects/1/assets/#{icon}" | |
view_context = double( | |
"view_context", | |
tenant_name: "crowdlab", | |
params: { project_id: project_id }, | |
image_tag: nil, | |
project_asset_path: expected_source | |
) | |
decorator = create_decorator(task: task, view_context: view_context) | |
decorator.icon_tag | |
# Example using Capybara to integration test instead: | |
# markup = Capybara.string(decorator.icon_tag) | |
# expect(markup).to have_css("img[alt='Task Icon']") | |
# expect(markup).to have_css("img[width='70']") | |
# expect(markup).to have_css("img[src='#{expected_source}']") | |
expect(view_context) | |
.to have_received(:image_tag) | |
.with(expected_source, alt: "Task Icon", width: "70") | |
expect(view_context) | |
.to have_received(:project_asset_path) | |
.with(project_id, icon) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment