From 0ef227b679c7583d32c2361182402093d7039cfa Mon Sep 17 00:00:00 2001 From: Anna Cavalla Date: Fri, 1 Dec 2023 15:16:50 +0000 Subject: [PATCH] Small revisions and extra testing --- app/models/simple_smart_answer_edition.rb | 2 +- app/views/editions/show.html.erb | 1 + .../simple_smart_answer_edition_test.rb | 58 ++++++++++++++----- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/app/models/simple_smart_answer_edition.rb b/app/models/simple_smart_answer_edition.rb index f21200369..45da683fe 100644 --- a/app/models/simple_smart_answer_edition.rb +++ b/app/models/simple_smart_answer_edition.rb @@ -85,7 +85,7 @@ def generate_mermaid "'background': '#FFFFFF',\n 'primaryTextColor': '#0B0C0C',\n " \ "'lineColor': '#0b0c0c',\n 'fontSize': '23.75px' } } }%%\nflowchart TD"] parts << "accTitle: #{title}\naccDescr: A flowchart for the #{title} smart answer\nAA[Start]:::start" - unless nodes.nil? + if nodes.present? parts << "AA---Q#{nodes.first.slug.split('-')[1]}" nodes.each do |node| parts << if node.kind == "question" diff --git a/app/views/editions/show.html.erb b/app/views/editions/show.html.erb index 70aa077bf..41d868f67 100644 --- a/app/views/editions/show.html.erb +++ b/app/views/editions/show.html.erb @@ -60,3 +60,4 @@ <% content_for :page_title, "Editing #{@resource.title}" %> + diff --git a/test/models/simple_smart_answer_edition_test.rb b/test/models/simple_smart_answer_edition_test.rb index 8c8a6015c..5416e9dec 100644 --- a/test/models/simple_smart_answer_edition_test.rb +++ b/test/models/simple_smart_answer_edition_test.rb @@ -241,22 +241,52 @@ class SimpleSmartAnswerEditionTest < ActiveSupport::TestCase end end - should "generating mermaid.js syntax from a simple smart answer" do - edition = FactoryBot.build(:simple_smart_answer_edition, panopticon_id: @artefact.id) - edition.body = "This is a simple smart answer." + context "generating mermaid.js syntax" do + should "generate correct syntax from a simple smart answer with no nodes" do + edition = FactoryBot.build(:simple_smart_answer_edition, panopticon_id: @artefact.id) + edition.update(title: "Smarter than the average answer") + edition.save! + + assert_equal "%%{ init: {\n'theme': 'base',\n'themeVariables': {\n 'background': '#FFFFFF',\n" \ + " 'primaryTextColor': '#0B0C0C',\n 'lineColor': '#0b0c0c',\n 'fontSize': '23.75px'" \ + " } } }%%\nflowchart TD\naccTitle: Smarter than the average answer\naccDescr: A flowchart for the Smarter " \ + "than the average answer smart answer\nAA[Start]:::start\nclassDef answer fill: #F3F2F1, stroke:#505A5F;\nclassDef " \ + "outcome fill: #6FA4D2\nclassDef question fill: #B1B4B6, stroke:#505A5F;\nclassDef start fill:#00703c,color: " \ + "#ffffff", edition.generate_mermaid + end - edition.nodes.build(slug: "question-1", title: "You approach two locked doors. Which do you choose?", kind: "question", options: [{ label: "A tiger fights you", next_node: "outcome-1" }]) - edition.nodes.build(slug: "outcome-1", title: "Tiger wins", kind: "outcome") - edition.save! + should "generate correct syntax from a simple smart answer with one node" do + edition = FactoryBot.build(:simple_smart_answer_edition, panopticon_id: @artefact.id) + edition.update(title: "Smarter than the average answer") + edition.nodes.build(slug: "question-1", title: "You approach two locked doors. Which do you choose?", kind: "question") - assert_equal "%%{ init: {\n'theme': 'base',\n'themeVariables': {\n 'background': '#FFFFFF',\n" \ - " 'primaryTextColor': '#0B0C0C',\n 'lineColor': '#0b0c0c',\n 'fontSize': '23.75px'" \ - " } } }%%\nflowchart TD\naccTitle: Simple smart answer\naccDescr: A flowchart for the Simple " \ - "smart answer smart answer\nAA[Start]:::start\nAA---Q1\nQ1[\"`Q1. You approach two locked doors. " \ - "Which do you choose?`\"]:::question\nQ1---Q1A1\nQ1A1([\"`A1. A tiger fights you`\"]):::answer\n" \ - "Q1A1-->O1\n\nO1{{\"`O1. Tiger wins`\"}}:::outcome\nclassDef answer fill: #F3F2F1, stroke:#505A5F;\nclassDef " \ - "outcome fill: #6FA4D2\nclassDef question fill: #B1B4B6, stroke:#505A5F;\nclassDef start fill:#00703c,color: " \ - "#ffffff", edition.generate_mermaid + edition.save! + + assert_equal "%%{ init: {\n'theme': 'base',\n'themeVariables': {\n 'background': '#FFFFFF',\n" \ + " 'primaryTextColor': '#0B0C0C',\n 'lineColor': '#0b0c0c',\n 'fontSize': '23.75px'" \ + " } } }%%\nflowchart TD\naccTitle: Smarter than the average answer\naccDescr: A flowchart for the Smarter " \ + "than the average answer smart answer\nAA[Start]:::start\nAA---Q1\nQ1[\"`Q1. You approach two locked doors. " \ + "Which do you choose?`\"]:::question\nclassDef answer fill: #F3F2F1, stroke:#505A5F;\nclassDef " \ + "outcome fill: #6FA4D2\nclassDef question fill: #B1B4B6, stroke:#505A5F;\nclassDef start fill:#00703c,color: " \ + "#ffffff", edition.generate_mermaid + end + + should "generate mermaid.js syntax from a simple smart answer with multiple nodes" do + edition = FactoryBot.build(:simple_smart_answer_edition, panopticon_id: @artefact.id) + + edition.nodes.build(slug: "question-1", title: "You approach two locked doors. Which do you choose?", kind: "question", options: [{ label: "A tiger fights you", next_node: "outcome-1" }]) + edition.nodes.build(slug: "outcome-1", title: "Tiger wins", kind: "outcome") + edition.save! + + assert_equal "%%{ init: {\n'theme': 'base',\n'themeVariables': {\n 'background': '#FFFFFF',\n" \ + " 'primaryTextColor': '#0B0C0C',\n 'lineColor': '#0b0c0c',\n 'fontSize': '23.75px'" \ + " } } }%%\nflowchart TD\naccTitle: Simple smart answer\naccDescr: A flowchart for the Simple " \ + "smart answer smart answer\nAA[Start]:::start\nAA---Q1\nQ1[\"`Q1. You approach two locked doors. " \ + "Which do you choose?`\"]:::question\nQ1---Q1A1\nQ1A1([\"`A1. A tiger fights you`\"]):::answer\n" \ + "Q1A1-->O1\n\nO1{{\"`O1. Tiger wins`\"}}:::outcome\nclassDef answer fill: #F3F2F1, stroke:#505A5F;\nclassDef " \ + "outcome fill: #6FA4D2\nclassDef question fill: #B1B4B6, stroke:#505A5F;\nclassDef start fill:#00703c,color: " \ + "#ffffff", edition.generate_mermaid + end end # rubocop:enable Rails/SaveBang end