|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | +require "zip" |
| 5 | +require "rexml/document" |
| 6 | + |
| 7 | +RSpec.describe "Prince George report template" do |
| 8 | + let(:template_path) { Rails.root.join("app/documents/templates/prince_george_report_template.docx").to_s } |
| 9 | + let(:w_ns) { "http://schemas.openxmlformats.org/wordprocessingml/2006/main" } |
| 10 | + |
| 11 | + def extract_document_xml(docx_path) |
| 12 | + Zip::File.open(docx_path) do |zip| |
| 13 | + entry = zip.find_entry("word/document.xml") |
| 14 | + REXML::Document.new(entry.get_input_stream.read) |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + def find_contacts_table(doc) |
| 19 | + tables = REXML::XPath.match(doc, "//w:tbl", "w" => w_ns) |
| 20 | + tables.find do |tbl| |
| 21 | + text = "" |
| 22 | + REXML::XPath.each(tbl, ".//w:t", "w" => w_ns) { |t| text += t.text.to_s } |
| 23 | + text.include?("Contact Dates") |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + describe "contacts table column widths" do |
| 28 | + it "allocates more width to the Contact Dates column than Name or Title columns" do |
| 29 | + doc = extract_document_xml(template_path) |
| 30 | + table = find_contacts_table(doc) |
| 31 | + expect(table).not_to be_nil, "Could not find contacts table in template" |
| 32 | + |
| 33 | + grid_cols = REXML::XPath.match(table, ".//w:tblGrid/w:gridCol", "w" => w_ns) |
| 34 | + expect(grid_cols.length).to eq(3) |
| 35 | + |
| 36 | + widths = grid_cols.map { |col| col.attributes["w:w"].to_i } |
| 37 | + name_width, title_width, dates_width = widths |
| 38 | + |
| 39 | + expect(dates_width).to be > name_width, "Contact Dates column (#{dates_width}) should be wider than Name column (#{name_width})" |
| 40 | + expect(dates_width).to be > title_width, "Contact Dates column (#{dates_width}) should be wider than Title column (#{title_width})" |
| 41 | + expect(dates_width).to be >= 5760, "Contact Dates column should be at least 4 inches (5760 twips), got #{dates_width}" |
| 42 | + end |
| 43 | + |
| 44 | + it "preserves the total table width" do |
| 45 | + doc = extract_document_xml(template_path) |
| 46 | + table = find_contacts_table(doc) |
| 47 | + expect(table).not_to be_nil, "Could not find contacts table in template" |
| 48 | + |
| 49 | + grid_cols = REXML::XPath.match(table, ".//w:tblGrid/w:gridCol", "w" => w_ns) |
| 50 | + total = grid_cols.sum { |col| col.attributes["w:w"].to_i } |
| 51 | + |
| 52 | + expect(total).to eq(9606), "Total table width should be 9606 twips (original width), got #{total}" |
| 53 | + end |
| 54 | + end |
| 55 | +end |
0 commit comments