|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class ContentBlockDetectorTest < ActiveSupport::TestCase |
| 4 | + setup do |
| 5 | + @flow = mock("flow") |
| 6 | + @flow.stubs(:name).returns("example") |
| 7 | + @flow_class = mock("flow_class") |
| 8 | + @flow_class.stubs(:to_s).returns("ExampleFlow") |
| 9 | + @flow.stubs(:class).returns(@flow_class) |
| 10 | + |
| 11 | + @flow_file = "/path/to/flow.rb" |
| 12 | + @template_file = "/path/to/template.erb" |
| 13 | + @calculator_file = "/path/to/calculator.rb" |
| 14 | + |
| 15 | + @detector = ContentBlockDetector.new(@flow) |
| 16 | + end |
| 17 | + |
| 18 | + context "#flow_filename" do |
| 19 | + should "return the filename for the flow" do |
| 20 | + Object.expects(:const_source_location).with(@flow_class.to_s).returns([@flow_file]) |
| 21 | + |
| 22 | + assert_equal @flow_file, @detector.flow_filename |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + context "#calculators" do |
| 27 | + should "return the filename for the template" do |
| 28 | + flow_content = "FLOW CONTENT" |
| 29 | + calculator = stub("calculator", constantize: "calculator_constantized") |
| 30 | + |
| 31 | + @detector.stubs(:flow_filename).returns(@flow_file) |
| 32 | + File.expects(:read).with(@flow_file).returns(flow_content) |
| 33 | + flow_content.expects(:scan) |
| 34 | + .with(ContentBlockDetector::CALCULATOR_PATTERN) |
| 35 | + .returns([calculator]) |
| 36 | + |
| 37 | + assert_equal [calculator.constantize], @detector.calculators |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context "#calculator_filenames" do |
| 42 | + should "return the filename for the calculators" do |
| 43 | + calculator = stub("calculator") |
| 44 | + |
| 45 | + @detector.stubs(:calculators).returns([calculator]) |
| 46 | + Object.stubs(:const_source_location) |
| 47 | + .with(calculator.to_s) |
| 48 | + .returns([@calculator_file]) |
| 49 | + |
| 50 | + assert_equal [@calculator_file], @detector.calculator_filenames |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + context "#template_filenames" do |
| 55 | + should "return the filename for the templates" do |
| 56 | + glob = File.join("app", "flows", "#{@flow.name.underscore}_flow", "**", "*.erb") |
| 57 | + Dir.expects(:glob).with(glob) |
| 58 | + .returns([@template_file]) |
| 59 | + |
| 60 | + assert_equal [@template_file], @detector.template_filenames |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + context "#flow_content" do |
| 65 | + should "return the content for the flow's files" do |
| 66 | + @detector.stubs(:flow_filename).returns(@flow_file) |
| 67 | + @detector.stubs(:calculator_filenames).returns(@calculator_file) |
| 68 | + @detector.stubs(:template_filenames).returns(@template_file) |
| 69 | + |
| 70 | + flow_content = "FLOW CONTENT" |
| 71 | + calculator_content = "CALCULATOR CONTENT" |
| 72 | + template_content = "TEMPLATE CONTENT" |
| 73 | + |
| 74 | + File.expects(:read).with(@flow_file).returns(flow_content) |
| 75 | + File.expects(:read).with(@calculator_file).returns(calculator_content) |
| 76 | + File.expects(:read).with(@template_file).returns(template_content) |
| 77 | + |
| 78 | + expected_content = [flow_content, calculator_content, template_content].join |
| 79 | + |
| 80 | + assert_equal expected_content, @detector.flow_content |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context "#content_blocks" do |
| 85 | + should "return unique content blocks from flow content" do |
| 86 | + flow_content = "FLOW CONTENT" |
| 87 | + |
| 88 | + @detector.stubs(:flow_content).returns(flow_content) |
| 89 | + |
| 90 | + reference = stub("reference", embed_code: "EMBED CODE") |
| 91 | + content_block = stub("content_block") |
| 92 | + |
| 93 | + ContentBlockTools::ContentBlockReference.stubs(:find_all_in_document) |
| 94 | + .with(flow_content) |
| 95 | + .returns([reference]) |
| 96 | + |
| 97 | + ContentBlockTools::ContentBlock.stubs(:from_embed_code) |
| 98 | + .with(reference.embed_code) |
| 99 | + .returns(content_block) |
| 100 | + |
| 101 | + assert_equal [content_block], @detector.content_blocks |
| 102 | + end |
| 103 | + end |
| 104 | +end |
0 commit comments