|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | require "rails_helper" |
2 | 4 |
|
3 | 5 | RSpec.describe FdfInputsService, type: :service do |
4 | | - # TODO: Add tests for FdfInputsService |
| 6 | + describe ".clean" do |
| 7 | + context "when the string is nil" do |
| 8 | + it "returns nil" do |
| 9 | + expect(FdfInputsService.clean("")).to be_nil |
| 10 | + expect(FdfInputsService.clean(" ")).to be_nil |
| 11 | + expect(FdfInputsService.clean(nil)).to be_nil |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + it "returns the escaped string" do |
| 16 | + expect(FdfInputsService.clean("hello world")).to eq("hello world") |
| 17 | + expect(FdfInputsService.clean("(test)")).to eq("\\(test\\)") |
| 18 | + expect(FdfInputsService.clean("path\\to\\file")).to eq("path\\\\to\\\\file") |
| 19 | + expect(FdfInputsService.clean("(a\\b)")).to eq("\\(a\\\\b\\)") |
| 20 | + expect(FdfInputsService.clean("((a))")).to eq("\\(\\(a\\)\\)") |
| 21 | + expect(FdfInputsService.clean("\\(test\\)")).to eq("\\\\\\(test\\\\\\)") |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + describe "#write_to_file" do |
| 26 | + it "calls PdfForms with the given arguments and returns a file" do |
| 27 | + inputs = {name: "Bob Cat"} |
| 28 | + pdf_template_path = "/path/to/template.pdf" |
| 29 | + basename = "test_file" |
| 30 | + |
| 31 | + fake_pdf_forms = double("PdfForms") |
| 32 | + allow(PdfForms).to receive(:new).and_return(fake_pdf_forms) |
| 33 | + allow(fake_pdf_forms).to receive(:fill_form_with_fdf) |
| 34 | + |
| 35 | + service = FdfInputsService.new( |
| 36 | + inputs: inputs, |
| 37 | + pdf_template_path: pdf_template_path, |
| 38 | + basename: basename |
| 39 | + ) |
| 40 | + |
| 41 | + result = service.write_to_file |
| 42 | + |
| 43 | + expect(fake_pdf_forms).to have_received(:fill_form_with_fdf) do |template, output_path, fdf_path, flatten| |
| 44 | + expect(template).to eq(pdf_template_path) |
| 45 | + expect(output_path).to be_a(String) |
| 46 | + expect(File.exist?(output_path)).to be(true) |
| 47 | + expect(fdf_path).to be_a(String) |
| 48 | + expect(File.exist?(fdf_path)).to be(true) |
| 49 | + expect(flatten).to eq(flatten: true) |
| 50 | + end |
5 | 51 |
|
6 | | - pending "add some tests for FdfInputsService" |
| 52 | + expect(result).to be_a(Tempfile) |
| 53 | + expect(File.exist?(result.path)).to be(true) |
| 54 | + end |
| 55 | + end |
7 | 56 | end |
0 commit comments