discourse/plugins/automation/spec/models/field_spec.rb

122 lines
3.0 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
describe DiscourseAutomation::Field do
describe "post field" do
DiscourseAutomation::Scriptable.add("test_post_field") { field :foo, component: :post }
fab!(:automation) { Fabricate(:automation, script: "test_post_field") }
it "works with an empty value" do
field =
DiscourseAutomation::Field.create(automation: automation, component: "post", name: "foo")
expect(field).to be_valid
end
it "works with a text value" do
field =
DiscourseAutomation::Field.create(
automation: automation,
component: "post",
name: "foo",
metadata: {
value: "foo",
},
)
expect(field).to be_valid
end
it "doesnt work with an object value" do
field =
DiscourseAutomation::Field.create(
automation: automation,
component: "post",
name: "foo",
metadata: {
value: {
x: 1,
},
},
)
expect(field).to_not be_valid
end
end
describe "period field" do
DiscourseAutomation::Scriptable.add("test_period_field") { field :foo, component: :period }
fab!(:automation) { Fabricate(:automation, script: "test_period_field") }
it "works with an object value" do
value = { interval: "2", frequency: "day" }
field =
DiscourseAutomation::Field.create(
automation: automation,
component: "period",
name: "foo",
metadata: {
value: value,
},
)
expect(field).to be_valid
end
end
describe "choices field" do
DiscourseAutomation::Scriptable.add("test_choices_field") { field :foo, component: :choices }
fab!(:automation) { Fabricate(:automation, script: "test_choices_field") }
it "works with a string value" do
field =
DiscourseAutomation::Field.create(
automation: automation,
component: "choices",
name: "foo",
metadata: {
value: "some text",
},
)
expect(field).to be_valid
end
it "works with an integer value" do
field =
DiscourseAutomation::Field.create(
automation: automation,
component: "choices",
name: "foo",
metadata: {
value: 21,
},
)
expect(field).to be_valid
end
it "does not work with an array value" do
field =
DiscourseAutomation::Field.create(
automation: automation,
component: "choices",
name: "foo",
metadata: {
value: [1, 2, 3],
},
)
expect(field).to_not be_valid
end
it "works with a nil value" do
field =
DiscourseAutomation::Field.create(
automation: automation,
component: "choices",
name: "foo",
metadata: {
value: nil,
},
)
expect(field).to be_valid
end
end
end