0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-14 22:02:53 +01:00
discourse-custom-wizard/spec/extensions/custom_field_extensions_spec.rb

113 Zeilen
3,2 KiB
Ruby

# frozen_string_literal: true
describe "custom field extensions" do
2024-10-16 13:52:03 +02:00
fab!(:topic)
fab!(:post)
fab!(:category)
fab!(:group)
fab!(:user)
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
let(:custom_field_json) { get_wizard_fixture("custom_field/custom_fields") }
2024-10-16 13:52:03 +02:00
let(:subscription_custom_field_json) do
get_wizard_fixture("custom_field/subscription_custom_fields")
end
2021-03-11 07:30:15 +01:00
before do
2024-10-16 13:52:03 +02:00
custom_field_json["custom_fields"].each do |field_json|
custom_field = CustomWizard::CustomField.new(nil, field_json)
custom_field.save
end
end
2021-03-11 07:30:15 +01:00
context "topic" do
it "registers topic custom fields" do
topic
expect(Topic.get_custom_field_descriptor("topic_field_1").type).to eq(:boolean)
end
2021-03-11 07:30:15 +01:00
it "adds topic custom fields to the topic_view serializer" do
topic.custom_fields["topic_field_1"] = true
topic.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
serializer =
TopicViewSerializer.new(
TopicView.new(topic.id, user),
scope: Guardian.new(user),
root: false,
).as_json
2021-03-11 07:30:15 +01:00
expect(serializer[:topic_field_1]).to eq(true)
end
2021-03-11 07:30:15 +01:00
it "adds topic custom fields to the topic_list_item serializer" do
topic.custom_fields["topic_field_1"] = true
topic.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
serializer =
TopicListItemSerializer.new(topic, scope: Guardian.new(user), root: false).as_json
2021-03-11 07:30:15 +01:00
expect(serializer[:topic_field_1]).to eq(true)
end
end
2021-03-11 07:30:15 +01:00
context "post" do
it "registers post custom fields" do
post
expect(Post.get_custom_field_descriptor("post_field_1").type).to eq(:integer)
end
2021-03-11 07:30:15 +01:00
it "adds post custom fields to the post serializer" do
post.custom_fields["post_field_1"] = 7
post.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
serializer = PostSerializer.new(post, scope: Guardian.new(user), root: false).as_json
2021-03-11 07:30:15 +01:00
expect(serializer[:post_field_1]).to eq(7)
end
end
2021-03-11 07:30:15 +01:00
2021-09-24 11:58:42 +02:00
context "subscription custom fields" do
2021-09-07 14:06:13 +02:00
before do
enable_subscription("business")
2021-09-07 14:06:13 +02:00
2024-10-16 13:52:03 +02:00
subscription_custom_field_json["custom_fields"].each do |field_json|
2021-09-07 14:06:13 +02:00
custom_field = CustomWizard::CustomField.new(nil, field_json)
custom_field.save
end
end
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
context "category" do
it "registers" do
category
expect(Category.get_custom_field_descriptor("category_field_1").type).to eq(:json)
2021-09-07 14:06:13 +02:00
end
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
it "adds custom fields to the basic category serializer" do
category.custom_fields["category_field_1"] = { a: 1, b: 2 }.to_json
category.save_custom_fields(true)
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
serializer =
BasicCategorySerializer.new(category, scope: Guardian.new(user), root: false).as_json
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
expect(serializer[:category_field_1]).to eq({ a: 1, b: 2 }.to_json)
end
end
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
context "group" do
it "registers" do
group
expect(Group.get_custom_field_descriptor("group_field_1").type).to eq(:string)
2021-09-07 14:06:13 +02:00
end
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
it "adds custom fields to the basic group serializer" do
group.custom_fields["group_field_1"] = "Hello"
group.save_custom_fields(true)
2024-10-16 13:52:03 +02:00
serializer = BasicGroupSerializer.new(group, scope: Guardian.new(user), root: false).as_json
2021-03-11 07:30:15 +01:00
2021-09-07 14:06:13 +02:00
expect(serializer[:group_field_1]).to eq("Hello")
end
end
end
2021-03-11 07:30:15 +01:00
end