0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-28 03:40:29 +01:00
discourse-custom-wizard/spec/components/custom_wizard/mapper_spec.rb

510 Zeilen
15 KiB
Ruby

2022-12-13 15:56:20 +01:00
# rubocop:disable Style/FrozenStringLiteralComment
2020-11-03 01:24:20 +01:00
describe CustomWizard::Mapper do
2024-10-16 13:52:03 +02:00
fab!(:user1) do
Fabricate(
:user,
2020-11-03 01:24:20 +01:00
name: "Angus",
username: "angus",
email: "angus@email.com",
2024-10-16 13:52:03 +02:00
trust_level: TrustLevel[3],
2020-11-03 01:24:20 +01:00
)
2024-10-16 13:52:03 +02:00
end
fab!(:user2) do
Fabricate(
:user,
2020-11-03 01:24:20 +01:00
name: "Patrick",
username: "patrick",
email: "patrick@email2.com",
2024-10-16 13:52:03 +02:00
trust_level: TrustLevel[1],
2020-11-03 01:24:20 +01:00
)
2024-10-16 13:52:03 +02:00
end
fab!(:user_field) do
field =
Fabricate(
:user_field,
id: 3,
name: "dropdown_field",
description: "field desc",
field_type: "dropdown",
user_field_options_attributes: [{ value: "a" }, { value: "b" }, { value: "c" }],
)
end
2021-09-07 14:06:13 +02:00
let(:inputs) { get_wizard_fixture("mapper/inputs") }
let(:data) { get_wizard_fixture("mapper/data") }
2024-10-16 13:52:03 +02:00
let(:template_params) { { "step_1_field_1" => "Hello" } }
let(:template_params_empty) do
{ "step_1_field_1" => nil, "step_1_field_2" => nil, "step_1_field_3" => "" }
end
let(:template_params_non_empty) do
{ "step_1_field_1" => nil, "step_1_field_2" => "", "step_1_field_3" => "Value" }
end
let(:template_params_multiple_non_empty) do
{ "step_1_field_1" => nil, "step_1_field_2" => "Value1", "step_1_field_3" => "Value" }
end
let(:template_params_object) { { step_1_field_1: get_wizard_fixture("field/upload") } }
let(:template_params_object_array) do
{ "step_1_field_1" => [{ text: "Hello" }, { text: "World" }] }
end
def create_template_mapper(data, user)
2024-10-16 13:52:03 +02:00
CustomWizard::Mapper.new(data: data, user: user)
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "maps values" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: inputs["assignment"], data: data, user: user1).perform,
).to eq([13])
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "maps associations" do
2024-10-16 13:52:03 +02:00
association =
CustomWizard::Mapper.new(inputs: inputs["association"], data: data, user: user1).perform
2020-11-03 01:24:20 +01:00
expect(association.length).to eq(3)
expect(association.first[:value]).to eq("Choice 1")
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
context "conditional mapping" do
it "maps when the condition is met" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: inputs["conditional"], data: data, user: user1).perform,
).to eq("true")
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "does not map when the condition is not met" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: inputs["conditional"], data: data, user: user2).perform,
).to eq(nil)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "maps when multiple conditions are met" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["conditional_multiple_pairs"],
data: data,
user: user1,
).perform,
).to eq("true")
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "does not map when one of multiple conditions are not met" do
user1.email = "angus@other-email.com"
2021-03-11 07:30:15 +01:00
user1.save
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["conditional_multiple_pairs"],
data: data,
user: user1,
).perform,
).to eq(nil)
2020-11-03 01:24:20 +01:00
end
end
2021-03-11 07:30:15 +01:00
2021-04-22 20:20:54 +02:00
context "conditional validation" do
it "validates valid data" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: inputs["validation"], data: data, user: user1).perform,
).to eq(true)
2021-04-22 20:20:54 +02:00
end
2021-03-11 07:30:15 +01:00
2021-04-22 20:20:54 +02:00
it "does not validate invalid data" do
data["input_2"] = "value 3"
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: inputs["validation"], data: data, user: user1).perform,
).to eq(false)
2021-04-22 20:20:54 +02:00
end
2021-04-22 20:22:38 +02:00
2021-04-22 20:20:54 +02:00
context "using or condition" do
it "validates the data when all of the conditions are met" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper
.new(
inputs: inputs["validation_multiple_pairs"],
data: data,
user: user1,
opts: {
multiple: true,
},
)
.perform
.any?,
).to eq(true)
2021-04-22 20:20:54 +02:00
end
it "validates the data when one of the conditions are met" do
custom_data = data.dup
2024-10-16 13:52:03 +02:00
custom_data["input_1"] = "value 3"
expect(
CustomWizard::Mapper
.new(
inputs: inputs["validation_multiple_pairs"],
data: custom_data,
user: user1,
opts: {
multiple: true,
},
)
.perform
.any?,
).to eq(true)
2021-04-22 20:20:54 +02:00
end
it "doesn't validate the data when none of the conditions are met" do
custom_data = data.dup
2024-10-16 13:52:03 +02:00
custom_data["input_1"] = "value 3"
custom_data["input_2"] = "value 4"
expect(
CustomWizard::Mapper
.new(
inputs: inputs["validation_multiple_pairs"],
data: custom_data,
user: user1,
opts: {
multiple: true,
},
)
.perform
.any?,
).to eq(false)
2021-04-22 20:20:54 +02:00
end
end
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "maps text fields" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: inputs["assignment_text"], data: data, user: user1).perform,
).to eq("Value")
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "maps user fields" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["assignment_user_field"],
data: data,
user: user1,
).perform,
).to eq("Angus")
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "maps user field options" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["assignment_user_field_options"],
data: data,
user: user1,
).perform,
).to eq(%w[a b c])
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "maps wizard fields" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["assignment_wizard_field"],
data: data,
user: user1,
).perform,
).to eq("value 1")
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "maps wizard actions" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["assignment_wizard_action"],
data: data,
user: user1,
).perform,
).to eq("value 2")
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
context "interpolates" do
it "user fields" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["interpolate_user_field"],
data: data,
user: user1,
).perform,
).to eq("Name: Angus")
end
2021-03-11 07:30:15 +01:00
it "user emails" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["interpolate_user_email"],
data: data,
user: user1,
).perform,
).to eq("Email: angus@email.com")
end
2021-03-11 07:30:15 +01:00
it "user options" do
user1.user_option.update_columns(email_level: UserOption.email_level_types[:never])
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["interpolate_user_option"],
data: data,
user: user1,
).perform,
).to eq("Email Level: #{UserOption.email_level_types[:never]}")
end
it "date" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["interpolate_timestamp"],
data: data,
user: user1,
).perform,
).to eq("Time: #{Time.now.strftime("%B %-d, %Y")}")
end
2022-12-12 13:19:04 +01:00
it "avatar" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["interpolate_avatar"],
data: data,
user: user1,
).perform,
).to eq("Avatar: ![avatar](#{user1.small_avatar_url})")
2022-12-12 13:19:04 +01:00
end
it "avatar with invalid size" do
2024-10-16 13:52:03 +02:00
avatar_inputs = inputs["interpolate_avatar"].dup
2022-12-12 13:19:04 +01:00
avatar_inputs[0]["output"] = "Avatar: ![avatar](u{avatar.345})"
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: avatar_inputs, data: data, user: user1).perform,
).to eq("Avatar: ![avatar](#{user1.small_avatar_url})")
2022-12-12 13:19:04 +01:00
end
it "avatar with valid size" do
2024-10-16 13:52:03 +02:00
avatar_inputs = inputs["interpolate_avatar"].dup
2023-06-02 12:51:28 +02:00
avatar_inputs[0]["output"] = "Avatar: ![avatar](u{avatar.144})"
2022-12-12 13:19:04 +01:00
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: avatar_inputs, data: data, user: user1).perform,
).to eq("Avatar: ![avatar](#{user1.avatar_template_url.gsub("{size}", "144")})")
2022-12-12 13:19:04 +01:00
end
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2023-05-03 11:01:54 +02:00
it "handles not equal pairs" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: inputs["not_equals_pair"], data: data, user: user1).perform,
).to eq(true)
expect(
CustomWizard::Mapper.new(inputs: inputs["not_equals_pair"], data: data, user: user2).perform,
).to eq(false)
2023-05-03 11:01:54 +02:00
end
2020-11-03 01:24:20 +01:00
it "handles greater than pairs" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["greater_than_pair"],
data: data,
user: user1,
).perform,
).to eq(true)
expect(
CustomWizard::Mapper.new(
inputs: inputs["greater_than_pair"],
data: data,
user: user2,
).perform,
).to eq(false)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "handles less than pairs" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: inputs["less_than_pair"], data: data, user: user1).perform,
).to eq(false)
expect(
CustomWizard::Mapper.new(inputs: inputs["less_than_pair"], data: data, user: user2).perform,
).to eq(true)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "handles greater than or equal pairs" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["greater_than_or_equal_pair"],
data: data,
user: user1,
).perform,
).to eq(true)
expect(
CustomWizard::Mapper.new(
inputs: inputs["greater_than_or_equal_pair"],
data: data,
user: user2,
).perform,
).to eq(true)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "handles less than or equal pairs" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(
inputs: inputs["less_than_or_equal_pair"],
data: data,
user: user1,
).perform,
).to eq(true)
expect(
CustomWizard::Mapper.new(
inputs: inputs["less_than_or_equal_pair"],
data: data,
user: user2,
).perform,
).to eq(true)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "handles regex pairs" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: inputs["regex_pair"], data: data, user: user1).perform,
).to eq(true)
expect(
CustomWizard::Mapper.new(inputs: inputs["regex_pair"], data: data, user: user2).perform,
).to eq(false)
2020-11-03 01:24:20 +01:00
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
it "handles shorthand pairs" do
2024-10-16 13:52:03 +02:00
expect(
CustomWizard::Mapper.new(inputs: inputs["shorthand_pair"], data: data, user: user1).perform,
).to eq(false)
2020-11-03 01:24:20 +01:00
end
context "output templating" do
it "passes the correct values to the template" do
template = "w{step_1_field_1}"
mapper = create_template_mapper(template_params, user1)
2024-10-16 13:52:03 +02:00
result =
mapper.interpolate(template.dup, template: true, user: true, wizard: true, value: true)
expect(result).to eq(template_params["step_1_field_1"])
end
it "does not require a subscription" do
template = '{{ "w{step_1_field_1}" | size }}'
mapper = create_template_mapper(template_params, user1)
2024-10-16 13:52:03 +02:00
result =
mapper.interpolate(template.dup, template: true, user: true, wizard: true, value: true)
expect(result).to eq("5")
end
2021-09-24 11:58:42 +02:00
context "with a subscription" do
2024-10-16 13:52:03 +02:00
before { enable_subscription("standard") }
2021-09-07 14:06:13 +02:00
it "treats replaced values as string literals" do
template = '{{ "w{step_1_field_1}" | size }}'
mapper = create_template_mapper(template_params, user1)
2024-10-16 13:52:03 +02:00
result =
mapper.interpolate(template.dup, template: true, user: true, wizard: true, value: true)
2021-09-07 14:06:13 +02:00
expect(result).to eq(template_params["step_1_field_1"].size.to_s)
end
2021-09-07 14:06:13 +02:00
it "allows the wizard values to be used inside conditionals" do
template = <<-LIQUID
{%- if "w{step_1_field_1}" contains "ello" -%}
Correct
{%- else -%}
Incorrect
{%-endif-%}
LIQUID
2021-09-07 14:06:13 +02:00
mapper = create_template_mapper(template_params, user1)
2024-10-16 13:52:03 +02:00
result =
mapper.interpolate(template.dup, template: true, user: true, wizard: true, value: true)
2021-09-07 14:06:13 +02:00
expect(result).to eq("Correct")
end
2021-09-07 14:06:13 +02:00
it "can access data passed to render method as variable" do
template = "{{step_1_field_1.size}}"
mapper = create_template_mapper(template_params, user1)
2024-10-16 13:52:03 +02:00
result =
mapper.interpolate(template.dup, template: true, user: true, wizard: true, value: true)
2021-09-07 14:06:13 +02:00
expect(result).to eq(template_params["step_1_field_1"].size.to_s)
end
it "doesn't parse the template when template param is false" do
template = <<-LIQUID.strip
{{ "w{step_1_field_1}" | size}}
LIQUID
mapper = create_template_mapper(template_params, user1)
2024-10-16 13:52:03 +02:00
result = mapper.interpolate(template.dup, template: false)
2021-09-07 14:06:13 +02:00
expect(result).to eq(template)
end
it "handles correct object variable references" do
template = <<-LIQUID.strip
{%- if "w{step_1_field_1.id}" == "step_2_field_7" -%}
Correct
{%- else -%}
Incorrect
{%-endif-%}
LIQUID
mapper = create_template_mapper(template_params_object, user1)
2024-10-16 13:52:03 +02:00
result = mapper.interpolate(template.dup, template: true, wizard: true)
expect(result).to eq("Correct")
end
it "handles incorrect object variable references" do
template = <<-LIQUID.strip
{%- if "w{step_1_field_1}" == "step_2_field_7" -%}
Correct
{%- else -%}
Incorrect
{%-endif-%}
LIQUID
mapper = create_template_mapper(template_params_object, user1)
2024-10-16 13:52:03 +02:00
result = mapper.interpolate(template.dup, template: true, wizard: true)
expect(result).to eq("Incorrect")
end
2024-06-20 13:06:02 +02:00
it "iterates over an interpolated list of objects" do
template = <<-LIQUID.strip
{% for object in step_1_field_1 %}{{object.text}} {% endfor %}
LIQUID
2024-10-16 13:52:03 +02:00
mapper = create_template_mapper(template_params_object_array, user1)
result =
mapper.interpolate(template.dup, template: true, user: true, wizard: true, value: true)
expect(result).to eq("Hello World ")
2024-06-20 13:06:02 +02:00
end
2021-09-07 14:06:13 +02:00
context "custom filter: 'first_non_empty'" do
it "gives first non empty element from list" do
template = <<-LIQUID.strip
{%- assign entry = "" | first_non_empty: step_1_field_1, step_1_field_2, step_1_field_3 -%}
{{ entry }}
LIQUID
mapper = create_template_mapper(template_params_non_empty, user1)
2024-10-16 13:52:03 +02:00
result =
mapper.interpolate(template.dup, template: true, user: true, wizard: true, value: true)
2021-09-07 14:06:13 +02:00
expect(result).to eq(template_params_non_empty["step_1_field_3"])
end
it "gives first non empty element from list when multiple non empty values present" do
template = <<-LIQUID.strip
{%- assign entry = "" | first_non_empty: step_1_field_1, step_1_field_2, step_1_field_3 -%}
{{ entry }}
LIQUID
mapper = create_template_mapper(template_params_multiple_non_empty, user1)
2024-10-16 13:52:03 +02:00
result =
mapper.interpolate(template.dup, template: true, user: true, wizard: true, value: true)
2021-09-07 14:06:13 +02:00
expect(result).to eq(template_params_multiple_non_empty["step_1_field_2"])
end
it "gives empty if all elements are empty" do
template = <<-LIQUID.strip
{%- assign entry = "" | first_non_empty: step_1_field_1, step_1_field_2, step_1_field_3 -%}
{%- if entry -%}
{{ entry }}
{%- endif -%}
LIQUID
mapper = create_template_mapper(template_params_empty, user1)
2024-10-16 13:52:03 +02:00
result =
mapper.interpolate(template.dup, template: true, user: true, wizard: true, value: true)
2021-09-07 14:06:13 +02:00
expect(result).to eq("")
end
end
end
end
2021-03-11 07:30:15 +01:00
end