|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +describe TranslatedEtiquetteValidator do |
| 6 | + subject { validatable.new(body:, current_organization:) } |
| 7 | + |
| 8 | + let(:validatable) do |
| 9 | + Class.new do |
| 10 | + def self.model_name |
| 11 | + ActiveModel::Name.new(self, nil, "Validatable") |
| 12 | + end |
| 13 | + |
| 14 | + def self.translatable_fields(*fields) |
| 15 | + @translatable_fields = fields |
| 16 | + end |
| 17 | + |
| 18 | + include Decidim::AttributeObject::Model |
| 19 | + include ActiveModel::Validations |
| 20 | + |
| 21 | + attribute :body |
| 22 | + attribute :current_organization |
| 23 | + |
| 24 | + translatable_fields :body |
| 25 | + |
| 26 | + validates :body, translated_etiquette: true |
| 27 | + |
| 28 | + # Add accessor for translated fields |
| 29 | + def body_en |
| 30 | + body[:en] |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + let(:current_organization) { create(:organization, default_locale: :en) } |
| 36 | + |
| 37 | + let(:body) { { en: "A SCREAMING BODY WITH TOO MANY CAPS" } } |
| 38 | + |
| 39 | + context "when Decidim.enable_etiquette_validator is false" do |
| 40 | + before do |
| 41 | + allow(Decidim).to receive(:enable_etiquette_validator).and_return(false) |
| 42 | + end |
| 43 | + |
| 44 | + it "skips validation for all translatable fields" do |
| 45 | + expect(subject).to be_valid |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context "when Decidim.enable_etiquette_validator is true" do |
| 50 | + before do |
| 51 | + allow(Decidim).to receive(:enable_etiquette_validator).and_return(true) |
| 52 | + end |
| 53 | + |
| 54 | + context "with invalid content" do |
| 55 | + it "performs validation on translatable fields" do |
| 56 | + expect(subject).not_to be_valid |
| 57 | + expect(subject.errors[:body_en]).not_to be_empty |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context "with valid content" do |
| 62 | + let(:body) { { en: "This is a reasonable body with proper capitalization" } } |
| 63 | + |
| 64 | + it "allows valid content" do |
| 65 | + expect(subject).to be_valid |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments