0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-09-19 23:31:11 +02:00
discourse-custom-wizard/spec/components/custom_wizard/template_validator_spec.rb
Faizaan Gagan 00682fb4c2
FIX: field validation was not working in backend (#165)
* FIX: field validation was not working in backend

* added tests
2021-12-07 14:20:34 +05:30

81 Zeilen
2,3 KiB
Ruby

# frozen_string_literal: true
require_relative '../../plugin_helper'
describe CustomWizard::TemplateValidator do
fab!(:user) { Fabricate(:user) }
let(:template) {
JSON.parse(File.open(
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
).read).with_indifferent_access
}
it "validates valid templates" do
expect(
CustomWizard::TemplateValidator.new(template).perform
).to eq(true)
end
it "invalidates templates without required attributes" do
template.delete(:id)
expect(
CustomWizard::TemplateValidator.new(template).perform
).to eq(false)
end
it "invalidates templates with duplicate ids if creating a new template" do
CustomWizard::Template.save(template)
expect(
CustomWizard::TemplateValidator.new(template, create: true).perform
).to eq(false)
end
it "validates after time settings" do
template[:after_time] = true
template[:after_time_scheduled] = (Time.now + 3.hours).iso8601
expect(
CustomWizard::TemplateValidator.new(template).perform
).to eq(true)
end
it "invalidates invalid after time settings" do
template[:after_time] = true
template[:after_time_scheduled] = "not a time"
expect(
CustomWizard::TemplateValidator.new(template).perform
).to eq(false)
end
context "steps" do
CustomWizard::TemplateValidator.required[:step].each do |attribute|
it "invalidates if \"#{attribute.to_s}\" is not present" do
template[:steps][0][attribute] = nil
expect(
CustomWizard::TemplateValidator.new(template).perform
).to eq(false)
end
end
end
context "fields" do
CustomWizard::TemplateValidator.required[:field].each do |attribute|
it "invalidates if \"#{attribute.to_s}\" is not present" do
template[:steps][0][:fields][0][attribute] = nil
expect(
CustomWizard::TemplateValidator.new(template).perform
).to eq(false)
end
end
end
context "actions" do
CustomWizard::TemplateValidator.required[:action].each do |attribute|
it "invalidates if \"#{attribute.to_s}\" is not present" do
template[:actions][0][attribute] = nil
expect(
CustomWizard::TemplateValidator.new(template).perform
).to eq(false)
end
end
end
end