Skip to content

Commit cc76326

Browse files
committed
Fix GraphPresenter to work with both i18n & ERB
In #2103 I forgot that we still need to support flows/questions with their content in i18n YAML files vs ERB templates. In this commit I've renamed the `GraphPresenterTest` to `GraphPresenterWithErbRendererTest` and reinstated the earlier version of `GraphPresenterTest` to give suitable test coverage. I've also reinstated the i18n YAML file and used an explicit call to `Flow#use_erb_templates_for_questions` in `GraphPresenterWithErbRendererTest` to switch over to ERB rendering. I've avoided needing to handle `I18n::MissingInterpolationArgument` by passing in an empty `Hash` as the `state` when instantiating the `QuestionPresenter`.
1 parent 07decbf commit cc76326

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

lib/graph_presenter.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class GraphPresenter
22
def initialize(flow)
33
@flow = flow
4+
@i18n_prefix = "flow.#{@flow.name}"
45
end
56

67
def labels
@@ -81,7 +82,7 @@ def method_missing(method, *args, &block)
8182
end
8283

8384
def node_title(node)
84-
presenter = QuestionPresenter.new(nil, node, nil, helpers: [MethodMissingHelper])
85+
presenter = QuestionPresenter.new(@i18n_prefix, node, {}, helpers: [MethodMissingHelper])
8586
presenter.title
8687
end
8788

test/fixtures/smart_answer_flows/graph.rb

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ def define
44
name 'graph'
55
status :draft
66

7-
use_erb_templates_for_questions
8-
97
multiple_choice :q1? do
108
option :yes
119
option :no
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
en-GB:
2+
flow:
3+
graph:
4+
q1?:
5+
title: "What is the answer to q1?"
6+
7+
q2?:
8+
title: "What is the answer to q2?"
9+
10+
q_with_interpolation?:
11+
title: "Question with %{interpolation}?"

test/unit/graph_presenter_test.rb

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
require_relative '../test_helper'
2-
require_relative '../helpers/fixture_flows_helper'
3-
require_relative '../fixtures/smart_answer_flows/graph'
2+
require_relative '../helpers/i18n_test_helper'
3+
4+
require 'fixtures/smart_answer_flows/graph'
45

56
module SmartAnswer
67
class GraphPresenterTest < ActiveSupport::TestCase
7-
include FixtureFlowsHelper
8+
include I18nTestHelper
89

910
setup do
10-
setup_fixture_flows
11+
use_additional_translation_file(fixture_file('smart_answer_flows/locales/en/graph.yml'))
12+
1113
@flow = SmartAnswer::GraphFlow.build
1214
@presenter = GraphPresenter.new(@flow)
1315
end
1416

1517
teardown do
16-
teardown_fixture_flows
18+
reset_translation_files
1719
end
1820

19-
test "presents labels of graph flow" do
21+
test "presents labels of simple graph" do
2022
expected_labels = {
2123
q1?: "MultipleChoice\n-\nWhat is the answer to q1?\n\n( ) yes\n( ) no",
2224
q2?: "MultipleChoice\n-\nWhat is the answer to q2?\n\n( ) a\n( ) b",
23-
q_with_interpolation?: "MultipleChoice\n-\nQuestion with <%= inter.pol.ation %>?\n\n( ) x\n( ) y",
25+
q_with_interpolation?: "MultipleChoice\n-\nQuestion with %{interpolation}?\n\n( ) x\n( ) y",
2426
done_a: "Outcome\n-\ndone_a",
2527
done_b: "Outcome\n-\ndone_b"
2628
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require_relative '../test_helper'
2+
require_relative '../helpers/fixture_flows_helper'
3+
require_relative '../fixtures/smart_answer_flows/graph'
4+
5+
module SmartAnswer
6+
class GraphPresenterWithErbRendererTest < ActiveSupport::TestCase
7+
include FixtureFlowsHelper
8+
9+
setup do
10+
setup_fixture_flows
11+
@flow = SmartAnswer::GraphFlow.new
12+
@flow.use_erb_templates_for_questions
13+
@flow.define
14+
@presenter = GraphPresenter.new(@flow)
15+
end
16+
17+
teardown do
18+
teardown_fixture_flows
19+
end
20+
21+
test "presents labels of graph flow" do
22+
expected_labels = {
23+
q1?: "MultipleChoice\n-\nWhat is the answer to q1?\n\n( ) yes\n( ) no",
24+
q2?: "MultipleChoice\n-\nWhat is the answer to q2?\n\n( ) a\n( ) b",
25+
q_with_interpolation?: "MultipleChoice\n-\nQuestion with <%= inter.pol.ation %>?\n\n( ) x\n( ) y",
26+
done_a: "Outcome\n-\ndone_a",
27+
done_b: "Outcome\n-\ndone_b"
28+
}
29+
30+
assert_equal expected_labels, @presenter.labels
31+
end
32+
33+
test "presents adjacency_list of simple graph" do
34+
expected_adjacency_list = {
35+
q1?: [[:q2?, ""]],
36+
q2?: [[:done_a, ''], [:q_with_interpolation?, '']],
37+
q_with_interpolation?: [[:done_b, ""]],
38+
done_a: [],
39+
done_b: []
40+
}
41+
42+
assert_equal expected_adjacency_list, @presenter.adjacency_list
43+
end
44+
45+
test "indicates does not define transitions in a way which can be visualised" do
46+
p = GraphPresenter.new(SmartAnswer::GraphFlow.build)
47+
assert p.visualisable?, "'graph' should be visualisable"
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)