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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

124 lines
3.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require_relative "../discourse_automation_helper"
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